The behavior of a tree map is well-defined even if its Operation, but a sorted map performs all key comparisons using itsĬompareTo (or compare) method, so two keys that areĭeemed equal by this method are, from the standpoint of the sorted map,Įqual. The Map interface is defined in terms of the equals Precise definition of consistent with equals.) This is so because ![]() The Comparable interface or Comparator interface for a The sorted map is to correctly implement the Map interface. Note that the ordering maintained by a sorted map (whether or not anĮxplicit comparator is provided) must be consistent with equals if Offending method or constructor invocation to throw a Attempts to violate this restriction will cause the Such keys must be mutually comparable: k1.compareTo(k2) (orĬpare(k1, k2)) must not throw aĬlassCastException for any elements k1 and k2 in Interface (or be accepted by the specified comparator). (This interface is the map analogue ofĪll keys inserted into a sorted map must implement the Comparable ![]() Several additional operations are provided Map's collection views (returned by the entrySet, keySetĪnd values methods). This order is reflected when iterating over the sorted Sorted according to the natural ordering of its keys (see theĬomparable interface), or by a comparator provided at sorted mapĬreation time. Interface SortedMap All Superinterfaces: Map All Known Implementing Classes: TreeMapĪ map that further guarantees that it will be in ascending key order, put ( "Pakistan", 92 ) // convert `HashMap` to `TreeMap` Map sorted = new TreeMap (codes ) // print tree elementsĪs you can see above, the keys (countries' names) are sorted in a natural order.Java 2 Platform SE v1.3: Interface SortedMap All you need to do is create a TreeMap object, and push all the existing data from the HashMap into it: // create a map Map codes = new HashMap ( ) Ĭodes. It works perfect for sorting a Map by keys. The TreeMap class stores the keys in natural sorted order. Entry :: getValue, (e1, e2 ) -> e1, LinkedHashMap :: new ) ) Using TreeMap Here is the output that shows the Map is sorted by values: United States = 1 France = 33 Germany = 49 China = 86 Pakistan = 92Īnd if you want to sort the Map by values in descending order, just do the following: // sort the map by values in reversed order Map sorted = codes. Entry :: getValue, (e1, e2 ) -> e1, LinkedHashMap :: new ) ) // print map elements put ( "Pakistan", 92 ) // sort the map by values Map sorted = codes. Off course, you can sort a Map by its values too using the Stream API: // create a map Map codes = new HashMap ( ) Ĭodes. Entry :: getValue, (e1, e2 ) -> e1, LinkedHashMap :: new ) ) Sort By Values If you want to sort the Map by keys in reserve order, you only need to change the comparing order to reverse like below: // sort the map by keys in reversed order Map sorted = codes. If we convert the sorted stream back to the HashMap, we might lose the elements ordering. ![]() The HashMap class does not provide any ordering guarantee. By default, Collectors.toMap() returns a HashMap. Notice the use of LinkedHashMap to store the sorted results to keep the order. The above program will print the following on the console: China=86 put ( "Pakistan", 92 ) // sort the map by keys Map sorted = codes. Here is an example that sorts the Map by keys using Java 8 streams: // create a map Map codes = new HashMap ( ) Ĭodes. The sorted() method takes a Comparator as a parameter, making it possible to sort the map by any type of value. Collect the entries and return them as a LinkedHashMap (keep the sorted order).Sort it by using Streams' sorted() method.Using Java 8 Streams, we can sort a map both by keys and by values. In Java, there are multiple ways to sort a Map, but we will focus on Java 8 Streams which is quite an elegant way of achieving this. This is probably one of the most frequently asked questions in Java interviews. In this short article, you will learn how to sort a Map in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |