0
Sponsored Links


Ad by Google
In this post we will show you the usage example of java.util.Comparator interface. The Comparator interface will help you to sort your objects using Collections.sort() or Arrays.sort(). In our previous post we have seen the implementation example of java.lang.Comparable interface and here we are going to use java.util.Comparator interface. The Comparator interface has two public methods.
  1. public int compare(Object obj1, Object obj2)
  2. public boolean equals(Object obj)
If you implement java.util.Comparator interface in your class, then you must have to override the compare(Object obj1, Object obj2) method. The compare(Object obj1, Object obj2) method will compare two objects and return negative integer or zero or positive integer value depending on the comparison. If obj1 is greater than obj2 return positive, if obj1 is less than obj2 return negative and if obj1 is equals to obj2 return 0.
java.util.Comparator interface can help you to write sorting based on different attribute of your object.
Let's see an example, here we have Order.java class and we would like to sort the order based on orderDate and emailID.
Order.java
package com.javamakeuse.poc;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Order {
 private String emailId;
 private String mobile;
 private Date orderDate;

 public Order(String emailId, String mobile, Date orderDate) {
  super();
  this.emailId = emailId;
  this.mobile = mobile;
  this.orderDate = orderDate;
 }
 public String getEmailId() {
  return emailId;
 }
 public String getMobile() {
  return mobile;
 }
 public Date getOrderDate() {
  return orderDate;
 }
 @Override
 public String toString() {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
  return "Order [emailId=" + emailId + ", mobile=" + mobile
    + ", orderDate=" + sdf.format(orderDate) + "]";
 }

}
Now let's create OrderComparison.java class which will compares Order based on orderDate and if two order placed at the same date than order will compare based on email Id.

OrderComparison.java
package com.javamakeuse.poc;

import java.util.Comparator;

public class OrderComparison implements Comparator<Order> {
 @Override
 public int compare(Order obj1, Order obj2) {
  int result = obj1.getEmailId().compareTo(obj2.getEmailId());
  if (obj2.getOrderDate().after(obj1.getOrderDate())) {
   result = -1;
  } else if (obj2.getOrderDate().before(obj1.getOrderDate())) {
   result = 1;
  }
  return result;
 }

}
Let's create OrderTest.java class to test our order comparison.
OrderTest.java
package com.javamakeuse.poc;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OrderTest {
 public static List<Order> getOrderList() {
  List<Order> orderList = new ArrayList<Order>();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
  try {
   orderList.add(new Order("pqr@gmail.com", "99999999999", sdf
     .parse("2015-01-02")));
   orderList.add(new Order("xyz@gmail.com", "99999999999", sdf
     .parse("2014-01-02")));
   orderList.add(new Order("abc@gmail.com", "99999999999", sdf
     .parse("2013-01-02")));
  } catch (ParseException pe) {
   System.out.println(pe);
  }
  return orderList;
 }

 public static void main(String[] args) {
  List<Order> orderList = getOrderList();
  System.out.println("Before sorting...");
  for (Order order : orderList) {
   System.out.println(order);
  }
  System.out.println("After sorting...");
  Collections.sort(orderList, new OrderComparison());
  for (Order order : orderList) {
   System.out.println(order);
  }
 }
}

OUT PUT:
Before sorting...
Order [emailId=pqr@gmail.com, mobile=99999999999, orderDate=2015-01-02]
Order [emailId=xyz@gmail.com, mobile=99999999999, orderDate=2014-01-02]
Order [emailId=abc@gmail.com, mobile=99999999999, orderDate=2013-01-02]
After sorting...
Order [emailId=abc@gmail.com, mobile=99999999999, orderDate=2013-01-02]
Order [emailId=xyz@gmail.com, mobile=99999999999, orderDate=2014-01-02]
Order [emailId=pqr@gmail.com, mobile=99999999999, orderDate=2015-01-02]
Sponsored Links

0 comments:

Post a Comment