Package Com.wzy.list;import Java.util.hashmap;import java.util.hashtable;import java.util.iterator;import Java.util.map;import java.util.map.entry;import java.util.set;import Java.util.TreeMap; Public classMaptest { Public Static voidMain (string[] args) {Map<String,Object> HashMap =NewHashmap<string,object>(); Hashmap.put ("AA","AAA"); Hashmap.put ("BB",222); Hashmap.put ("NULL","Kong"); Hashmap.put (NULL,NULL); Hashmap.put ("cc",""); Hashmap.put ("","Test"); System. out. println (HashMap.Get(NULL));//NULLSystem. out. println (HashMap.Get("NULL"));//KongSystem. out. println (HashMap.Get("Key"));//key value does not exist return nullSystem. out. println (HashMap.Get(""));//Testset<string> keys = Hashmap.keyset ();//get all the keysIterator key =Keys.iterator (); Key.foreachremaining ((x)->{system. out. println (x);}); System. out. println ("----------------"); Set<entry<string, object>> values =Hashmap.entryset (); Values.stream (). ForEach ((x)->{system. out. println (x);});//Sample Collection ObjectsValues.stream (). ForEach ((x)->{system. out. println (X.getkey ());});//the key of the collection objectValues.stream (). ForEach ((x)->{system. out. println (X.getvalue ());});//the value of the sample collection ObjectSystem. out. println ("-------------"); /** * HASH (hash) output is out of order; The tree is ordered * * HASHMAP,HASHTABLE,TREEMAPMAP subclass * Three uses similar * However, Hashtable does not allow the key and value to be null * hashtable is thread safe, because there are synchronized on the method * * TreeMap allow value to be null, but the key is not allowed to be null, because To sort by key, * classes like String,integer have already covered the comparable<string> and Hashcode,equals methods * so you can sort directly or find operations, * If it is a custom object to sort or find, to overwrite the comparable<string> and Hashcode,equals methods * * such as key to a custom object, the search must be implemented Hashcod E,equals method * Sort words must be implemented comparable<string> interface **/Map<string, object> hashtable =NewHashtable<string, object>(); //table.put (null, NULL); Key and value are not nullableSystem. out. println ("----TREEMAP----"); Map<Integer,String> TreeMap =NewTreemap<integer,string>(); //treemap.put (NULL, "2222");//key cannot be emptyTreemap.put (2,"2222"); Treemap.put (1,"1111"); Treemap.put (6,"6666"); Treemap.put (4,"4444"); System. out. println (TreeMap);//{1=1111, 2=2222, 4=4444, 6=6666} auto Sort }}
Hashmap,hashtable,treemapmap