apache之beanutils包比较工具类ComparatorUtils

关于beanutils

beanutils是apache开源组织中的一个包,主要是为了更好的让开发者隔离JAVA包中自带的及其strus,spring等框架中

复杂的bean工具类的使用。

开源地址:http://commons.apache.org/beanutils/

这里主要介绍下beanutils下的ComparatorUtils的使用

直接上代码

public class Person {

    int id;
    private String name;
    int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 Person(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Person(){}

    public String toString() {
        return "[id=" + this.id + ",name=" + this.name +",age=" + this.age +"]";
    }
    
}

我们先按原先java自带的方式来处理

public class ComparatorDemo implements java.util.Comparator {

    public int compare(Object o1, Object o2) {
        if (o1 instanceof Person) {
      //按ID倒叙 name正序重新排列
            Person p1 = (Person) o1;
            Person p2 = (Person) o2;
            if (p1.getId() > p2.getId())
                return -1;
            else if (p1.getId() < p2.getId())
                return 1;
            else {
                if (p1.getName().compareTo(p2.getName()) > 0)
                    return 1;
                else if (p1.getName().compareTo(p2.getName()) == 0)
                    return 0;
                else
                    return -1;
            }

        }
        return 0;
    }

    public static void main(String[] args) {
        String names[]={"a","b"};
        ArrayList<Object>list=new ArrayList<Object>();
            list.add(new Person(1,names[1], 20));
            list.add(new Person(1,names[0], 21));
            list.add(new Person(2,names[1], 22));
            list.add(new Person(3,names[1], 24));
            list.add(new Person(2,names[0], 23));
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Person person = (Person) iterator.next();
            System.out.println(person.getId()+"--"+person.getName());
        }
        Collections.sort(list,new ComparatorDemo());
        System.err.println("****对比前后*****");
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Person person = (Person) iterator.next();
            System.out.println(person.getId()+"--"+person.getName());
        }
    }

}

打印结果:

1--b
1--a
2--b
3--b
2--a
****对比前后*****
3--b
2--a
2--b
1--a
1--b

然后我们在用beanutils包下的

ComparatorUtils来实现
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;

public class ComparatorUtilDemo {

    public static void main(String[] args) {
        String names[]={"a","b"};
        ArrayList<Object>list=new ArrayList<Object>();
            list.add(new Person(1,names[1], 20));
            list.add(new Person(1,names[0], 21));
            list.add(new Person(2,names[1], 22));
            list.add(new Person(3,names[1], 24));
            list.add(new Person(2,names[0], 23));
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Person person = (Person) iterator.next();
            System.out.println(person.getId()+"--"+person.getName());
        }
        //
        Comparator mycmp=ComparableComparator.getInstance();
        mycmp=ComparatorUtils.nullLowComparator(mycmp);//允许null
        mycmp=ComparatorUtils.reversedComparator(mycmp);//逆序
        ArrayList<Object>sortFields=new ArrayList<Object>();
        sortFields.add(new BeanComparator("id",mycmp));
        sortFields.add(new BeanComparator("name"));
        ComparatorChain multiSort=new ComparatorChain(sortFields);
        Collections.sort(list, multiSort);
        System.err.println("****对比前后*****");
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Person person = (Person) iterator.next();
            System.out.println(person.getId()+"--"+person.getName());
        }
        
    }
}

打印结果:

1--b
1--a
2--b
3--b
2--a
****对比前后*****
3--b
2--a
2--b
1--a
1--b

 发现两者结果一样。当然也可以选用Comparable来实现,效果是一样的

区别在于当我们不想改变原先代码逻辑的情况下,使用Comparable会更合适。

the end!

 

posted @ 2013-01-17 15:50  draem0507  阅读(6439)  评论(0编辑  收藏  举报
View Code