【问题标题】:Sorting collection of objects in java在java中对对象集合进行排序
【发布时间】:2015-12-20 09:07:53
【问题描述】:

我想使用我创建的排序函数按年龄对学生进行排序,但我不确定是否需要在这部分实现比较器/可比较。

我有以下 POJO:

public class Student {

 private String name;
 private int age;

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 interface CollectionsProblem {
    public void sort(Collection<Student> memes, boolean ascending);
}

这是接口的实现:

public class CollectionsProblemImpl implements CollectionsProblem { 
    public void sort(Collection<Student> students, boolean ascending) { 
        /****Need to add some sorting here in order to sort students by age 
          param0 - students: the collection to sort
          param1 - ascending: true if the collection should be sorting in ascending order, otherwise false***
    */ 
    } 
 } 

【问题讨论】:

  • 如果是你自己的排序算法,你可以随心所欲。但是,就地对集合进行排序没有意义:例如,您将永远无法对 HashSet 进行排序,因为 HashSet 元素在集合中没有任何索引。该方法应该将 List 作为参数。或者它应该采用一个集合,但创建一个排序的副本并因此返回一个列表。

标签: java sorting collections


【解决方案1】:

你可以在Student POJO 类中实现Comparable,或者通过实现Comparator 接口来编写一个单独的比较器,比如说

public class StudentComparatorByAge implements  Comparator<Student>  {
@Override
public int compare(Object Obj1 , Object Obj2){
}
}

。然后使用Collections.sort(studentArray);(如果实现Comparable)或Collections.sort(studentArray,StudentComparatorByAgeObj );(如果编写单独的比较器)。您还需要从StudentComparatorByAge 创建StudentComparatorByAgeObj 实例。

您的CollectionsProblem 接口没有意义,因为您不需要编写自己的排序函数——Collections 类已经提供了该函数。比较逻辑是你要担心的。

【讨论】:

    【解决方案2】:

    如果不需要就地排序,我会使用 java 的 8 流:

    students.stream()
                .sorted(comparator)
                .collect(Collectors.toList());
    

    您甚至可以更改界面以允许用户提供自定义收集器:

    public interface CollectionsProblem {
        <R, A> R sort(Collection<Student> students, boolean ascending, Collector<? super Student, A, R> collector);
    }
    

    实施:

    public class CollectionsProblemImpl implements CollectionsProblem {
    public <R, A> R sort(Collection<Student> students, boolean ascending, Collector<? super Student, A, R> collector) {
        final int order = ascending ? 1 : -1;
        Comparator<Student> comparator = new Comparator<Student>() {
            public int compare(Student o1, Student o2) {
                return order * Integer.compare(o1.getAge(), o2.getAge());
            }
        };
    
        return students.stream()
                .sorted(comparator)
                .collect(collector);
    }
    

    及用法:

    Student a = new Student();
    a.setAge(19);
    Student b = new Student();
    b.setAge(18);
    
    List<Student> students = Lists.newArrayList(a, b);
    CollectionsProblem problem = new CollectionsProblemImpl();
    List<Student> sortedList = problem.sort(students, true, Collectors.<Student>toList());
    

    当然,在这种情况下传递,即Collectors.toSet() 没有多大意义。

    【讨论】:

    • 函数定义中的R和A是什么意思:public R sort(Collection memes, boolean ascending, Collector super Meme, A, R> collector )
    • 这些来自 Collector 的 api - 它有 3 个泛型。看一下 Collector 的 javadoc:param &lt;T&gt; The type of input elements for the new collectorparam &lt;A&gt; The intermediate accumulation type of the new collectorparam &lt;R&gt; The final result type of the new collectortoList 收集器的情况下,这些是按顺序排列的:T, ?, List&lt;T&gt;,在你的情况下是T -&gt; Student
    猜你喜欢
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    相关资源
    最近更新 更多