Four traversal methods of map

Map is a very commonly used data structure in java, but map is different from set and list, both inherit from the Collection interface.

So map does not implement the Iterator method of Collection, and it does not have its own iterator to traverse elements.


construct a map

[java]  view plain copy  
  1. Map<String, String> map = new HashMap<String, String>();    
  2. map.put("001""hello");    
  3. map.put("002""world");    
  4. map.put("003""main");    
If you want to traverse this map, what are the usual ways of traversing the map?


Here we introduce four commonly used methods: keySet collection iteration , entrySet collection iteration , keySet collection for-each loop , entrySet collection for-each loop .


Method 1 keySet collection iteration

[java]  view plain copy  
  1. // method1  
  2. Set<String> keySet = map.keySet();  
  3. Iterator<String> it = keySet.iterator();  
  4. while (it.hasNext()) {  
  5.     String key = it.next();  
  6.     System.out.println(key + "=" + map.get(key));  
  7. }  

Method 2 entrySet collection iteration

[java]  view plain copy  
  1. // method2  
  2. Set<Map.Entry<String, String>> entrySet = map.entrySet();  
  3. Iterator<Map.Entry<String, String>> meIt = entrySet.iterator();  
  4. while (meIt.hasNext()) {  
  5.     Entry<String, String> entry = meIt.next();  
  6.     System.out.println(entry.getKey() + "=" + entry.getValue());  
  7. }  

Method 3 keySet collection for-each loop

[java]  view plain copy  
  1. // method3  
  2. for (String key : map.keySet()) {  
  3.     System.out.println(key + "=" + map.get(key));  
  4. }  

Method 4 entrySet collection for-each loop

[java]  view plain copy  
  1. // method4  
  2. for (Map.Entry<String, String> entry : map.entrySet()) {  
  3.     System.out.println(entry.getKey() + "=" + entry.getValue());  
  4. }  


 
 

四种方式中,method1 和 method2 是通过迭代器来显示完成的,method3 和 method4 是通过for-each来隐式的通过迭代器来完成的。

同时 method1 和 method3 是通过key的集合来完成的,method2 和 method4 是通过entry 的集合来完成的。

方法1 和方法2 的区别

一个是获取keySet ,一个是获取entrySet

推荐使用entrySet 的方式去获取,查看map通过key获取value的方法

注意:大量数据时使用该entrySet方式效率更高

[java]  view plain  copy
  1. public V get(Object key) {  
  2.     if (key == null)  
  3.         return getForNullKey();  
  4.     Entry<K,V> entry = getEntry(key);  
  5.   
  6.     returnnull == entry ? null : entry.getValue();   
  7. }  
It is also to first obtain the entry corresponding to the key, and then obtain the value value. Therefore, it is recommended to use the entrySet method, and then traverse the entry set to traverse the map.

There is also a method values() in the map interface. Since only all the value values ​​can be obtained, but the key value cannot be obtained, it is not considered to traverse the map here, only the value of the map can be counted.


Reprinted from: https://blog.csdn.net/magi1201/article/details/41968495

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572001&siteId=291194637