Comparator in java

In this post, we will see how you can use comparator to sort list of objects in java.

Comparator:

When you want to sort the list of objects of a class,you can use Comparator interface. You don’t need to implement Comparator on the class whose objects need to be sorted. You can create a separate class and implement a Comparator interface as below.

For example:

You can use different sorting logic based on different attributes of object that needs to be sorted.

For example:
Let’s say you want to sort list of employees by name,you can use below ocde to do that.

Java code for Comparator:

Create a class named Employee.java which will have empId, name and age.

1.Employee.java

Create a class named “EmployeeSortByIdComparator”. This class will have logic to sort list of Employees by empId.
2.EmployeeSortByIdComparator.java

Let’s create main class which will have logic to create a list of objects and sort it based on empId.

3.EmployeeComparatorMain.java
Output:

Anonymous Comparator:

One of advantage of Comparator over comparable is you can create anonymous comparator i.e you don’t need to implement Comparable interface to class whose objects need to be sorted.
Let’s understand more with help of example:
We will use an anonymous class to sort the list of Employees by name.

ComparatorMain.java
Output:
As you can see here, we used an anonymous comparator to sort the list of employees by name. We did not create comparator specific class here.
that’s all about Comparator in Java.

You may also like:


import_contacts

You may also like:

Related Posts

  • Print HashMap in java
    30 June

    Print HashMap in Java

    Table of ContentsPrint from the HashMap ReferencePrint HashMap using foreach method with keyset()Print HashMap using Consumer with entrySet()Print HashMap using Arrays’s asList() methodPrint HashMap using Collections’s singletonList()Print HashMap using getkey() and getValue with entrySet()Print HashMap using BiConsumerPrint HashMap using IteratorPrint HashMap using custom ObjectsConclusion In this article, we will see how to print HashMap in […]

  • Print LinkedList in java
    26 June

    Print LinkedList in java

    Table of ContentsJava inbuilt LinkedListPrint LinkedList using a for loopPrint LinkedList using Enhanced for loopPrint LinkedList using streamsPrint LinkedList using toString() methodPrint LinkedList using IteratorPrint LinkedList using custom objectsCustom singly LinkedListHow to print a custom singly LinkedListConclusion Java inbuilt LinkedList Java inbuilt LinkedList class uses doubly linked list as internal data structure to store its […]

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Print ArrayList in java
    11 May

    Print ArrayList in Java

    Learn about how to print ArrayList in java in different ways.

  • 15 April

    How to Deep Copy Arraylist in Java

    Learn about how to create deep copy of ArrayList in different ways.

  • 02 October

    Initialize ArrayList with values in Java

    Table of ContentsUsing Arrays.asList()Initialize ArrayList with String valuesintialize ArrayList with Integer valuesintialize ArrayList with float valuesUsing Stream in Java 8Using Factory Method in java 9Using double braces In this article, we will learn to initialize ArrayList with values in Java. ArrayList is an implementation class of List interface in Java. It is used to store […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.