Guide to Sorting by HashMap Value in Java: The Most Effective Methods
🚀 What Will You Learn in This Guide?
In this article, you will learn how to sort by HashMap value in Java**.
You will see how you can sort values in different data types (String, Integer, special classes) and
You will apply step by step how to get efficient results with modern methods (Comparator, Stream API).
🧠 Summary of the Topic
Main Technical Topic: Sorting by HashMap values.
Solved Problem: HashMap structure is not sequential. These methods are used when regular data output by value is required.
Basic Steps:
- Transferring values into an
List - Sort the list by
Collections.sort()or customComparator - Creating a new
LinkedHashMapwith sequential data
🔍 Sorting by HashMap Value Step by Step
HashMap in Java holds data in key-value structure but does not provide a natural ordering.
That's why several different methods are applied to sort data by values.
1️⃣ Sorting String Values
In this example, the values in the HashMap are of type String.
Step 1: Import all values into an ArrayList.
Step 2: Sort alphabetically using the Collections.sort() method.
Step 3: Add sequential values by creating a new LinkedHashMap.
// String tipindeki listeyi özel karşılaştırıcıyla alfabetik sıralar
Collections.sort(list, new Comparator<String>() {
public int compare(String str, String str1) {
return str.compareTo(str1);
}
});
Full application example:
package ornek.com.veri;
import java.util.*;
public class SiralamaOrnegi {
public static void main(String[] args) {
HashMap<String, String> harita = new HashMap<>();
LinkedHashMap<String, String> siraliHarita = new LinkedHashMap<>();
ArrayList<String> liste = new ArrayList<>();
harita.put("tr1-node01", "Baskent");
harita.put("tr1-node02", "Ankara");
harita.put("tr1-node03", "Denizli");
harita.put("tr1-node04", "Trabzon");
harita.put("tr1-node05", "Istanbul");
for (Map.Entry<String, String> girdi : harita.entrySet()) {
liste.add(girdi.getValue());
}
Collections.sort(liste, new Comparator<String>() {
public int compare(String str, String str1) {
return str.compareTo(str1);
}
});
for (String str : liste) {
for (Map.Entry<String, String> girdi : harita.entrySet()) {
if (girdi.getValue().equals(str)) {
siraliHarita.put(girdi.getKey(), str);
}
}
}
System.out.println(siraliHarita);
}
}
Output:
{tr1-node02=Ankara, tr1-node01=Baskent, tr1-node03=Denizli, tr1-node05=Istanbul, tr1-node04=Trabzon}
2️⃣ Sorting Integer Values
There is no need for a special comparator for integer values. Collections.sort() automates natural sorting.
// Integer değerleri küçükten büyüğe sıralar
Collections.sort(liste);
Afterwards, the result is obtained by creating LinkedHashMap in the same way.
3️⃣ Sorting Custom Objects
For custom classes (example: User) you can use Stream API + Comparator as a modern solution.
// Kullanici sınıfı
package ornek.com.veri;
public class Kullanici {
String ad;
String soyad;
Kullanici(String a, String b){
ad = a;
soyad = b;
}
public String getAd() {
return ad;
}
}
// Kullanıcıları 'ad' alanına göre sıralar
Comparator<Kullanici> adaGore = (Kullanici o1, Kullanici o2) ->
o1.getAd().compareTo(o2.getAd());
Direct sorting with Stream API:
HashMap<Integer, Kullanici> hmap = new HashMap<>();
hmap.put(9, new Kullanici("Ayse", "Yilmaz"));
hmap.put(1, new Kullanici("Burak", "Kaya"));
hmap.put(6, new Kullanici("Cem", "Demir"));
LinkedHashMap<Integer, Kullanici> sirali = hmap.entrySet().stream()
.sorted(Map.Entry.<Integer, Kullanici>comparingByValue(adaGore))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
Output:
9: Ayse Yilmaz
1: Burak Kaya
6: Cem Demir
💬 Frequently Asked Questions (FAQ)
- Why do we use LinkedHashMap?
HashMap does not preserve ordering. LinkedHashMap preserves the insertion order of sorted data.
- Does the key order change?
Yes, a new row is created based on the values. The original key sequence changes.
- How to sort in reverse (from largest to smallest)?
Collections.sort(list, Collections.reverseOrder()); or obj2.compareTo(obj1) can be used.
- Which Java versions is this method compatible with?
The classic Collections.sort() works in all versions. Stream API requires Java 8+.
- What to do if key and value are of the same type?
For string type values, sorting is done with the Comparator.
🧩 Result
This guide showed different ways to sort HashMap values in Java. Collections.sort() is the most effective solution for simple data types, and the combination of Comparator + Stream API is the most effective solution for special objects.
You can immediately try these methods in your own projects or applications on the GenixNode platform. ☁️

