🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Sorting a LinkedHashMap by its values is a common requirement in Java programming. Unlike sorting by keys, sorting by values adds an additional layer of complexity. In this post, we'll explore different ways to achieve this, using practical examples.
Method 1: Using Stream API
The Stream API in Java 8 and later versions is a flexible way to sort a LinkedHashMap by its values. Here's how you can do it:
import java.util.*;
import java.util.stream.Collectors;
public class FruitSorter {
// Method to sort LinkedHashMap by values
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(LinkedHashMap<K, V> map) {
return map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
}
public static void main(String[] args) {
LinkedHashMap<String, Integer> fruits = new LinkedHashMap<>();
fruits.put("Apple", 3);
fruits.put("Orange", 2);
fruits.put("Banana", 4);
fruits.put("Grapes", 1);
fruits.put("Pineapple", 5);
System.out.println("Original LinkedHashMap: " + fruits);
System.out.println("Sorted LinkedHashMap by values: " + sortByValue(fruits));
}
}
Output:
Original LinkedHashMap: {Apple=3, Orange=2, Banana=4, Grapes=1, Pineapple=5}
Sorted LinkedHashMap by values: {Grapes=1, Orange=2, Apple=3, Banana=4, Pineapple=5}
Method 2: Using Collections.sort with a Custom Comparator
import java.util.*;
import java.util.stream.Collectors;
public class FruitSorter {
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueUsingList(LinkedHashMap<K, V> map) {
List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet());
list.sort(Map.Entry.comparingByValue());
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public static void main(String[] args) {
LinkedHashMap<String, Integer> fruits = new LinkedHashMap<>();
fruits.put("Apple", 3);
fruits.put("Orange", 2);
fruits.put("Banana", 4);
fruits.put("Grapes", 1);
fruits.put("Pineapple", 5);
System.out.println("Original LinkedHashMap: " + fruits);
System.out.println("Sorted LinkedHashMap by values using list: " + sortByValueUsingList(fruits));
}
}
Output:
Original LinkedHashMap: {Apple=3, Orange=2, Banana=4, Grapes=1, Pineapple=5}
Sorted LinkedHashMap by values using list: {Grapes=1, Orange=2, Apple=3, Banana=4, Pineapple=5}
Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment