Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java collections - keyset() vs entrySet() in map

I put a string array elements is a map where elements of string array is key and frequency of word is value, e.g.:

String[] args = {"if","it","is","to","be","it","is","up","me","to","delegate"}; 

then the map will have entries like [ if:1, it:2 .... ]

Set<String> keys = m.keySet(); System.out.println("keyset of the map : "+keys); 

prints all keys: "if","it","is","to","be","it","is","up","me","to","delegate"

Set<Map.Entry<String, Integer>> entrySet = m.entrySet(); Iterator<Map.Entry<String, Integer>> i = entrySet.iterator(); while(i.hasNext()){     Map.Entry<String, Integer> element = i.next();     System.out.println("Key: "+element.getKey()+" ,value: "+element.getValue()); } 

prints all key values pairs :

Using entry set prints all values:

Key: if ,value: 1 Key: it ,value: 2 Key: is ,value: 2 Key: to ,value: 2 Key: be ,value: 1 Key: up ,value: 1 Key: me ,value: 1 Key: delegate ,value: 1 

But the block of code below should print exactly the same output as above, but it does not:

Iterator<String> itr2 = keys.iterator(); while(itr2.hasNext()){     //System.out.println(itr1.next()+" ");     //System.out.println(m.get(itr1.next())+" ");     System.out.println("Key: "+itr2.next()+" ,value: "+m.get(itr2.next())); } 

It prints:

Key: if ,value: 2 Key: is ,value: 2 Key: be ,value: 1 Key: me ,value: 1 

But if we uncomment line 1 in the while loop i.e

System.out.println(itr1.next()+" "); 

and comment the line

System.out.println("Key: "+itr2.next()+" ,value: "+m.get(itr2.next())); 

Then we get all keys: {"if","it","is","to","be","it","is","up","me","to","delegate"};

If we use m.get() with itr2.next(), then the iterator does not have few keys!

like image 857
AllTooSir Avatar asked Jan 22 '12 16:01

AllTooSir


People also ask

What is use of keySet () in map?

HashMap. keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them.

What is entrySet in map in Java?

The Java HashMap entrySet() returns a set view of all the mappings (entries) present in the hashmap. The syntax of the entrySet() method is: hashmap.entrySet() Here, hashmap is an object of the HashMap class.

What does the map method returning views entrySet () return?

entrySet() method in Java is used to create a set out of the same elements contained in the hash map. It basically returns a set view of the hash map or we can create a new set and store the map elements into them.

How do I get the value of a map using keySet?

in your current code, you are doing the following: for (String dog: data. keySet()) { // use the dog String race = data. get(dog); // this will give the value of race for the key dog // using dog to do fetch details from site... }


2 Answers

Every call to the Iterator.next() moves the iterator to the next element. If you want to use the current element in more than one statement or expression, you have to store it in a local variable. Or even better, why don't you simply use a for-each loop?

for (String key : map.keySet()) {     System.out.println(key + ":" + map.get(key)); } 

Moreover, loop over the entrySet is faster, because you don't query the map twice for each key. Also Map.Entry implementations usually implement the toString() method, so you don't have to print the key-value pair manually.

for (Entry<String, Integer> entry : map.entrySet()) {     System.out.println(entry); } 
like image 182
Natix Avatar answered Sep 20 '22 06:09

Natix


Every time you call itr2.next() you are getting a distinct value. Not the same value. You should only call this once in the loop.

Iterator<String> itr2 = keys.iterator();     while(itr2.hasNext()){         String v = itr2.next();         System.out.println("Key: "+v+" ,value: "+m.get(v));     } 
like image 23
Vincent Ramdhanie Avatar answered Sep 21 '22 06:09

Vincent Ramdhanie