How to Map Mapping using an iterator?

Iterator type only for the collection of data, the map types must be converted to the type of collection you can use an iterator to acquire elements.

            1. In the map, although not directly instantiated iterator, but it provides a set of map keySet () method and the value () method, can be output to a set of keys by first, may be set or set list set.

            2. set of keys can be directly instantiated iterator.

            3. When performing the iterative operation, can get () method, to identify each key value corresponding to the output.

1       Map mm=new HashMap();
2         Set keySet = mm.keySet();
3         Iterator iterator=keySet.iterator();
4         while(iterator.hasNext()) {
5             Object key = iterator.next();
6             Object value = mm.get(key);
7             System.out.println(key+"   "+value);
8         }
. 1 HashSet set EnumMap.entrySet = (); // convert map type data set into a set of types. 
2 ITER set.iterator = (); // get the iterator set.
1  // the value is converted to type Map collection type to use iterator   
2 Collection <STUDENT2> = Collection treeMap.values ();
 . 3    the Iterator <STUDENT2> collection.iterator Iterator = ();

keySet acquire only the set of key values,
values obtained value is set,
entrySet obtained is a set of key-value pairs.

 

 

Guess you like

Origin www.cnblogs.com/sunbr/p/11901429.html