In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the simple sorting methods for Java HashMap". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the simple sorting methods of Java HashMap?"
HashMap is stored in no order, but is implemented according to key's HashCode.
Key= mobile phone brand, value= price, here is an example to sort by name and by price.
Map phone=new HashMap (); phone.put ("Apple", 8899); phone.put ("SAMSUNG", 7000); phone.put ("Meizu", 2698); phone.put ("Xiaomi", 1800); System.out.println (phone)
Output HashMap directly to get an unordered Map (not sequential storage like Arraylist).
1. Sort by key
To sort the names, you first need to get the keySet of the keys in HashMap and convert them to an array, so that you can sort them with Arrays.sort ().
Set set=phone.keySet (); Object [] arr=set.toArray (); Arrays.sort (arr); for (Object key:arr) {System.out.println (key);}
Get the sorted key value
Finally, the corresponding value of the key can be obtained by using HashMap.get (key).
For (Object key:arr) {System.out.println (key+ ":" + phone.get (key));}
The resulting printed result
two。 Sort by value
To sort the prices, you first need to get the view (entrySet) containing the mapping relationship in the HashMap, as shown in the figure:
Convert entrySet to List, and then rewrite the comparator comparison. Either List.sort (comparator) or Collections.sort (list,comparator) can be used here
Convert to list
List list = new ArrayList (phone.entrySet ()); / / convert to list
Sort using list.sort ()
List.sort (new Comparator () {@ Override public int compare (Map.Entry o1, Map.Entry O2) {return o2.getValue () .compareTo (o1.getValue ());}})
Sort using Collections.sort ()
Collections.sort (list, new Comparator () {@ Override public int compare (Map.Entry o1, Map.Entry O2) {return o2.getValue () .compareTo (o1.getValue ());}})
Two ways of result output
/ / for loop for (int I = 0; I < list.size (); iTunes +) {System.out.println (list.get (I). GetKey () + ":" + list.get (I). GetValue ());} / / for-each loop for (Map.Entry mapping: list) {System.out.println (mapping.getKey () + ":" + mapping.getValue ());}
Ergodic printout
/ / for for (int I = 0; I < list.size (); iTunes +) {System.out.println (list.get (I). GetKey () + ":" + list.get (I). GetValue ());} System.out.println (); / / for-each for (Map.Entry mapping: list) {System.out.println (mapping.getKey () + ":" + mapping.getValue ());}
At this point, I believe you have a deeper understanding of "what are the simple sorting methods of Java HashMap?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.