Sword refers to Offer-51-the difference between the first non-repeated character in the character stream and the HashMap and LinkedHashMap in this question

Title description

Please implement a function to find the first character that appears only once in the character stream. For example, when the first two characters "go" are read only from the character stream, the first character that appears only once is "g". When reading the first six characters "google" from the character stream, the first character that appears only once is "l".

Output description:

If there is no character that appears once in the current character stream, the # character is returned.

Idea analysis

What is used here is LinkedHashMapto solve. Input it to judge whether it is contained in the map, and put it in and set the value to 1, if it does not. If it contains, take the value value and add 1 to it.

Code

import java.util.Map;
import java.util.LinkedHashMap;
public class Solution {
    
    
    Map<Character,Integer> map = new LinkedHashMap<>();
    //Insert one char from stringstream
    public void Insert(char ch)
    {
    
    
        if(!map.containsKey(ch)){
    
    
            map.put(ch,1);
        }else{
    
    
            map.put(ch,map.get(ch)+1);
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
    
    
        //如果大小为0就返回‘#’
       if(map.size() == 0) return '#';
       for(char c : map.keySet()){
    
    
           if(map.get(c)==1){
    
    
               return c;
           }
       }
        return '#';
    }
}

Look at the difference between HashMap and LinkedHashMap

  • Look at the storage form of HashMap:
    Insert picture description here

From here, we can see that the underlying data structure of HashMap is an array (Entry) + linked list (only the header interpolation method is available, and the length is more than 8 converted into a red-black tree).

  • LinkedHashMap is a subclass of HashMap, but there is also a doubly linked list that maintains the order of key-value pairs. Each key-value pair is located in both the hash table and the doubly linked list. LinkedHashMap supports two kinds of sequence insertion sequence, access sequence
    1. Insertion sequence: the one added first is in the front, and the one added later is in the back. Modification operation does not affect the order
    2. Access sequence: The so-called access refers to the get/put operation. After the get/put operation is performed on a key, the corresponding key-value pair will move to the end of the linked list, so the last one is the most recently accessed , The first is the one that has not been visited for the longest time. This is the order of visits.
  • Why use LinkedHashMap instead of HashMap here?

First of all: HashMap is a hash table. When storing, it is stored according to the hashcode, so when traversing, the order of obtaining data is completely random.
LinkedHashMap saves the insertion order of records. When traversing LinkedHashMap, the first record must be inserted first. It can also be constructed with parameters and sorted according to the number of applications.
这里我们有一个先后顺序的关系,先为第一个出现一次的先出去. This is the reason why only LinkedHashMap is used instead of HashMap (here you can also consider using a queue to make a record)

Second: It will be slower than HashMap when traversing, but there is an exception: when the HashMap has a large capacity and the actual data is small, the traversal may be slower than the LinkedHashMap. Because the traversal speed of LinkedHashMap is only related to actual data, and has nothing to do with capacity, while the traversal speed of HashMap is related to its capacity. Here, the incoming and outgoing character streams are frequently accessed, so the first few data will not be very large.

Guess you like

Origin blog.csdn.net/H1517043456/article/details/107504778