Map four ways to get the key and value values and sort the elements in the map

Source: Internet
Author: User

There are four ways to get the value of the map, the four methods are divided into two categories, one is to call the Map.keyset () method to get the value of key and values, the other is the Map.entryset () method to take the value, the difference is that, The former is the first to obtain all the key collection, when you need to query the value of values, you need to use key to query value, the latter will directly take key and value of the key value directly out of the query only once, for that performance better, I think it is still used Map.entryset () A little better, see Map.keyset () and map for details. EntrySet (), the next step is to introduce the four traversal methods and the elements in the map to compare size and sort by key or value:

 PackageCom.sort;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Map.Entry; Public classTest { Public Static voidMain (string[] args) {Map<String,String> map =NewHashmap<string,string>(); Map.put ("B", "4"); Map.put ("A", "5"); Map.put ("C", "3"); Map.put ("D", "5"); //through the Map.keyset () method//method One: By getting the value of key, and then get value;        /*For (String Key:map.keySet ()) {String value = Map.get (key);        System.out.println (key+ "" +value); }*/        //use an iterator to get the key;        /*iterator<string> iter = Map.keyset (). Iterator ();            while (Iter.hasnext ()) {String key=iter.next ();            String value = Map.get (key);        System.out.println (key+ "" +value); }*/        //through the Map.entryset () method//method One: Loop the map for each pair of key pairs, and then get the key and value        /*for (entry<string, string> vo:map.entrySet ()) {Vo.getkey ();            Vo.getvalue ();        System.out.println (Vo.getkey () + "" +vo.getvalue ()); }*/                /*//Using iterators, get key iterator<entry<string,string>> iter = Map.entryset (). Iterator ();            while (Iter.hasnext ()) {entry<string,string> Entry = Iter.next ();            String key = Entry.getkey ();            String value = Entry.getvalue ();        System.out.println (key+ "" +value); }*/                //Convert map<string,string> to arrylist, but the elements in list are entry<string,string>list<entry<string,string>> list =NewArraylist<map.entry<string,string>>(Map.entryset ()); Collections.sort (list,NewComparator<entry<string,string>>() {@Override Public intCompare (Entry<string, string>O1, Entry<string, string>O2) {                intFlag =O1.getvalue (). CompareTo (O2.getvalue ()); if(flag==0){                    returnO1.getkey (). CompareTo (O2.getkey ()); }                returnFlag;        }        }); //iterate through the list to get the sorted elements in the map         for(Entry<string, string>en:list) {System.out.println (En.getkey ()+" "+En.getvalue ()); }            } }

Map four ways to get the key and value values and sort the elements in the map (go)

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.