根据对象JAVA 11中值的自定义优先级对对象列表进行排序
我有一个学生(姓名、结果、卷号)对象列表根据对象JAVA 11中值的自定义优先级对对象列表进行排序,java,list,sorting,arraylist,java-11,Java,List,Sorting,Arraylist,Java 11,我有一个学生(姓名、结果、卷号)对象列表 List.of( new Student("Sam", "Pass", "100"), new Student("Gill", "Not available", "101"), new Student("Joe", &qu
List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133"),
);
我需要按照以下要求对上面的列表进行排序(我使用的是Java11)
{"Joe", "Fail", "111"}
{"Matt", "Fail", "115"}
{"Sam", "Pass", "100"}
{"Ann", "Pass", "121"}
{"Moss", "Pass", "133"}
{"Gill", "Not available", "101"}
由于Java 8,我们可以在多个字段上对流进行排序 当您想按优先级“Fail”、“Pass”、“Not available”排序时,另一种选择是在学生中添加一个额外字段。当没有自然顺序时,这将有助于确定排序的优先级。在本例中,学生按“通过”、“失败”、“不可用”排序,然后按姓名排序,只是为了显示更多选项 对代码段进行排序:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
在学生中添加了字段:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
添加了排序课堂优先级的方法学生:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
上下文中的getPriority()方法:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
由于Java 8,我们可以在多个字段上对流进行排序 当您想按优先级“Fail”、“Pass”、“Not available”排序时,另一种选择是在学生中添加一个额外字段。当没有自然顺序时,这将有助于确定排序的优先级。在本例中,学生按“通过”、“失败”、“不可用”排序,然后按姓名排序,只是为了显示更多选项 对代码段进行排序:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
在学生中添加了字段:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
添加了排序课堂优先级的方法学生:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
上下文中的getPriority()方法:
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
public static void main(String []args){
List<Student> students = List.of(
new Student("Sam", "Pass", "100"),
new Student("Gill", "Not available", "101"),
new Student("Joe", "Fail", "111"),
new Student("Matt", "Fail", "115"),
new Student("Ann", "Pass", "121"),
new Student("Moss", "Pass", "133")
);
Comparator<Student> compareByPriorityThenName =
Comparator.comparing(Student::getPriority)
.thenComparing(Student::getName);
List<Student> sortedStudents = students.stream()
.sorted(compareByPriorityThenName)
.collect(Collectors.toList());
sortedStudents.forEach(System.out::println);
}
Student{name='Ann', result='Pass', rollNo='121', priority='1'}
Student{name='Moss', result='Pass', rollNo='133', priority='1'}
Student{name='Sam', result='Pass', rollNo='100', priority='1'}
Student{name='Joe', result='Fail', rollNo='111', priority='2'}
Student{name='Matt', result='Fail', rollNo='115', priority='2'}
Student{name='Gill', result='Not available', rollNo='101', priority='3'}
private int priority;
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
public class Student {
private final String name;
private final String result;
private final String rollNo;
private int priority;
public Student(String name, String result, String rollNo) {
this.name = name;
this.result = result;
this.rollNo = rollNo;
this.priority = 0;
}
public String getName() {
return name;
}
public String getResult() {
return result;
}
public String getRollNo() {
return rollNo;
}
public int getPriority() {
setPriority();
return priority;
}
private int setPriority() {
if(priority > 0) {
return priority;
}
switch(result) {
case "Pass":
priority = 1;
break;
case "Fail":
priority = 2;
break;
default:
priority = 3;
}
return priority;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", result='" + result + '\'' +
", rollNo='" + rollNo + '\'' +
", priority='" + priority + '\'' +
'}';
}
}
编写一个表示该排序的
比较器
,并将该比较器的实例传递给集合。排序
。您还可以在学生
类中为结果字段实现可比
,然后您不需要优先级顺序,您可以使用ordinal。编写一个表示该排序的比较器
排序并将该比较器的实例传递给集合。排序
。您还可以在学生
类中为结果字段实现可比
,然后您不需要优先级,您可以使用序号。