- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting a HashMap according to values in Java
As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it.
Now in order to sort a hash map according to the values mapped to its corresponding keys we first need to get all values of map considering that hash map has unique values only.Now put all the values in a list and sort this list with the comparator or comparable interface of Java.
As we get sorted list of unique values now get corresponding keys from the map and put the value key pair in new tree map so that again the insertion order is maintained according to the values.After insertion we would transverse same tree map which is sorted and is our resultant sorted map.
Example
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; public class HashMapSortByValue { public static void main(String[] args) { HashMap<String, String> hMap = new HashMap<>(); LinkedHashMap<String, String> sortedMap = new LinkedHashMap<>(); ArrayList<String> list = new ArrayList<>(); hMap.put("5", "Akshay"); hMap.put("8", "Veer"); hMap.put("3", "Guang"); hMap.put("7", "Bakshi"); hMap.put("2", "TomTom"); hMap.put("10", "Chang"); hMap.put("1", "Sandy"); for (Map.Entry<String, String> entry : hMap.entrySet()) { list.add(entry.getValue()); } Collections.sort(list, new Comparator<String>() { public int compare(String str, String str1) { return (str).compareTo(str1); } }); for (String str : list) { for (Entry<String, String> entry : hMap.entrySet()) { if (entry.getValue().equals(str)) { sortedMap.put(entry.getKey(), str); } } } System.out.println(sortedMap); } }
Output
{5 = Akshay, 7 = Bakshi, 10 = Chang, 3 = Guang, 1 = Sandy, 2 = TomTom, 8 = Veer}
- Related Articles
- Sorting a HashMap according to keys in Java
- Sorting a HashMap according to keys in C#
- Extract values from HashMap in Java
- Sorting according to weights of numbers in JavaScript
- Sorting objects according to days name JavaScript
- Remove all values from HashMap in Java
- Sorting numbers according to the digit root JavaScript
- Iterate through the values of HashMap in Java
- Sorting array according to increasing frequency of elements in JavaScript
- Java Program to retrieve the set of all values in HashMap
- Sorting according to number of 1s in binary representation using JavaScript
- Sorting contents of a string that holds integer values in Java
- Create a HashMap in Java
- Java Program to retrieve the set of all keys and values in HashMap
- Sorting an array according to another array using pair in STL in C++

Advertisements