![]() ![]() ![]() TreeMap in Java does not allow null keys (like Map) and thus a NullPointerException is thrown. The class implements Map interfaces including NavigableMap, SortedMap, and extends AbstractMap class. You can see that the sorted() method takes Comparator as an argument, making it possible to sort a map with any kind of value. This class is a member of the Java Collections Framework. The complete program for sorting in reverse order is: public class SortMapByValueExample If you want to sort a map in reverse order, then you just need to specify comparing the value as reversed order as: final Map sortedB圜ount = wordCounts.entrySet() It doesn't modify the original data wordCounts, making it more thread safe.Here, I am using LinkedHashMap to store the sorted result to preserve the order of the elements in the resulting map. A subtype of map that provides iteration over keys conforming to the natural sorting order, backed by a tree map. collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)) Now, if I have to sort this map with value in ascending order, then it would be simplest and readable as below: final Map sortedB圜ount = wordCounts.entrySet() With streams introduced after Java 8, this can be achieved in an elegant and functional way.įor example, let us consider I map a word and its corresponding counts in a particular document as shown below: final Map wordCounts = new HashMap() 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.This has been one of the most frequently asked questions in Java interviews. 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. A Map, in short, is: An object that maps keys to values. If we convert the sorted stream back to the HashMap, we might lose the elements ordering. You are required to implement the SortedMap interface in the java.util package using the AVL-g tree. 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 |