tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons Bean Utils > Custom Bean Comparator

Custom Bean Comparator 

Apache Commons BeanUtils is a java library useful for accessing bean properties and methods. It provides introspection capabilities to view and manipulate the properties and operations provided by the given class. This requires the libraries commons-beanutils-1.8.3.jar, commons-beanutils-bean-collections-1.8.3.jar, commons-beanutils-core-1.8.3.jar, commons-collections-3.2.1.jar, commons-logging.jar to be in classpath. The following example shows customizing BeanComparator class.

File Name  :  
com/bethecoder/tutorials/commons_beanutils/common/Student.java 
   
package com.bethecoder.tutorials.commons_beanutils.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  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;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/commons_beanutils/CustomBeanComparatorTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.commons_beanutils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanComparator;

import com.bethecoder.tutorials.commons_beanutils.common.Student;

public class CustomBeanComparatorTest {

  /**
   @param args
   @throws InvocationTargetException 
   @throws IllegalAccessException 
   */
  public static void main(String[] args
    throws IllegalAccessException, InvocationTargetException {
    
    List<Student> studentList = Arrays.asList(
        new Student("Sriram"2"Chess"),
        new Student("Sudhakar"29"Cricket"),
        new Student("Anu"28"Cooking"),
        new Student("Sahithi"1"Running")
    );
    
    BeanComparator beanComp = new BeanComparator("age"new Comparator<Integer>() {

      @Override
      public int compare(Integer ageFirst, Integer ageSecond) {
        
        //Sort in descending order
        return ageSecond.compareTo(ageFirst);
      }
      
    });
    Collections.sort(studentList, beanComp);
    
    for (Student std : studentList) {
      System.out.println(std);
    }
  }

}
   

It gives the following output,
Student[name = Sudhakar, age = 29, hobby = Cricket]
Student[name = Anu, age = 28, hobby = Cooking]
Student[name = Sriram, age = 2, hobby = Chess]
Student[name = Sahithi, age = 1, hobby = Running]



 
  


  
bl  br