Wednesday, April 24, 2019

Comparator In Java

Q- What are the different ways to use comparator.

There are different ways of to use Comparator in you code.

  • Way -1:  Anonymous class of Comparator. 
  • Way -2:  Java Class to implements Comparator
  • Way -3 : Java method which return java.util.Comparator

Note :- See all three ways in below code.

Comparator Example:

User.java

package com.javaiq.in.comparator;
import java.util.Date;

public class User {
 
 private int id;
 private String name;
 private String role;
 private Date dateOfBirth;

 public User (int id,String name, String role, Date dob) {
  this.id = id;
  this.name = name;
  this.role = role;
  this.dateOfBirth = dob;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getRole() {
  return role;
 }

 public void setRole(String role) {
  this.role = role;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Date getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(Date dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 @Override
 public String toString() {
  return "User["+this.id + ", " + this.name + ", " + this.role+ ","+this.dateOfBirth;
 }
}

MainClass.java

package com.javaiq.in.comparator;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MainClass {

 public static void main(String[] args) {
  
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.YEAR, -20);
  cal.add(Calendar.MONTH, 5);
  
  List<User> userList = new ArrayList<User>();
  userList.add(new User(1, "Rajeev", "Admin",cal.getTime()));
  
  cal.add(Calendar.YEAR, 2); // add 3 years in current year.
  userList.add(new User(3, "Sumit", "User",cal.getTime()));
  
  cal.add(Calendar.YEAR, -6);
  userList.add(new User(2, "Dinesh", "User",cal.getTime()));
  
  cal.add(Calendar.YEAR, 10);
  userList.add(new User(4, "Kumar", "Admin",cal.getTime()));

  System.out.println("#### Before order ####");
  System.out.println(userList);

  // Anonymous comparator method
  System.out.println("#### Sort by date, ascending order ####");
  Collections.sort(userList, new Comparator<User>() {
    @Override
    public int compare(User obj1, User obj2) {
   if (obj1.getDateOfBirth().before(obj2.getDateOfBirth())) {
    return -1;
   } else if (obj1.getDateOfBirth().after(obj2.getDateOfBirth())) {
     return 1;
   } else {
     return 0;
   }
    }
   
  });
  System.out.println(userList);

  // Sort by date
  System.out.println("#### Sort by date ####");
  Collections.sort(userList, getNameComparatorDesc());
  System.out.println(userList);
  
  // Sort in by name descending order
  System.out.println("#### Sort by name, decending order ####");
  Collections.sort(userList, getNameComparatorDesc());
  System.out.println(userList);
  
  //DateOfBirthComparatorAscOrder
  System.out.println("#### Sort by date, Asc order ####");
  Collections.sort(userList, new DateOfBirthComparatorAscOrder());
  System.out.println(userList);
 }
 
 
 //Sorting on Name Property : descending order by name.
 public static Comparator<User> getNameComparatorDesc() {
  Comparator<User> userCompartor = new Comparator<User>() {

   @Override
   public int compare(User obj1, User obj2) {
    if (obj1.getName().compareTo(obj2.getName()) > 1) {
     return -1;
    } else if (obj1.getName().compareTo(obj1.getName()) < 0) {
     return 1;
    } else {
     return 0;
    }
   }

  };

  return userCompartor;
 }
 
 //Sorting on Date Property : ascending order by date
 public static Comparator<User> getDateOfBirthComparator() {
  Comparator<User> userCompartor = new Comparator<User>() {

   @Override
   public int compare(User obj1, User obj2) {
    if (obj1.getDateOfBirth().before(obj2.getDateOfBirth())) {
     return -1;
    } else if (obj1.getDateOfBirth().after(obj2.getDateOfBirth())) {
     return 1;
    } else {
     return 0;
    }
   }
  };

  return userCompartor;
 }
}

Java Class to implements Comparator - (way-2)
DateOfBirthComparatorAscOrder .java

package com.javaiq.in.comparator;
import java.util.Comparator;

public class DateOfBirthComparatorAscOrder implements Comparator<User> {

 @Override
 public int compare(User obj1, User obj2) {
  if (obj1.getDateOfBirth().before(obj2.getDateOfBirth())) {
   return -1;
  } else if (obj2.getDateOfBirth().after(obj2.getDateOfBirth())) {
   return 1;
  } else {
   return 0;
  }
 }
}

No comments:

Post a Comment