How to Sort HashMap by Value10 Sept 2024 | 3 min read In Java, sorting HashMap by values is complicated because there is no direct method available. If we need to sort the HashMap by values, we should create a Comparator. It compares two elements based on the values. After that get the Set of elements from the Map and convert Set into the List. Use the Collections.sort(List) method to sort the list of elements by values by passing customized comparator. Now create a new LinkedHashMap and copy the sorted elements into that. Since LinkedHashMap guarantees the insertion order of mappings. We get a HashMap whose values are in sorted order. Java Collections.sort() methodJava collections class provides a method to sort all list implementations such as LinkedList and ArrayList. There are two overloaded sort methods():
Syntax The method does not return any value. It throws the following exceptions: ClassCastException: If the list contains elements that are not mutually comparable. UnsupportedOperationException: If the specified list's list-iterator does not support the set operation. The difference between sorting HashMap by Keys and Values is that it can have duplicate values but not duplicate Keys. We cannot use TreeMap to sort values because TreeMap sorts elements by Keys. Example of sort HashMap by Value In the following example, we have sorted the map in ascending and descending order. Output: Before sorting: Company Price Dell 32000 HP 20000 Lenovo 19990 Samsung 36546 Apple 65000 Asus 21478 Sorting values in ascending order: Company Price Lenovo 19990 HP 20000 Asus 21478 Dell 32000 Samsung 36546 MAC Book 65000 Sorting values in descending order: Company Price MAC Book 65000 Samsung 36546 Dell 32000 Asus 21478 HP 20000 Lenovo 19990 Next TopicJava Tutorial |
The minimum cost path problem in Java is one the most prominent problems that have been asked in the interview. In this problem, a matrix is provided (costMatrix[][]), which represents the cost of each of the cells present in the costMatrix[][]. The task is to go...
11 min read
Australian National University explores functional programming as a declaration programming paradigm that processes mathematical functions as evaluations while rejecting state modifications or mutable data changes. The adoption of functional programming in Java occurred with Java 8 by implementing lambda expressions alongside functional interfaces and the Stream API...
5 min read
The java.io.ObjectInputStream class is used to deserialize objects and primitive data iously serialized using the ObjectOutputStream. It allows the reconstruction of object graphs and ensures compatibility between the serialized object's class and the current JVM (Java Virtual Machine) class definitions. ObjectOutputStream and ObjectInputStream collaborate to save and...
22 min read
We can check palindrome string by reversing string and checking whether it is equal to original string or not. Let's see the example code to check palindrome string in java. File: PalindromeChecker.java public class PalindromeChecker { public static boolean isPalindrome(String str){ StringBuilder sb=new StringBuilder(str); sb.reverse(); String rev=sb.toString(); if(str.equals(rev)){ return true; }else{ return false; } } } File: TestPalindrome.java public class...
1 min read
When we write a program in any programming language it requires converting that code in the machine-understandable form because the machine only understands the binary language. According to the programming languages, compiler differs. The compiler is a program that converts the high-level language to machine...
3 min read
In this section, we will learn what is a frugal number and also create Java programs to check if the given number is a frugal number or not. The frugal number program is frequently asked in Java coding interviews and academics. Frugal Number A frugal number is a...
3 min read
In the domain of computing and algorithm designing, the median of a dataset is one common type of problem that people face. The median which gives the midpoint of a distribution of values indexed in ascending order of values is another measure of central tendency. However, when...
4 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
6 min read
Binary numbers play a vital role computer science. It shows information using only the numbers 0 and 1. Determining whether a binary number is divisible by 3 or not is an understanding of modular arithmetic, numbers and multipliers is required. A binary number can be analyzed to...
5 min read
In Java, an array is a data structure that allows you to store multiple values of the same type in a single variable. It provides a convenient way to work with collections of data. The Arrays.toString() and Arrays.deepToString() methods are then used to produce the values...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India