The collection interface has been exposed before, and each time the object is saved is an object, but a pair of objects saved in the map is saved in the form of Key->value .
Defined:
Public Interface Map<k,v>
Map.entry interface.
Defined:
Public Static Interface Map.entry<k,v>
Common methods:
Map and Map.entry Relationship:
Common sub-categories:
Take HashMap as an example of how to do this:put () add content, get () to remove content .
The contents are removed by key. map.get ("key").
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =NewHashmap<string,string>() ; Map. put ("Mldn", "www.mldn.cn");//Add ContentMap.put("Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put("Mldnjava", "www.mldnjava.cn");//Add ContentStringVal=map.get ("mldn"); Take the value out by keySystem.out.println ("Out of the content is:" + Val); }};
Output Result:
The contents of the extract are: www.mldn.cn
You can also pass contains. The Xxx () method determines whether the specified key or values exist .
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =NewHashmap<string,string>() ; Map.put ("Mldn", "www.mldn.cn");//Add ContentMap.put ("Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put ("Mldnjava", "www.mldnjava.cn");//Add Content if( map.containskey("mldn")){//determine if key existsSystem.out.println ("Key to search exists!") ") ; }Else{System.out.println ("The search key does not exist!" ") ; } if(Map.containsvalue("www.mldn.cn")) {//determine if value existsSystem.out.println ("The value of the search exists!") ") ; }Else{System.out.println ("The value of the search does not exist!" ") ; } }};
If you want to output all the
Key, note that the values are not included, use the following method:
Set KeySet ()。
Examples are as follows:
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Iterator;ImportJava.util.Set; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type string map = new Hashmap<string,string>() ; Map.put ("Mldn", "www.mldn.cn");//Add ContentMap.put ("Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put ("Mldnjava", "www.mldnjava.cn");//Add Content set<string> keys =Map.keyset (); Get all the keys iterator<string> iter = Keys.iterator (); while(ITER. Hasnext()) {String str=ITER. Next () ; System.out.print (str+ ",") ; } }};
Output Result:
MLDN, Zhinangtuan, Mldnjava,
Now that you can get all the keys, you can get all the value. By methodvalues ().
Defined as follows:
Collection Values ()
Instance:
Packageclass set;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Iterator; Public classtest1{ Public Static voidMain (String args[]) { Map <String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =New HashMap<String,String>() ; Map.put ("Mldn", "www.mldn.cn");//Add ContentMap.put ("Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put ("Mldnjava", "www.mldnjava.cn");//Add Content collection<string> values = Map.values (); Get all the value iterator<string> iter = Values.iterator (); while(ITER. Hasnext()) {String str=ITER. Next () ; System.out.print (str+ ",") ; } }};
Output:
www.mldn.cn, www.zhinangtuan.net.cn, www.mldnjava.cn,
In the map there are alsoTreeMapof subclasses,Sort by Key。
Packageclass set;ImportJava.util.Map;ImportJava.util.Iterator;ImportJava.util.Set;ImportJava.util.TreeMap; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =NewTreemap<string,string>() ; Map. put ("A, Mldn", "www.mldn.cn");//Add ContentMap.put("C, Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put("B, Mldnjava", "www.mldnjava.cn");//Add Content set<string> keys = Map.keyset (); get all the key iterator<string> iter = keys.iterator () ; while(Iter.hasnext ()) {String str=Iter.next (); System.out.println (str+ "-+" +map.get (str)) ;//Remove content } }};
Output Result:
A, mldn-----www.zhinangtuan.net.cn
Will find that the above to get the map key all get, take out, one by one output, found that the output of the second and third sequence and the input map is not the same, changed,
This shows thattreemap the input content, sorted by key .
Using TreeMap, you can easily complete the sorting operation.
if the custom class wants to be a key, then it is necessary to implement the comparable interface, specifying the rules for comparison .
Weak reference class: Weakhashmap,system.gc ()
If you assume that some content in the map is not used for a long time, the previous practice is not automatically deleted, and if you want it to be deleted automatically, use a weak reference class. When the content is not needed, it is automatically deleted .
Packageclass set;ImportJava.util.Map;ImportJava.util.WeakHashMap; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =NewWeakhashmap<string,string>() ; Map.put (NewString ("Mldn"),NewString ("www.mldn.cn")) ; Map.put (NewString ("Zhinangtuan"),NewString ("www.zhinangtuan.net.cn")) ; Map.put (NewString ("Mldnjava"),NewString ("www.mldnjava.cn")) ; System.GC (); //Mandatory garbage collection operationsMap.put (NewString ("Lxh"),NewString ("Lixinghua")) ; SYSTEM.OUT.PRINTLN (map); }};
Output Result:
{Lxh=lixinghua}
the content before the Discovery GC () was deleted .
Map interface, HashMap class, TreeMap class, Weakhashmap.