Java 8 Comparator

In this tutorial we will see some examples of how to use Java 8 Comparator both using Lambda Expression and in Streams.

Java 8 Comparator

                                                       Java 8 Comparator

Java Comparator Prior to Java 8

package lambda;

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

public class Java7Comparator {
	public static void main(String[] args) {
		List<Person> personList = Arrays.asList(new Person("Sridharan", "Nithyanandam", 30, 12345.8),
				new Person("Ram", "Kumar", 32, 432424.8), new Person("Vijay", "Devarkonda", 23, 34345323.8),
				new Person("Atal", "Bihari", 45, 34534.8));

		System.out.println("Before Sorting");
		personList.stream().forEach(System.out::println);

		// Sort by Age Ascending Order
		Collections.sort(personList, new Comparator<Person>() {
			@Override
			public int compare(Person arg0, Person arg1) {
				return arg0.getAge() - arg1.getAge();
			}
		});

		System.out.println("After Sorting By Age - Ascending Order");
		personList.stream().forEach(System.out::println);

		// Sort by Age Descending Order
		Collections.sort(personList, new Comparator<Person>() {
			@Override
			public int compare(Person arg0, Person arg1) {
				return arg1.getAge() - arg0.getAge();
			}
		});

		System.out.println("After Sorting By Age - Descending Order");
		personList.stream().forEach(System.out::println);

		// Sort by Last Name Ascending Order
		Collections.sort(personList, new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o1.getLastName().compareTo(o2.getLastName());
			}
		});

		System.out.println("After Sorting By LastName - Ascending Order");
		personList.stream().forEach(System.out::println);

		// Sort by Last Name Descending Order
		Collections.sort(personList, new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o2.getLastName().compareTo(o1.getLastName());
			}
		});

		System.out.println("After Sorting By LastName - Descending Order");
		personList.stream().forEach(System.out::println);

	}

}


Output

Before Sorting
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

After Sorting By Age
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

After Sorting By Age - Descsending
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

After Sorting By LastName - Ascending
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Java 8 Comparator –  Without Using Collections.sort

From Java 8, directly we can use sort() method from List interface. Prior to Java 8, we should use Collections class.


personList.sort(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o1.getAge() - o2.getAge();
			}
		});

Java 8 Comparator –  Using Lambda Expression


	personList.sort((p1, p2) -> p1.getAge() - p2.getAge());

Complete Example

package lambda;

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

public class LambdaComparator {

	public static void main(String[] args) {
		List<Person> personList = Arrays.asList(new Person("Sridharan", "Nithyanandam", 30, 12345.8),
				new Person("Ram", "Kumar", 32, 432424.8), new Person("Vijay", "Devarkonda", 23, 34345323.8),
				new Person("Atal", "Bihari", 45, 34534.8));

		System.out.println("Before Sorting");
		personList.stream().forEach(System.out::println);

		// Sort by Age Ascending Order
		personList.sort(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o1.getAge() - o2.getAge();
			}
		});

		System.out.println("After Sorting By Age - Ascending Order");
		personList.stream().forEach(System.out::println);

		// Sort by Age Descending Order
		personList.sort(new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				return o2.getAge() - o1.getAge();
			}
		});

		System.out.println("After Sorting By Age - Descending Order");
		personList.stream().forEach(System.out::println);

		// Sort by Age Ascending Order - Using Lambda Expression
		personList.sort((p1, p2) -> p1.getAge() - p2.getAge());
		System.out.println("After Sorting By Age - Ascending Order - Using Lambda Expression");
		personList.stream().forEach(System.out::println);
		// Sort by Age Descending Order - Using Lambda Expression
		Comparator<Person> personSorterDesc = (p1, p2) -> p2.getAge() - p1.getAge();
		personList.sort(personSorterDesc);
		System.out.println("After Sorting By Age - Descending Order - Using Lambda Expression");
		personList.stream().forEach(System.out::println);

		// Sort by Age Descending Order - Using Lambda Expression and reversed Method
		Comparator<Person> personSorterDescReversed = (p1, p2) -> p1.getAge() - p2.getAge();
		personList.sort(personSorterDescReversed.reversed());
		System.out.println("After Sorting By Age - Descending Order - Using Lambda Expression and reversed");
		personList.stream().forEach(System.out::println);

		// Sort by LastName Ascending Order - Using Lambda Expression
		Comparator<Person> personSortByLastName = (p1, p2) -> p1.getLastName().compareTo(p2.getLastName());
		personList.sort(personSortByLastName);
		System.out.println("After Sorting By LastName - Ascending Order - Using Lambda Expression");
		personList.stream().forEach(System.out::println);

		// Sort by LastName Descending Order - Using Lambda Expression and Reversed
		Comparator<Person> personSortByLastNameReversed = (p1, p2) -> p1.getLastName().compareTo(p2.getLastName());
		personList.sort(personSortByLastNameReversed.reversed());
		System.out.println("After Sorting By LastName - Ascending Order - Using Lambda Expression and Reversed");
		personList.stream().forEach(System.out::println);
	}

}


Output


Before Sorting

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

&amp;nbsp;

After Sorting By Age - Ascending Order

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

&amp;nbsp;

After Sorting By Age - Descending Order

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

&amp;nbsp;

After Sorting By Age - Ascending Order - Using Lambda Expression

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

&amp;nbsp;

After Sorting By Age - Descending Order - Using Lambda Expression

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

&amp;nbsp;

After Sorting By Age - Descending Order - Using Lambda Expression and reversed

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

&amp;nbsp;

After Sorting By LastName - Ascending Order - Using Lambda Expression

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

&amp;nbsp;

After Sorting By LastName - Ascending Order - Using Lambda Expression and Reversed

Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]

Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]

Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

Java 8 Comparator –  Using Stream Expression

 

List<Person> ageSortedList = personList.stream().sorted((p1, p2) -> p1.getAge() - p2.getAge())
				.collect(Collectors.toList());

Complete Example

package stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import lambda.Person;

public class StreamComparator {

	public static void main(String[] args) {
		List<Person> personList = Arrays.asList(new Person("Sridharan", "Nithyanandam", 30, 12345.8),
				new Person("Ram", "Kumar", 32, 432424.8), new Person("Vijay", "Devarkonda", 23, 34345323.8),
				new Person("Atal", "Bihari", 45, 34534.8));

		System.out.println("Before Sorting");
		personList.stream().forEach(System.out::println);

		List<Person> ageSortedList = personList.stream().sorted((p1, p2) -> p1.getAge() - p2.getAge())
				.collect(Collectors.toList());

		System.out.println("After Sorting By Age");
		ageSortedList.stream().forEach(System.out::println);

		List<Person> ageSortedDescList = personList.stream().sorted((p1, p2) -> -p1.getAge() - p2.getAge())
				.collect(Collectors.toList());

		System.out.println("After Sorting By Age - Descsending");
		ageSortedDescList.stream().forEach(System.out::println);

		Comparator<Person> comparator = (p1, p2) -> p1.getLastName().compareTo(p2.getLastName());
		List<Person> lastNameSortAscList = personList.stream()
				.sorted(comparator.reversed()).collect(Collectors.toList());
		System.out.println("After Sorting By LastName - Ascending");
		lastNameSortAscList.stream().forEach(System.out::println);

	}

}


Before Sorting
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

After Sorting By Age
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]

After Sorting By Age - Descsending
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]

After Sorting By LastName - Ascending
Person [firstName=Sridharan, lastName=Nithyanandam, age=30, salary=12345.8]
Person [firstName=Ram, lastName=Kumar, age=32, salary=432424.8]
Person [firstName=Vijay, lastName=Devarkonda, age=23, salary=3.43453238E7]
Person [firstName=Atal, lastName=Bihari, age=45, salary=34534.8]


If you have any questions in this Java 8 Comparator, please comment your queries below. If you like this tutorial please share it with your friends.

 

References

Comparator Interface

Leave a comment

Your email address will not be published. Required fields are marked *