我接下来的课程不是可编译的代码。我想发送条件作为参数来对employees数组进行排序。如何在Java 8功能样式中使用Comparator.comparing()来实现?例如,我想按名称或年龄等对其进行排序。
$ cat input.txt
col1,col2,col3,col4,col5
s1,s2,s2,s3,s1
col1,col2,col3,col4,col5
s1,s1,s2,s3,s3
col1,col2,col3,col4,col5
s1,s1,s1,s3,s3
col1,col2,col3,col4,col5
s1,s1,s2,s3,s3
答案 0 :(得分:4)
您错过了方法的定义:
public static <T, U extends Comparable<? super U>> void sortEmpoyeeByCriteria(Function<? super T, ? extends U> byCriteria) {
Comparator<T> c = Comparator.comparing(byCriteria);
}
在这种情况下,您还应该将List
传递给您要排序的方法。
但是为什么不在这种情况下使用Collections.sort
并在表单中传递List
和Comparator
(例如):
Collections.sort(yourList, Comparator.comparing(Employee::getXXX))
修改强> 您可以这样声明:
public static <T, U extends Comparable<? super U>> void sortEmpoyeeByCriteria(
List<T> list,
Function<? super T, ? extends U> byCriteria) {
Comparator<? super T> c = Comparator.comparing(byCriteria);
list.sort(c);
}
答案 1 :(得分:1)
您可以使用参考方法
public static void main(String[] args) {
Employee[] employees = new Employee[] { new Employee("John", 25, 3000.0, 9922001),
new Employee("Ace", 22, 2000.0, 5924001), new Employee("Keith", 35, 4000.0, 3924401) };
Comparator<Employee> comparingAge = Comparator.comparing(Employee::getAge);
Comparator<Employee> comparingName = Comparator.comparing(Employee::getName);
Comparator<Employee> comparingSalary = Comparator.comparing(Employee::getSalary);
Arrays.sort(employees, comparingAge);
System.out.println(Arrays.toString(employees));
Arrays.sort(employees, comparingName);
System.out.println(Arrays.toString(employees));
Arrays.sort(employees, comparingSalary);
System.out.println(Arrays.toString(employees));
}
你也可以这样做:
sortMe(employees, comparingAge);
sortMe(employees, comparingName);
sortMe(employees, comparingSalary);
sortme是:
private static void sortMe(Employee[] employees, Comparator<Employee> comparingCriteria) {
Arrays.sort(employees, comparingCriteria);
}
答案 2 :(得分:0)
在Comparator.comparing()中尝试使用lambda方法
class Employee {
String name;
int age;
double salary;
long mobile;
public Employee(String name, int age, double salary, long mobile) {
this.name = name;
this.age = age;
this.salary = salary;
this.mobile = mobile;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public long getMobile() {
return mobile;
}
public void setMobile(long mobile) {
this.mobile = mobile;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + ", mobile=" + mobile + "]";
}
}
public class MainClass
{
public static List<Employee> getEmployee()
{
List<Employee> ob = new ArrayList<>();
ob.add(new Employee("John", 25, 3000.0, 9922001));
ob.add(new Employee("Ace", 22, 2000.0, 5924001));
ob.add(new Employee("Keith", 35, 4000.0, 3924401));
return ob;
}
public static void main(String a[]) {
List<Employee> ob = getEmployee();
System.out.println("Sort By Name");
ob.sort(Comparator.comparing(e -> e.getName()));
System.out.println(ob);
System.out.println("Sort By Age");
ob.sort(Comparator.comparing(e -> e.getAge()));
System.out.println(ob);
}
}
输出:
Sort By Name
[Employee [name=Ace, age=22, salary=2000.0, mobile=5924001], Employee [name=John, age=25, salary=3000.0, mobile=9922001], Employee [name=Keith, age=35, salary=4000.0, mobile=3924401]]
Sort By Age
[Employee [name=Ace, age=22, salary=2000.0, mobile=5924001], Employee [name=John, age=25, salary=3000.0, mobile=9922001], Employee [name=Keith, age=35, salary=4000.0, mobile=3924401]]
希望得到帮助!