基于对象属性的ArrayList排序



下面是Employee bean类。

public class Employee {
public String name;
public int age;
public Employee()
{
}
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;
}
}

我有另一个EmployeeTest类,在它里面我创建Employee类的对象并存储在ArrayList中。

import java.util.ArrayList;
public class EmployeeTest {
public static void main(String[] args) 
{
ArrayList<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setAge(15);
emp1.setName("Employee1");
Employee emp2 = new Employee();
emp2.setAge(10);
emp2.setName("Employee1");
empList.add(emp1);
empList.add(emp2);
for(Employee emp : empList)
{
System.out.println("employee name : " + emp.getName());
System.out.println("employee age : " + emp.getAge());
}
}
}

现在我有一个问题是,我想根据Employee类年龄属性对ArrayList进行排序。所以请解释一下我该如何分类。

让类实现Comparable接口是一种选择,正如其他答案中所建议的那样。

但一般来说,我建议NOT实现Comparable接口,只要该类没有毋庸置疑的自然排序。对于Employee,肯定存在NO自然有序

想象一下,您想根据员工的年龄对他们进行排序。按升序一次,按降序一次。你怎么能那样做?现在想象一下,你想按照他们的年龄对他们进行一次排序,按照他们的名字按字母顺序进行一次。如果不实现Comparator,就无法做到这一点。这就是我在这里推荐的:

你可以创建一个类似的方法

private static Comparator<Employee> byAge()
{
return new Comparator<Employee>()
{
@Override
public int compare(Employee o1, Employee o2)
{
return o1.getAge() - o2.getAge();
}
};        
}

然后你可以简单地调用

Collections.sort(empList, byAge());

如果你想按相反的顺序排序,你可以打电话给

Collections.sort(empList, Collections.reverseOrder(byAge()));

如果你想按它们的名字排序,你可以创建一个方法

private static Comparator<Employee> byName()
{
return new Comparator<Employee>()
{
@Override
public int compare(Employee o1, Employee o2)
{
return o1.getName().compareTo(o2.getName());
}
};        
}

并用进行分类

Collections.sort(empList, byName());

这比实现Comparable要通用得多。

您可以将Collections.sort方法与自定义比较器一起使用:

import java.util.Collections;
import java.util.Comparator;
[...]
Collections.sort(empList, new Comparator<Employee>() {
@Override public int compare(Employee x, Employee y) {
return Integer.compare(x.getAge(), y.getAge());
}
});

我用的是匿名的Comparator类,你也可以写一个普通的Comparator类,见Sri Harsha Chilakapati的答案。

您需要编写一个比较器。

class EmployeeAgeComparator implements Comparator<Employee>
{
@Override
public int compare(Employee e1, Employee e2)
{
if (e1.getAge() > e2.getAge())
{
return -1;
} 
else if (e1.getAge() < e2.getAge())
{
return 1;
}
return 0;
}
}

然后像这样使用Collections.sort方法。

Collections.sort(empList, new EmployeeAgeComparator());

希望这能有所帮助。

实现Comparable接口

class Employee implements Comparable<Employee> {   

添加实现Employee类中的方法compareTo,如下所示:

@Override
public int compareTo(Employee other) {
return this.age - other.age;
}

然后你可以像这里一样对你的列表进行排序:

Collections.sort(empList)

您的类必须实现Comparable接口并实现compareTo方法。然后您可以使用Arrays.sort(yourArray)对其进行排序。

我认为您可能想要对不同的属性进行排序,因此在这种情况下,您应该创建适当的Comparator,并将其与适当的排序方法(也需要这样做的方法)一起使用。

这是一个简单的排序算法。我们存储最低年龄的下标值,这样我们就可以交换它。

for (int i = 0; i < empList.size(); i++)
{
int smallest = i;
for (int j = i; j < numbers.length; j++)
{
if (empList.get(j).getAge() < emplist.get(smallest).getAge())
smallest = j;
}
int temp = empList.get(i).getAge();
empList.set(i, empList.get(smallest))
empList.set(smallest, temp);
}

最新更新