Sorting HashMap by value
In this post, We are going to discuss
one of the most common Java Interview Program to sort HashMap by value.
HashMap store the data in the form of key and value pair where the key can not be duplicate.
Steps:
Code:
HashMapSort.java
package com.javainstance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author javainstance
*
*/
public class HashMapSort {
public static void main(String[] args) {
HashMap<String, Integer> unsortedMap = new HashMap<>();
unsortedMap.put("Sachin", 3);
unsortedMap.put("Rahul", 7);
unsortedMap.put("Sourav", 11);
unsortedMap.put("Laxman", 5);
unsortedMap.put("Virender", 2);
unsortedMap.put("Zaheer", 9);
printSortedHashMap(unsortedMap);
}
/**
* @param inputMap
*/
public static void printSortedHashMap(HashMap<String, Integer> inputMap) {
Set<Entry<String, Integer>> mapEntries = inputMap.entrySet();
List<Entry<String, Integer>> entriesList = new ArrayList<>(mapEntries);
MapSortComparator mapSort = new MapSortComparator();
Collections.sort(entriesList, mapSort);
for (Entry<String, Integer> entry : entriesList) {
System.out.println("Key--" + entry.getKey() + "--Value--" + entry.getValue());
}
}
}
/**
* Comparator class to sort on value
*
* @author javainstance
*
*/
class MapSortComparator implements Comparator<Entry<String, Integer>> {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue() - o2.getValue();
}
}
Output:
Key--Viraj--Value--2
Key--Sarc--Value--3
Key--Lavan--Value--5
Key--Rajesh--Value--7
Key--Zaik--Value--9
Key--Sourav--Value--11
Give you suggestion in comments.If you like the post Share with your friends on social network.
one of the most common Java Interview Program to sort HashMap by value.
HashMap store the data in the form of key and value pair where the key can not be duplicate.
Steps:
- Converted the HashMap into Set storing Entry.
- Created the comparator to sort the entry based on value.
- Converted the Set to List.
- Collections.sort() is used to sort the list.
Code:
HashMapSort.java
package com.javainstance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author javainstance
*
*/
public class HashMapSort {
public static void main(String[] args) {
HashMap<String, Integer> unsortedMap = new HashMap<>();
unsortedMap.put("Sachin", 3);
unsortedMap.put("Rahul", 7);
unsortedMap.put("Sourav", 11);
unsortedMap.put("Laxman", 5);
unsortedMap.put("Virender", 2);
unsortedMap.put("Zaheer", 9);
printSortedHashMap(unsortedMap);
}
/**
* @param inputMap
*/
public static void printSortedHashMap(HashMap<String, Integer> inputMap) {
Set<Entry<String, Integer>> mapEntries = inputMap.entrySet();
List<Entry<String, Integer>> entriesList = new ArrayList<>(mapEntries);
MapSortComparator mapSort = new MapSortComparator();
Collections.sort(entriesList, mapSort);
for (Entry<String, Integer> entry : entriesList) {
System.out.println("Key--" + entry.getKey() + "--Value--" + entry.getValue());
}
}
}
/**
* Comparator class to sort on value
*
* @author javainstance
*
*/
class MapSortComparator implements Comparator<Entry<String, Integer>> {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue() - o2.getValue();
}
}
Output:
Key--Viraj--Value--2
Key--Sarc--Value--3
Key--Lavan--Value--5
Key--Rajesh--Value--7
Key--Zaik--Value--9
Key--Sourav--Value--11
Give you suggestion in comments.If you like the post Share with your friends on social network.
6 Different ways to iterate through HashMap
Sorting HashMap by value
Reviewed by JavaInstance
on
11:31:00 AM
Rating:
No comments: