0

Hello I use Lambda expressions to sorted birthYear descendingly, but it doesn't work. Can you help ? Can you check where I am in the following code:

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);
    }
Storg
  • 33
  • 8
  • Please explain ["doesn't work"](http://importblogkit.com/2015/07/does-not-work/). – Pshemo Mar 31 '18 at 11:25
  • To clarify your question (to include problem description like error message or invalid results vs expected results) use [edit] option. – Pshemo Mar 31 '18 at 11:34

4 Answers4

4

It seems like you want the result as a list of integers representing the years after sorting considering the receiver type is:

  List<Integer> years

So, to get the expected result you'll need to do:

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

Also, note the use of reversed() as opposed to reverseOrder().

If that is not the intention then the receiver type, as well as the query, should be:

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

As an aside, it's better to use Comparator.comparingInt as opposed to Comparator.comparing in this case as the former is more efficient.

example:

.sorted(Comparator.comparingInt(Student::getBirthYear).reversed())
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

I suppose the root issue here is that you are trying to get list of Integers List<Integer>, but you sort Students. You need to add .map(Student::getBirthYear) after sorted to get Integers or change List<Integer> to List<Student>

1

I suggest you use the stream like this:

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

It will map the years first and than sort it in a descending order. Mapping the years first will give you a better beformance.

The problem with your code was the Comparator.comparing didn't work and you expected an Integer-List but haven't returned a list of integers, but of students. So you must map it.

The right Comparator.comparing would be:

.sorted(Comparator.comparing((Student s) -> s.getBirthYear()).reversed())
watchme
  • 720
  • 1
  • 9
  • 25
1

If you want to get back a list of students, you just want to use the Comparators.reverse() method:

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

However, if you really do want a list of years, I think you'll want to add an extra map step to extract it from the Student objects:

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

Alternatively, you could also map the student to the birth year first and sort these, which might make the comparison step slightly simpler.

hugh
  • 2,237
  • 1
  • 12
  • 25