In this article, we will discuss how to sort list of Objects on the basis of any one parameter/field using Java 8 Comparator’s static method comparing() which accepts Function functional interface
Comparator.comparing() is the overloaded method where 1st variant accepts only-one argument and 2nd variant accepts 2-arguments
Here, in this article we will discuss 1st variant which accepts Function functional interface and in the next article we will discuss about 2nd variant which accepts 2-arguments Function & Comparator
1. Comparator.comparing() method :
- This static method accepts a function that extracts a
Comparable
sort key from a type T - Returns a
Comparator<T>
that compares by that sort key - The returned comparator is serializable if the specified function is also serializable
- Method signature :-
- static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor)
- Where
- T is the type of element to be compared
- U is the type of the Comparable sort key
- keyExtractor is the function used to extract the Comparable sort key
- Exception :- Throws NullPointerException, if the argument is null
2. Comparator.comparing() examples :
- Product class is defined with 4 attributes namely id, name, quantity and their price
- Along with 4 attributes, parameterized constructor, getters/setters and toString() method is defined – removed for brevity
Product.java
package net.bench.resources.comparator.comparing;
public class Product {
// member variables
private int id;
private String name;
private long quantity;
private double price;
// 4-arg parameterized constructor
// getters and setters
// toString() method
}
2.1 Sort Product list in alphabetical order of its name
- A list contains Product information for 5 as per insertion-order
- Sorting :- we are going to sort the Product list according to alphabetial order of its name using Stream‘s sorted() method
SortProductListByName.java
package net.bench.resources.comparator.comparing;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class SortProductListByName {
// List of Products
private static List<Product> getProductList() {
return Arrays.asList(
new Product(101, "Wheat", 1089L, 36.89),
new Product(102, "Rice", 502L, 58.19),
new Product(103, "Lentils", 803L, 102.45),
new Product(104, "Oil", 208L, 164.75),
new Product(105, "Vegetables", 303L, 45.50)
);
}
public static void main(String[] args) {
// 1. get Product list
List<Product> products = getProductList();
// 1.1 print to console
System.out.println("Product list as per Insertion-order :-\n");
products.forEach(System.out::println); // iterating/printing
// 2. sorting Product list in alphabetical order by its Name
System.out.println("\n\nSorted Product list by alphabetical order of Name :-\n");
// 2.1 sorting/iterating/printing
products
.stream() // 1. get sequential stream
.sorted(Comparator.comparing(Product::getName)) // 2. alphabetical name
.forEach(System.out::println); // 3. iterate/printing
}
}
Output:
Product list as per Insertion-order :-
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Sorted Product list by alphabetical order of Name :-
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=101, name=Wheat, quantity=1089, price=36.89]
2.2 Sort Product list in ascending-order of Product Id
- A list contains Product information for 5 as per insertion-order
- Sorting :- we are going to sort the Product list in the ascending order of Product Id using Stream‘s sorted() method
SortProductListById.java
package net.bench.resources.comparator.comparing;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class SortProductListById {
// List of Products
private static List<Product> getProductList() {
return Arrays.asList(
new Product(102, "Rice", 502L, 58.19),
new Product(104, "Oil", 208L, 164.75),
new Product(103, "Lentils", 803L, 102.45),
new Product(105, "Vegetables", 303L, 45.50),
new Product(101, "Wheat", 1089L, 36.89)
);
}
public static void main(String[] args) {
// 1. get Product list
List<Product> products = getProductList();
// 1.1 print to console
System.out.println("Product list as per Insertion-order :-\n");
products.forEach(System.out::println); // iterating/printing
// 2. sorting Product list in ascending-order by its Id
System.out.println("\n\nSorted Product list in ascending-order of Id :-\n");
// 2.1 sorting/iterating/printing
products
.stream() // 1. get sequential stream
.sorted(Comparator.comparing(Product::getId)) // 2. Id sorting
.forEach(System.out::println); // 3. iterate/printing
}
}
Output:
Product list as per Insertion-order :-
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Sorted Product list in ascending-order of Id :-
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
2.3 Sort Product list in ascending-order of its quantity
- A list contains Product information for 5 as per insertion-order
- Sorting :- we are going to sort the Product list in the ascending order of its quantity using Collections.sort() method passing Comparator as 2nd argument and original list as 1st argument
SortProductListByQuantity.java
package net.bench.resources.comparator.comparing;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortProductListByQuantity {
// List of Products
private static List<Product> getProductList() {
return Arrays.asList(
new Product(101, "Wheat", 1089L, 36.89),
new Product(102, "Rice", 502L, 58.19),
new Product(103, "Lentils", 803L, 102.45),
new Product(104, "Oil", 208L, 164.75),
new Product(105, "Vegetables", 303L, 45.50)
);
}
public static void main(String[] args) {
// 1. get Product list
List<Product> productList = getProductList();
// 1.1 print to console
System.out.println("Product list as per Insertion-order :-\n");
productList.forEach(System.out::println); // iterating/printing
// 2. sorting Product list in ascending-order by its Quantity
System.out.println("\nSorted Product list in ascending-order of Quantity :-\n");
// 2.1 sorting using Collections.sort() & Comparator
Collections.sort(
productList, // original list
Comparator.comparing(Product::getQuantity) // Quantity sorting
);
// 2.2 iterate & printing
productList.forEach(System.out::println); // iterating/printing
}
}
Output:
Product list as per Insertion-order :-
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Sorted Product list in ascending-order of Quantity :-
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=101, name=Wheat, quantity=1089, price=36.89]
2.4 Sort Product list in increasing order of its price
- A list contains Product information for 5 as per insertion-order
- Sorting :- we are going to sort the Product list on the basis of increasing price using Collections.sort() method passing Comparator as 2nd argument and original list as 1st argument
SortProductListByPrice.java
package net.bench.resources.comparator.comparing;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortProductListByPrice {
// List of Products
private static List<Product> getProductList() {
return Arrays.asList(
new Product(101, "Wheat", 1089L, 36.89),
new Product(102, "Rice", 502L, 58.19),
new Product(103, "Lentils", 803L, 102.45),
new Product(104, "Oil", 208L, 164.75),
new Product(105, "Vegetables", 303L, 45.50)
);
}
public static void main(String[] args) {
// 1. get Product list
List<Product> productList = getProductList();
// 1.1 print to console
System.out.println("Product list as per Insertion-order :-\n");
productList.forEach(System.out::println); // iterating/printing
// 2. sorting Product list in increasing-order of price
System.out.println("\n\nSorted Product list in increasing-order of price :- \n");
// 2.1 sorting using Collections.sort() & Comparator
Collections.sort(
productList, // original list
Comparator.comparing(Product::getPrice) // Price sorting
);
// 2.2 iterate & printing
productList.forEach(System.out::println); // iterating/printing
}
}
Output:
Product list as per Insertion-order :-
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Sorted Product list in increasing-order of price :-
Product [id=101, name=Wheat, quantity=1089, price=36.89]
Product [id=105, name=Vegetables, quantity=303, price=45.5]
Product [id=102, name=Rice, quantity=502, price=58.19]
Product [id=103, name=Lentils, quantity=803, price=102.45]
Product [id=104, name=Oil, quantity=208, price=164.75]
2.5 Throws NullPointerException if argument is NULL
- If the input argument to Comparator.comparing() method is null then it throws NullPointerException as shown in the below illustration
- Check How to sort List and Arrays with null values to handle list with null values while sorting
SortProductListWithNullPresent.java
package net.bench.resources.comparator.comparing;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class SortProductListWithNullPresent {
// List of Products
private static List<Product> getProductList() {
return Arrays.asList(
new Product(102, "Rice", 502L, 58.19),
new Product(104, "Oil", 208L, 164.75),
new Product(103, "Lentils", 803L, 102.45),
new Product(105, "Vegetables", 303L, 45.50),
new Product(101, "Wheat", 1089L, 36.89)
);
}
public static void main(String[] args) {
// 1. get Product list
List<Product> products = getProductList();
// 2. sorting Product list in ascending-order by its Id
System.out.println("Exception - Sorted Product list with Null value :-\n");
// 3. sorting/iterating/printing
products
.stream() // 1. get sequential stream
.sorted(Comparator.comparing(null)) // 2. null
.forEach(System.out::println); // 3. iterate/printing
}
}
Output:
Exception - Sorted Product list with Null value :-
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Comparator.comparing(Comparator.java:467)
at net.bench.resources.comparator.comparing.SortProductListWithNullPresent
.main(SortProductListWithNullPresent.java:35)
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
Happy Coding !!
Happy Learning !!