[英]How to sort objects in arrayList by their class?
我有一個ArrayList
,其中包含 2 種類型的對象(來自Student
類的對象和Teacher
類中的對象)。
我的問題是如何對它進行排序,以便所有來自Student
對象首先出現,然后所有來自Teacher
班級的對象出現在它們之后。
例如:Student1、Student2、Student3、Teacher1、Teacher2、Teacher3
這是我的代碼:
public ArrayList sortList(){
ArrayList<Student> students = new ArrayList<Student>();
ArrayList<Teacher> teachers = new ArrayList<Teacher>();
ArrayList<Person> university = new ArrayList<Person>();
for(Person p : list){
if(p.getClass().getSimpleName().equals("Teacher")){
teachers.add((Teacher)p);
};
if(p.getClass().getSimpleName().equals("Student")){
students.add((Student)p);
}
university.addAll(students);
university.addAll(teachers);
}
return university;
}
您可以使用Collections.sort(list,comparator)
在 Comparator 中,您可以比較類名,因為(幸運的是) S
在字母表中位於T
之前:
List<Object> studentsAndTeachers = // ...
Collections.sort(studentsAndTeachers, (o1, o2) -> o1.getClass().getName().compareTo(o2.getClass().getName()));
因為您必須首先應用其他排序標准或擴展比較器以識別它們...
現在它應該像黃油面包一樣工作:
public class Main
{
public static void main(String[] args)
{
List<Human> humans = new ArrayList<>();
humans.add(new Teacher("Teacher1"));
humans.add(new Teacher("Teacher2"));
humans.add(new Student("Student3"));
humans.add(new Student("Student1"));
humans.add(new Teacher("Teacher3"));
humans.add(new Student("Student2"));
Collections.sort(humans, new Comparator<Human>()
{
@Override
public int compare(Human o1, Human o2)
{
if (o2 instanceof Teacher) {
return -1;
} else if (o1 instanceof Teacher && o2 instanceof Student) {
return 1;
}
/* other checks not about being teacher or student goes here... */
}
});
System.out.println(humans);
}
}
interface Human
{
String getName();
void setName(String name);
}
class Teacher implements Human
{
private String name;
public Teacher(String name)
{
super();
this.name = name;
}
@Override
public String getName()
{
return name;
}
@Override
public void setName(String name)
{
this.name = name;
}
}
class Student implements Human
{
private String name;
public Student(String name)
{
super();
this.name = name;
}
@Override
public String getName()
{
return name;
}
@Override
public void setName(String name)
{
this.name = name;
}
}
像這樣排序可以為您完成工作,盡管一般來說不是一個很好的解決方案。
Collections.sort(classroom, (o1, o2) -> {
if (o1 instanceof Student && o2 instanceof Teacher) {
return -1;
}
if (o1 instanceof Teacher && o2 instanceof Student) {
return 1;
}
return 0;
});
這是一個Comparator
類,它根據類名和 name 屬性的值對Human
對象進行排序。
public class Comparator<Human> {
public int compare(Human h1, Human h2) {
int res = h1.getClass().getName().compareTo(
h2.getClass().getName());
if (res == 0) {
res = h1.getName().compareTo(h2.getName());
}
return res;
}
}
如果您將它與Collections.sort(...)
,您可以將列表排序為所需的順序。
請注意,此實現:
Human
子類,則不會中斷。您可以使用Comparator.comparing
:
list.sort(Comparator.comparing(Teacher.class::isInstance))
這是有效的,因為Class.isInstance
返回boolean
,它被包裝成Boolean
,它實現了Comparable
。
您可以將Comparator.comparing
與listName.sort()
一起使用,如下所示;
list.sort(Comparator.comparing(o -> o.getClass().getName()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.