Home » Java » Two extraction methods for Java map collection

Two extraction methods for Java map collection

Java map collection of two ways out, there is a friend in need of reference.

Two extraction methods for map collection:

1, Set, keySet: store all keys in the map into the Set collection because set has the iterator.

, so you can iterate to take all the keys, and then, according to the get method, get the value of each key,.

The removal principle of Map collection: turn the map collection into a set collection, and then take the.
out of the iterator

2, Set>, entrySet: the mapping in the map collection is stored in the set collection,

, and the data type of this relationship is: Map.Entry

Entry is actually a static internal interface Map in.

Why should be defined internally,
because only the Map collection has a key value pair will have the key value mapping.

The relationship belongs to an internal thing in the Map collection,.

, and the element in the direct access to the Map collection,.

import java.util.*;
Class MapDemo2
Public, static, void, main (String[], args)
Map, map = new, HashMap ();
Map.put ("02", "zhangsan2");
Map.put ("03", "zhangsan3");
Map.put ("01", "zhangsan1");
Map.put ("04", "zhangsan4");
Remove / / Map mapping in the collection. In the Set collection.
Set>, entrySet = map.entrySet ();
Iterator>, it = entrySet.iterator ();
While (it.hasNext ())
Map.Entry, me = it.next ();
String key = me.getKey ();
String value = me.getValue ();
System.out.println (key+: "+value");
Get map / / the first set of all key Set collection, keySet ();
Set keySet = map.keySet ();
Have / / Set set. It can obtain the iterator.
Iterator it = keySet.iterator ();
While (it.hasNext ())
String key = it.next ();
The get / / map set by the method of key can get the corresponding value.
String value = map.get (key);
System.out.println ("key:", "+key+", "value:", "+value");
* /
Map.Entry in fact, Entry is also an interface, which is an internal interface in the Map interface
Interface Map
Public static interface Entry
Public, abstract, Object, getKey ();
Public, abstract, Object, getValue ();
Class HashMap implements Map
Class Hahs implements Map.Entry
Public Object (getKey) {}
Public Object (getValue) {}

-- from "Bi 25 days >