Tech I Enjoy Logo

Custom Search




  Home >> Java


How to write a comparator that can be used for any class that is
to be sorted, without changing the comparator or the class to be compared?

Suppose one has to sort all student objects in a list, then we know this can be
done by using either by defining java.lang.Comparable or 
java.util.Comparator interfaces.
By using java.util.Collections.sort method, one can do sorting on list of 
objects.

In this article I have tried to come up with another ways of providing
comparator that can be used with any number of JavaBeans, those are to be
comparated and sorted. By using this approach, I think, one should be able
to reduce somplexities of defining compareTo methods in many POJO, and
not to define many comparators for multiple POJOs (Those are to be sorted).

Explanation :
In this very scenario, the Comparator has to use reflection from java.lang.reflect
package to dynamically/runtime detection of the corresponding field to be used
for sorting and compare. Discussing with an example will help us understand this.

Example case study:
let us take the Student class as the class to be compared for sorting
based on two field "name" and "age".

Student.java -------------------------------------- private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } --------------------------------------- This Student class doesn't implement java.lang.Comparable interface, then the comparator (suppose the name is TestComparator): This TestComparator implements java.util.Comparator, and implement compareTo method. Input to this TestComparator are the field (on which the sorting will take place), and the type of sorting (Whether ascending or descending). There are two important information required for feeding required values to the compareTo method, one is the value of the field and the other one is the Java Type of this field. So is sorting is to be done based on Student.name, then required information are
  • value of name field - "test name"
  • type of name field - java.lang.String
  • And if sorting is to be done based on Student.age, then these are
  • value of age field - 23 (example age of student)
  • type of age field - int
  • To get these two inputs, we have two methods getValue and getType --------------------------------------- private Object getValue(Object a) { Object obj = null; Class[] obj1 = null; Object[] obj2 = null; String methodName = "get"+fieldName.replace(fieldName.substring(0,1) ,fieldName.substring(0,1).toUpperCase()); try{ obj = a.getClass().getMethod(methodName,obj1).invoke(a,obj2); }catch(Exception ex){ ex.printStackTrace(); } return obj; } ---------------------------------------- fieldName - the field to be used for sorting. methodName - to get the accessible method name for the attribute, like if attribute name is "age", the methodName will be "getAge". obj - gets the value that the methodName returns from object. ---------------------------------------- private Object getType(Object a) { Class[] obj1 = null; Object obj = null; String methodName = "get"+fieldName.replace(fieldName.substring(0,1), fieldName.substring(0,1).toUpperCase()); try{ obj = a.getClass().getMethod(methodName,obj1).getReturnType() .getName().toString(); }catch(Exception ex){ ex.printStackTrace(); } return obj; } ----------------------------------------- This method returns the Java type of the methodName. Client code using Java Technology, includes creation of Student Object multiple times 1. Student student1=new Student("name1",34); Student student2=new Student("aname2",64); Student student3=new Student("zname3",27); 2. create a list List list = new ArrayList(); 3. add all the three students list.add(student1); list.add(student2); list.add(student3); 4. sort this list using Collections Collections.sort(list,new TestComparator("name",1)); 1 = for descending order 0 = for ascending 5. Not this list is sorted Students by name in descending order. Now the complete source of this TestComparator is as follows: TestComparator.java
    
    /**
    *  This source is provided as is, without any warranty
    *  and /or guaranty of any kind.
    *  Copyright (C) 2008, ISHTIAK, All Rights Reserved.
    *  You can use it for Personal Learning purpose only.
    *  source: http://www.interview-questions-tips-forum.net
    */
    
    import java.util.Comparator;
    
    public class TestComparator implements Comparator
    {
        private String fieldName;
        public TestComparator(String argFieldName, int a) {
            this.fieldName = argFieldName;
    	}
        private Object getValue(Object a)
        {
          Object obj = null;
          Class[] obj1 = null;
          Object[] obj2 = null;
          String methodName = "get"+fieldName.replace(fieldName.substring(0,1)
                                     ,fieldName.substring(0,1).toUpperCase());
    
         try{
            obj = a.getClass().getMethod(methodName,obj1).invoke(a,obj2);
    
         }catch(Exception ex){
            ex.printStackTrace();
         }
          return obj;
       }
       private Object getType(Object a)
       {
         Class[] obj1 = null;
         Object obj = null;
         String methodName = "get"+fieldName.replace(fieldName.substring(0,1),
         fieldName.substring(0,1).toUpperCase());
         try{
             obj = a.getClass().getMethod(methodName,obj1)
                               .getReturnType().getName().toString();
         }catch(Exception ex){
           ex.printStackTrace();
         }
         return obj;
       }
    
      public int compare(Object obj1, Object obj2) {
    	  int status=0;
    	  //System.out.println(getType(obj2));
        if(getType(obj2).equals("java.lang.String")) {
    		status = ((String)getValue(obj1)).compareTo((String)getValue(obj2));
    	} 
        if(getType(obj2).equals("int")) {
    		status = ((Integer)getValue(obj1)).compareTo((Integer)getValue(obj2));
    	} 
    	return status;
      }
    }
    
    The client to test this application is as follows: TestClient.java /** * This source is provided as is, without any warranty * and /or guaranty of any kind. * Copyright (C) 2008, ISHTIAK, All Rights Reserved. * You can use it for Personal Learning purpose only. * source: http://www.interview-questions-tips-forum.net */
    import java.util.*;
    public class TestClient {
      public TestClient() {
    	      //Some random test data.
    
    	  Student student1=new Student("name1",34);
    	  Student student2=new Student("aname2",64);
    	  Student student3=new Student("zname3",27);
    
        List list = new ArrayList();
        list.add(student1);
    		list.add(student2);
    		list.add(student3);
        //test code for sorting Students based on name field.
        Collections.sort(list,new TestComparator("name",1));
        System.out.println("Sorted based on Student.name :");
        Iterator  itr = list.iterator();
    	while(itr.hasNext()) {
    		System.out.println(((Student)itr.next()).getName());
    	}
    
        //test code for sorting Students based on age field.
        Collections.sort(list,new TestComparator("age",1));
        System.out.println("Sorted based on Student.age :");
        Iterator  itr1 = list.iterator();
    	while(itr1.hasNext()) {
    		System.out.println(((Student)itr1.next()).getAge());
    	}
      }
      public static void main(String args[]) {
        new TestClient();
      }
    }
    
    As of now this code is at very preliminary stage, and I am working on it to make this a framework suitable enough to be used in any project/product. if interested in participating with this initiative, please provide your comments (those are really valuable to me).
    If anything missed out , please let me know at techienjoy at yahoo . com
    Image creation using Java AWT :
    In-Memory Image creation using AWT on Java Platform.
    Java Reflection using Comparator :
    Comparator using Reflection on Java Platform
    and example discussed.
    Synchronized Block wait and notify With Example :
    Example using wait and notify from within
    synchronized block with code and explained
    Thread wait and notify With Example :
    Example using wait and notify within Thread 
    with code and explained
    Event Handling using Java Technology :
    Example on how to use Event
    and handling code using Java Technology.
    Thread Deadlock using Java :
    Example reproducing a possible Thread 
    using Java Technology.
    Thread design scenario using Java :
    Thread design scenario using Java Technology.
    List of Examples on Java Platform :
    List of Examples on Java Platform.
    Example of Drag and Drop using Java Technology :
    Example of Drag and Drop using Java Technology.
    RMI and Java Stub and Skeleton :
    Example using RMI using Java Technology.
    Example ThreadPoolExecutor using Java :
    ThreadPoolExecutor Example using Java Technology.
    Interview Questions and answer of Java Technology :
    Interview Questions and answer of Java Technology.


    References :
    Tags: java comparator reflection
    Tags: java example drag n drop
    Tags: Java Interview Questions
    Tags: java rmi tutorial stub skeleton
    Tags: Java Thread Deadlock
    Tags: Java Thread Design Scenarios
    Tags: Java threadpoolexecutor
    Tags: Java
    

    
    
    DISCLAIMER :
    The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
    techienjoy.com is not liable for any negative consequences that may result/arise from 
    implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    All contents of this site is/are written and provided on an "AS IS" basis,
    without WARRANTIES or conditions of any kind, either express or implied, including, without
    limitation, merchantability, or fitness for a particular purpose. You are solely responsible
    for determining the appropriateness of using or refering this and assume any risks associated
    with this.
    
    In spite of all precautions taken to avoid any typo in these pages, there might be some 
    issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
    extends sincerest apologies to all our visitors for the same.
    
    
    

    Android Examples || Android Examples

    © Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions