Hashmap,hashtable,treemapmap

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.