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:

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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

  • 02 October

    Initialize ArrayList with values in Java

    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 elements. It is based on a dynamic array concept that grows accordingly. We can Initialize ArrayList with values in several ways. Let’s see some of them with […]

  • 17 March

    PriorityQueue in Java 8

    In this post, we will see about Java 8 PriorityQueue. When the objects are supposed to be processed on the basis of their priority, in that scenario we use PriorityQueue. It’s a special type of queue (also, unbound queues) where the elements can be ordered either as per their natural ordering or based on a […]

  • 06 October

    2d Arraylist java example

    In this post, we will see how to create 2d Arraylist in java. Best way to create 2d Arraylist is to create list of list in java. [crayon-606e2cf7cf83d296444109/] Let’s create a program to implement 2d Arraylist java. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

  • 04 May

    How HashMap works in java

    Most common interview questions are <code>How HashMap works in java</code>, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. [crayon-606e2cf805dc6493075862-i/]  is one of the most used Collections in java.Rather than going through theory, we will start with example first, so that you will […]

  • 21 October

    How to Sort HashSet in Java

    In this post, we will see how to sort HashSet in java. HashSet is a collection which does not store elements in any order. You might come across a situation where you need to sort HashSet. There can be many ways to sort HashSet, we will see two methods here. 💻 Awesome Tech Resources: Looking […]

  • 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using this method. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

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.