Java in the way traversal of 4 map

About java traversal map exactly which of four ways, see below explain it.

One way: This is the most common and in most cases is the most desirable way to navigate. Used when keys are needed.

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

 

Method two: traversing keys or values ​​in the for-each loop.

If you only need to key or value in the map, you can achieve traversed by keySet or values, rather than entrySet.

Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
//遍历map中的键
for (Integer key : map.keySet()) {
  System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
  System.out.println("Value = " + value);
} 

The ratio entrySet traversal method in slightly better performance (10% faster), and the code cleaner.

Method three use Iterator traversal

Using generics :

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
  Map.Entry<Integer, Integer> entry = entries.next();
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

 

Not using generics:

Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
  Map.Entry entry = (Map.Entry) entries.next();
  Integer key = (Integer)entry.getKey();
  Integer value = (Integer)entry.getValue();
  System.out.println("Key = " + key + ", Value = " + value); 
}

 

You can also apply the same method on keySet and values.

This manner has its advantages appear where redundant. First of all, in the old version of java this is the only way to traverse the map. Another benefit is that you can call iterator.remove while traversing () to delete the entries, the other two methods can not. The javadoc description, if the attempt to use this method for-each traversal, the result is unpredictable.

From a performance perspective, similar to the method for-each traversal (i.e., Method II) performance.

Method IV, traversed to find the key values ​​(low efficiency)

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
  Integer value = map.get(key);
  System.out.println("Key = " + key + ", Value = " + value);

 

As an alternative method, the code looks more clean; but it is actually quite slow and inefficient. Since the key value is time-consuming operation (compared with a method, different implementations of the method Map slow 20% to 200%). If you installed the FindBugs, it will make a check and warn you about what is inefficient to traverse. So try to avoid using.

to sum up

If only the required key (Keys) or values ​​(values) using Method II. If the language version you are using less than java 5, or intend to delete entries in the traversal, you must use the method III. Otherwise, use a (key should be).

Guess you like

Origin www.cnblogs.com/tank073/p/11331392.html