Hashmap contains key

I found this program in my tutorial, which basically takes into account the appearance of each row in the String tst array.

public class Test { private static HashMap<String, Integer> mp = new HashMap<String, Integer>(); public static void main(String[] args) { String[] tst = new String[] { "ABC", "DEF", "DEF", "DEF","ABC", "DEF", "ABC" }; checkMap(tst); } public static void checkMap(String[] str) { for (String st : str) { if (!mp.containsKey(st)) { mp.put(st, 1); } else { Integer ct = mp.get(st); if(ct!=null) { ct++; mp.put(st, ct); } } } for (Map.Entry<String, Integer> entry : mp.entrySet()) { System.out.println(entry.getKey() + " ocurrs " + entry.getValue()+ " times"); } } 

}

The output for the code is

 ABC ocurrs 3 times DEF ocurrs 4 times 

My question here in the if / else statement is -

 if (!mp.containsKey(st)) { mp.put(st, 1); } else { Integer ct = mp.get(st); if(ct!=null) { ct++; mp.put(st, ct); } } 

When we do not put any entries inside the hash map (the hashmap is empty), on what basis does it work? Sorry if this is a very simple question, but I have not found an answer anywhere on the Internet that explains this. I am confused by what is written in the if / else loop. Also, this line here is -

 Integer ct = mp.get(st); 

How can we get the value the key is mapped to when the infact hashmap is actually empty? I am trying to associate this with an array. If you request the elements of an array after its creation, but are not initialized, it returns a null pointer. Someone please explain how this works for a hash map. Once again, we apologize for asking this basic question.

+4
source share
5 answers

Well, in this line you check if the card contains a key

 if (!mp.containsKey(st)) { 

Since there is an expression before ! , this means "if the card does not contain a key." After that, the β€œthen” block follows where you insert the key on the card with a value of 1 (since it does not exist).

Otherwise, if the key exists ( else block), you take the value for this key, increase it ( ct++ ) and add it again to the map for the same key.

Let me just say that null checking ( if(ct!=null) ) is not required for this code.


General remark on this subject:

How can we get the value the key is mapped to when the infact hashmap is actually empty?

If you try to get something from the HashMap for a key that is not on the map, the map returns null . This is true for any key you are trying to get from a blank card.


Could you explain what this means - Integer ct = mp.get(st);

map.get(key) returns the value that is stored for this key. The card itself is a set of key-value pairs, which means: for each key on the card there is one value. Therefore, to get the value stored for this key, you call map.get(key) . If you save map.put("ABC", 10) , the map will return 10 for map.get("ABC") .

+3
source
  • This is because the containsKey function checks to see if hashMap contains a specific key.
  • If the HashMap is mpty and you are trying to execute a non-existing key, you will get a null value
+1
source

Zero check is not . Either the key is contained in the card, and its value is not null, or it is not contained in the card.

The reason we can be sure that the value is never null is because the map (and all its contents) is defined and used in the method, and there is no way to get a null value.

Although the get() method returns null if a key is passed that it does not contain, this will never happen with this code.

In any case, the code is inelegant: all these lines can be expressed as one simple line:

 mp.put(mp.containsKey(st) ? mp.get(st) + 1 : 1); 
0
source

Test:

 if (!mp.containsKey(st)) 

checks if this record is on the map.

Therefore, it is logical that an entry exists in the else branch and has a non-zero value ... Which makes the test ct == null redundant.

And when the value exists, the get() code adds the existing value to it 1 (in fact, it creates a new Integer , and another story) and put() returns the new value.

Please note that this code mixes autoboxing and non autoboxing. mp.put(st, 1) does autoboxing; behind the scenes, he really does mp.put(st, new Integer(1)) .

Similarly:

 Integer ct = mp.get(st); ct++; 

really:

 Integer ct = mp.get(st); Integer tmp = new Integer(ct.intValue() + 1); ct = tmp; 
0
source

st is here in a for (String st : str) loop for (String st : str) . This has nothing to do with HashMap .

 if (!mp.containsKey(st)) { 

This checks if the HashMap not contains the st key. If there are no elements, it obviously cannot contain the key. Then, in the else block, it uses mp.get(st) , which will now always be successful, since it has been verified that mp contains st (in fact, it does not contain it).

The zero check if (ct == null) here, because if for some reason the card contained null for the key in question. This, however, should not be possible if the code only puts integers on the card and checks for the key, so we delete the zero-checking pool.

0
source

Source: https://habr.com/ru/post/1487605/


All Articles