如何使用Comparator对ArrayList进行排序? [英] How to sort ArrayList using Comparator?
问题描述
我有一个类学生实现一个静态方法
public static Comparator< Student> getCompByName()
返回学生的一个新比较器对象,比较2学生对象
我现在需要通过使用我的函数getCompByName()将学生的ArrayList按'name'排序来测试这个。 b
这是我的Student类中的我的Comparator方法。
public static Comparator< Student> getCompByName()
{
Comparator comp = new Comparator< Student>(){
@Override
public int compare(Student s1,Student s2)
{
return s1.name.compareTo(s2.name);
}
};
return comp;
}
和主要我需要测试
public static void main(String [] args)
{
//这里的TODO代码应用逻辑
/ / --------学生阶级测试-------------------------------------- -----
ArrayList< Student> students = new ArrayList();
学生s1 =新学生(Mike);
学生s2 =新学生(Hector);
学生s3 =新学生(Reggie);
学生s4 =新学生(zark);
students.add(s1);
students.add(s2);
students.add(s3);
students.add(S4);
//使用学生类中的getCompByName()对学生进行排序
任何人告诉我如何使用getCompByName()在我的主要实际按名称排序ArrayList?对比较者来说是新的,并且很难与他们的用法。该方法返回一个比较器,所以我不知道这将如何实现。我知道我需要使用getCompByName()来排序,我只是不知道如何实现它。
href =http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29> Collections.sort (List,Comparator)方法:
Collections.sort(students,Student.getCompByName());
在你的代码中,使用 List $当声明
列表
:
; students = new ArrayList();
您还可以使用 Student []
并将其传递给 ArrayList
构造函数:
public static void main(String [] args){
Student [] studentArr = new Student [] {new student(Mike),new Student(Hector),new Student(Reggie),new学生(zark)};
List< Student> students = new ArrayList< Student>(Arrays.asList(studentArr));
Collections.sort(students,Student.getCompByName());
for(Student student:students){
System.out.println(student.getName());
}
}
这里是全文来源。
I have a Class Student that Implements a static method
public static Comparator<Student> getCompByName()
that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'.
I need to now test this by sorting a students ArrayList by 'name' using my function getCompByName().
Here is my Comparator method in my Student class.
public static Comparator<Student> getCompByName()
{
Comparator comp = new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2)
{
return s1.name.compareTo(s2.name);
}
};
return comp;
}
And Main where I need to test
public static void main(String[] args)
{
// TODO code application logic here
//--------Student Class Test-------------------------------------------
ArrayList<Student> students = new ArrayList();
Student s1 = new Student("Mike");
Student s2 = new Student("Hector");
Student s3 = new Student("Reggie");
Student s4 = new Student("zark");
students.add(s1);
students.add(s2);
students.add(s3);
students.add(S4);
//Use getCompByName() from Student class to sort students
Can anyone show me how I would use the getCompByName() in my main to actually sort the ArrayList by name? Im new to comparators and having a hard time with their usages. The method returns a comparator, so Im not sure how this will be implemented. I know I need to use getCompByName() to sort, im just not sure how to implement it.
Use the Collections.sort(List, Comparator) method:
Collections.sort(students, Student.getCompByName());
Also in your code it would be good to use the List
interface when declaring the List
:
List<Student> students = new ArrayList();
You could also tighten up the code by using a Student[]
and passing it to the ArrayList
constructor:
public static void main(String[] args) {
Student[] studentArr = new Student[]{new Student("Mike"),new Student("Hector"), new Student("Reggie"),new Student("zark")};
List<Student> students = new ArrayList<Student>(Arrays.asList(studentArr));
Collections.sort(students, Student.getCompByName());
for(Student student:students){
System.out.println(student.getName());
}
}
Here is a Gist of the full source.
这篇关于如何使用Comparator对ArrayList进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!