如何根据bean对arraylist进行排序?

时间:2015-05-20 02:54:50

标签: sorting arraylist collections javabeans

public List<Bean> getList() {
    List<Bean> beanList = new ArrayList<Bean>();
    // some code here

    while(hasNext()) {
        // some code here
        Bean bean = new Bean();

        listing(bean, beanList);
    }

    // sort by age bean here before returning

    return beanList
}

public void listing(Bean bean, List<Bean> beanList) {
    //some code here

    bean.setName(var1);
    bean.setAddress(var2);
    bean.setAge(var3);

    beanList.add(bean)  
}

我需要根据年龄对arraylist进行排序。

我尝试使用收藏家,但我不确定如何使用这些代码实现它。

谢谢。

3 个答案:

答案 0 :(得分:0)

您需要实现Comparator接口。在这种情况下,您的实现将是

    public class BeanAgeComparator implements Comparator<Bean> {

    int compare(Bean o1,Bean o2) {
    return o1.getAge() - o2.getAge();
    }
    }

然后,您可以使用Collections.sort(beanList, new BeanAgeComparator());

List进行排序

答案 1 :(得分:0)

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

public class BinarySearch{

    public static void main(String s[]) {
        List<Bean> beanList=new BinarySearch().getList(10);
        for(Bean bean:beanList){System.out.println(bean.getAge());}


}

    public List<Bean> getList(int size) {
        List<Bean> beanList = new ArrayList<Bean>();
        int i=0;

        while(i<size) {
            Bean bean = new Bean();
            listing(bean, beanList);
            i++;
        }
        Comparator<Bean> c=new Comparator<Bean>(){
            @Override
            public int compare(Bean bean1, Bean bean2) {
                return bean1.getAge()-bean2.getAge();
            }};
        Collections.sort(beanList,c);
        return beanList;
    }


    Random r=new Random();
    public void listing(Bean bean, List<Bean> beanList) {


        bean.setName("name");
        bean.setAddress("address");
        bean.setAge(r.nextInt(81)+18);

        beanList.add(bean);  
    }
}

class Bean {
String name;    
String address;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
int age;


}

答案 2 :(得分:0)

或者你可以在你的Bean类中实现类似的接口 -

public class SortedList{

    public static void main(String s[]) {
        List<Bean> beanList=new SortedList().getList(10);
        for(Bean bean:beanList){System.out.println(bean.getAge());}


}

    public List<Bean> getList(int size) {
        List<Bean> beanList = new ArrayList<Bean>();
        int i=0;

        while(i<size) {
            Bean bean = new Bean();
            listing(bean, beanList);
            i++;
        }

        Collections.sort(beanList);

        return beanList;
    }
    Random r=new Random();
    public void listing(Bean bean, List<Bean> beanList) {


        bean.setName("name");
        bean.setAddress("address");
        bean.setAge(r.nextInt(81)+18);

        beanList.add(bean);  
    }
}

class Bean implements Comparable<Bean>{
String name;    
String address;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
int age;
@Override
public int compareTo(Bean bean) {

    return this.getAge()-bean.getAge();
}

}