Close

Java Collections - Comparator.comparing() Examples

Java Collections Java Java API 


Interface:

java.util.Comparator

java.util.ComparatorComparatorLogicBig

Methods:

static <T,U extends Comparable<? super U>> Comparator<T> comparing(
                                            Function<? super T,? extends U> keyExtractor)

This static method returns an instance of Comparator , that compares type T objects according to the specified key extractor function.



static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor,
                                     Comparator<? super U> keyComparator)

This static method returns an instance of Comparator that compares type T objects according to the specified function (a compare key extractor) and a comparator (the extracted key comparator).


Examples


package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparingExample {

public static void main(String... args) {
List<Customer> list = createExampleCustomers();

System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparing(Customer::getEmail));
System.out.printf("after sort: %s%n", list);
}

private static String getEmailDomain(String email) {
int i = email.indexOf("@");
if (i != -1) {
return email.substring(i + 1);
}
return "";
}

private static List<Customer> createExampleCustomers() {
return Arrays.asList(
new Customer("sara@example.com", "Sara"),
new Customer("john@example.com", "John"),
new Customer("mike@example.com", "Mike")
);
}

private static class Customer {
private String email;
private String name;

public Customer(String email, String name) {
this.email = email;
this.name = name;
}

public String getEmail() {
return email;
}

public String getName() {
return name;
}

@Override
public String toString() {
return "Customer{" +
"email='" + email + '\'' +
", name='" + name + '\'' +
'}';
}
}
}

Output

before sort: [Customer{email='sara@example.com', name='Sara'}, Customer{email='john@example.com', name='John'}, Customer{email='mike@example.com', name='Mike'}]
after sort: [Customer{email='john@example.com', name='John'}, Customer{email='mike@example.com', name='Mike'}, Customer{email='sara@example.com', name='Sara'}]




package com.logicbig.example.comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparingExample2 {

public static void main(String... args) {
List<Customer> list = createExampleCustomers();

System.out.printf("before sort: %s%n", list);
Collections.sort(list, Comparator.comparing(Customer::getEmail, getEmailDomainComparator()));
System.out.printf("after sort: %s%n", list);
}

public static Comparator<String> getEmailDomainComparator(){
return (email1, email2) -> {
String emailDomain1 = getEmailDomain(email1);
String emailDomain2 = getEmailDomain(email2);
return emailDomain1.compareTo(emailDomain2);
};
}

private static String getEmailDomain(String email) {
int i = email.indexOf("@");
if (i != -1) {
return email.substring(i + 1);
}
return "";
}

private static List<Customer> createExampleCustomers() {
return Arrays.asList(
new Customer("sara@example.com", "Sara"),
new Customer("john@example.com", "John"),
new Customer("mike@example.com", "Mike")
);
}

private static class Customer {
private String email;
private String name;

public Customer(String email, String name) {
this.email = email;
this.name = name;
}

public String getEmail() {
return email;
}

public String getName() {
return name;
}

@Override
public String toString() {
return "Customer{" +
"email='" + email + '\'' +
", name='" + name + '\'' +
'}';
}
}
}

Output

before sort: [Customer{email='sara@example.com', name='Sara'}, Customer{email='john@example.com', name='John'}, Customer{email='mike@example.com', name='Mike'}]
after sort: [Customer{email='sara@example.com', name='Sara'}, Customer{email='john@example.com', name='John'}, Customer{email='mike@example.com', name='Mike'}]




See Also