The first is the use of the HashMap EntrySet () method:
map<string,string> map = new hashmap<string,string> (); Iterator iter = Map.entryset (). Iterator (); while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next (); String key = Entry.getkey (); String val = Entry.getvalue (); }
The second is to use HashMap's keyset () method:
map<string,string> map = new hashmap<string,string> (); Iterator iter = Map.keyset (). Iterator (); while (Iter.hasnext ()) {String key = Iter.next (); String val = map.get (key); }
In both ways, the efficiency of the former is higher than that of the latter, mainly because the former actually puts both the key and the value into the iterator, and only executes once to find all the key-value pairs. and the latter equivalent to execute two times, the first time the key and value are deposited into the iterator, the second time from the HashMap
Removing the value corresponding to key is equivalent to traversing the map again.
Is the first kind of good, simple ...
How to traverse HashMap in Java