【问题标题】:Streams and Lambda expressions sorted descendingly [duplicate]流和 Lambda 表达式按降序排序 [重复]
【发布时间】:2018-09-10 05:56:20
【问题描述】:

您好,我使用 Lambda 表达式对birthYear 进行降序排序,但它不起作用。你能帮我吗 ?你能检查我在以下代码中的位置吗:

final static List<Student> students = Arrays.asList(
        new Student("Joe","Clay",1994,Gender.MALE),
        new Student("Marie","Smith",1997,Gender.FEMALE),
        new Student("Ann","Thompson",2000,Gender.FEMALE),
        new Student("James","Bond",1989,Gender.MALE),
        new Student("Jennifer","Atkins",1995,Gender.FEMALE),
        new Student("Cristina","Gibbs",1999,Gender.FEMALE),
        new Student("Jason","Clark",1998,Gender.MALE),
        new Student("Kate","Barrett",1992,Gender.FEMALE),
        new Student("Peter","Garner",1999,Gender.MALE),
        new Student("Ben","Walsh",1996,Gender.MALE)
    );

 public static void runExercise8() {
        List<Integer> years = students
            .stream()
            .sorted(Comparator.comparing(Student::getBirthYear).reverseOrder())
            .collect(Collectors.toList());
       System.out.println(years);
    }

【问题讨论】:

  • 请解释"doesn't work"
  • 要澄清您的问题(包括问题描述,如错误消息或无效结果与预期结果),请使用edit 选项。

标签: java lambda java-8


【解决方案1】:

考虑到接收器类型,您似乎希望将结果作为表示排序后年份的整数列表:

  List<Integer> years

因此,要获得预期的结果,您需要这样做:

List<Integer> years = 
         students.stream()
                 .sorted(Comparator.comparing(Student::getBirthYear).reversed())
                 .map(s -> s.getBirthYear()) // transform to integers
                 .collect(Collectors.toList());

另外,请注意使用 reversed() 而不是 reverseOrder()

如果这不是本意,那么接收者类型以及查询应该是:

List<Student> resultSet = 
         students.stream()
                 .sorted(Comparator.comparing(Student::getBirthYear).reversed())                   
                 .collect(Collectors.toList());

顺便说一句,在这种情况下,最好使用Comparator.comparingInt 而不是Comparator.comparing,因为前者效率更高。

示例:

.sorted(Comparator.comparingInt(Student::getBirthYear).reversed())

【讨论】:

    【解决方案2】:

    我想这里的根本问题是您试图获取整数列表List&lt;Integer&gt;,但您对学生进行排序。您需要在排序后添加.map(Student::getBirthYear)以获得整数或将List&lt;Integer&gt;更改为List&lt;Student&gt;

    【讨论】:

      【解决方案3】:

      我建议你像这样使用流:

      List<Integer> years = students
                  .stream()
                  .map(s -> s.getBirthYear())
                  .sorted((Integer s1, Integer s2) -> -Integer.compare(s1, s2))
                  .collect(Collectors.toList());
      

      它将首先映射年份,然后按降序对其进行排序。 首先映射年份会给你更好的表现。

      您的代码的问题是 Comparator.comparing 不起作用,您期望一个 Integer-List 但没有返回整数列表,而是返回学生列表。所以你必须映射它。

      正确的 Comparator.comparing 应该是:

      .sorted(Comparator.comparing((Student s) -> s.getBirthYear()).reversed())
      

      【讨论】:

        【解决方案4】:

        如果要获取学生列表,只需使用Comparators.reverse() 方法即可:

                List<Student> sortedStudents = students
                    .stream()
                    .sorted(Comparator.comparing(Student::getBirthYear).reversed())
                    .collect(Collectors.toList());
               System.out.println(sortedStudents);
        

        但是,如果您确实需要年份列表,我认为您需要添加一个额外的映射步骤以从 Student 对象中提取它:

               List<Integer> years = students
                    .stream()
                    .sorted(Comparator.comparing(Student::getBirthYear).reversed())
                    .map(Student::getBirthYear)
                    .collect(Collectors.toList());
               System.out.println(years);
        

        或者,您也可以先将学生映射到出生年份并对其进行排序,这可能会使比较步骤稍微简单一些。

        【讨论】:

          猜你喜欢
          • 2013-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-24
          • 2010-12-10
          • 2018-08-13
          • 2016-03-03
          相关资源
          最近更新 更多