Preface
recent contacts in the University Cloud Platform Project Map more, the use of the map is not very familiar with, so this will Several methods of map learn again.
Map and Collection
mentionedMapThe collection interface cannot be mentionedCollectionCollection Interface,Mapand theCollectionare both collection interfaces,Collectioncontains what we often use.Listand theSetsub-interface;Mapis withCollectiona position at a lateral rank;Collectionis a set of objects stored in theMapstored is a key-value pair (Key/value).
Map
in the Map object, Key is unique and non-repeatable. null can also be a key, but such a key can only have one, but one or more the value corresponding to key is null.
When we want to determine if there is a key in the map , we can use the method ContainsKey() to determine if there is any value in the method containsvalue() to judge; The code is as follows:
<pre name= "code" class= "java" >public static void Main (string[] args) {map< Serializable, Serializable > Map = New hashmap< Serializable, Serializable > (), Map.put (Null,null), Map.put ("A", "1"), Map.put ("B", "2"), Map.put ("C" , "3"), if (Map.containskey ("a")) {System.out.println ("key=ture"), if (Map.containsvalue ("1")) {System.out.println (" Value=ture ");}}}
the execution results are:
Key=ture value=ture
Map provides a number of common methods to take out the data in the map , using more examples such as : EntrySet () method, The return value of EntrySet () is a set collection of type Map.entry. Map.entry is an internal interface to the map declaration, which is generic and defined as entry<k,v>. It represents an entity (a Key-value pair) in a map. The interface has Getkey (), the GetValue method, the code is as follows:
public static void Main (string[] args) {map< Serializable, Serializable > Map = new hashmap< Serializable, Serial Izable > () map.put (null,null) map.put ("A", "1"), Map.put ("B", "2"), Map.put ("C", "3"); Set<entry<serializable, serializable>> entryset= map.entryset (); System.out.println ("entryset=" +entryset); for (Entry Key:entryset) {System.out.println ("key.getkey=" +key.getkey () + "Key.getvalue () =" + Key.getvalue ());}}
The results of the implementation are as follows:
EntrySet = [Null=null, A=1, b=2, c=3]
Key.getkey=null Key.getvalue () =null
Key.getkey=a Key.getvalue () =1
Key.getkey=b Key.getvalue () =2
Key.getkey=c Key.getvalue () =3
The next thing to say is the KeySet method, the KeySet () method return value is a collection of key values in the map, and can then be traversed by a get (key) to get the value values. The code is as follows:
public static void Main (string[] args) {map< Serializable, Serializable > Map = new hashmap< Serializable, Serial Izable > () map.put (null,null) map.put ("A", "1"), Map.put ("B", "2"), Map.put ("C", "3"); Set keyset= Map.keyset (); System.out.println ("keyset=" +keyset); for (Iterator Iterator = Keyset.iterator (); Iterator.hasnext ();) {Object key = (object) iterator.next (); Object value = Map.get (key); System.out.println ("key =" +key+ "value=" +value);}}
The results of the implementation are as follows:
Keyset=[null, A, b,c]
Key = null Value=null
Key = a value=1
Key = b value=2
Key = C value=3
Finally, the map also has a values () method,the Values() method return value is a collection of value values in the map, which can be removed by traversing value, the code is as follows:
public static void Main (string[] args) {map<serializable, serializable> Map = new hashmap<serializable, Serializ Able> (); Map.put (null, NULL), Map.put ("A", "1"), Map.put ("B", "2"), Map.put ("C", "3"); Collection C = map.values (); System.out.println ("map.values () =" + map.values ()); for (Iterator Iterator = C.iterator (); Iterator.hasnext ();) {Object value = (object) iterator.next (); System.out.println ("value=" +value);}
The code execution results are as follows:
Map.values () =[null,1, 2, 3]
Value=null
Value=1
value=2
Value=3
Summary
This paper mainly introduces the use of the EntrySet () method in the Map collection and the keyset () and value () methods, the first two of which are the mappings between key and value , and only the last values are taken out of the set so that there is no key, and there is no corresponding mapping relationship.
Java Learning--map