1

I have a list of Student Objects as below.

Student1  DOB : 12/02/2010
Student2  DOB : 12/03/2010
Student1  DOB : 12/04/2010
Student4  DOB : 
Student2  DOB :
Student3  DOB : 12/01/2010

Student{
String name;
Date dob;
}

I want to group the Students based on the DOB as below.

  1. All Students should be grouped based on the student name.
  2. Students must be in descending order of dob.
  3. Same Students grouped together should be in descending order of dob.
  4. Remaining Student objects must be in same order as it is there in the input list

Like,

    Student1  DOB : 12/04/2010
    Student1  DOB : 12/02/2010
    Student2  DOB : 12/03/2010
    Student2  DOB : 
    Student3  DOB : 12/01/2010
    Student4  DOB : 

I found a matching solution here Sorting and Grouping on a list of objects , but it won't work if dob is null. Is there any work around in java8 using streams?

As per the above link step 1, I have tried

Map<String, Optional<Student>> students = students.stream()
    .collect(Collectors.groupingBy(
             Student::getName,
             Collectors.maxBy(Comparator.comparing(o -> o.getDob()))));

Here getDob() can return null sometimes and throws null pointer exception. Filter won't be a solution as it will totally avoid Students with dob as null.

Tester
  • 47
  • 7
  • What do you want to actually perform a comparison over if the attribute is `null`? `Comparing.nullsLast` types example might help – Naman Sep 14 '20 at 13:19
  • What do you mean by `Students are in descending order of dob.` ? What is the difference between point `3` and `Remaining Student objects` means every groups null DOB elements ? – Eklavya Sep 14 '20 at 13:21
  • @Rono modified the question. Point 3 says in the output it must maintain the order Student1 DOB : 12/03/2010 Student1 DOB : 12/01/2010 – Tester Sep 14 '20 at 13:36
  • @Tester `Comparator.comparing(o -> o.getDob())`. Try replacing this with a method reference: `Comparator.comparing(Student::getDob)` – Zaki Sep 14 '20 at 13:51
  • 2
    What’s the difference between point 2 and 3? And why are you using `maxBy` when you want to get objects in an order? As its name suggests, it will evaluate to the maximum element, in other words, *one* element. – Holger Sep 14 '20 at 14:10
  • By point 2 did you mean groups also sorted by DOB( means by first entry of each group) ? You are grouping by name but want show every group's element sequentially 1st group's element 2nd group's element ..... right ? What happened if a group having null DOB and not null DOB both ? Suppons you have `Student2` entry with null. – Eklavya Sep 14 '20 at 15:17
  • Exactly @Rono. If we have null and not null DOB present, still the names will be grouped together and name with dob should come first. – Tester Sep 15 '20 at 05:18
  • Then what is the meaning of point 4? If everything is supposed to be sorted by DOB, what are the “remaining Student objects” that are supposed to be “in same order as it is there in the input list”? – Holger Sep 15 '20 at 10:58
  • @Holger Here in this example, after grouping and sorting, Student4 remains neither grouped nor sorted. So Student4 should be appended at the end. – Tester Sep 15 '20 at 11:15
  • Since `Student4` has a name, it has a group. It will be the only element of its group, but still, it’s a group and according to [this comment](https://stackoverflow.com/questions/63884675/?noredirect=1#comment112987837_63884675), all groups are supposed to be sorted, so it must come after any group that has at least one student with a DOB. Like `Student3` which is also the sole element of its group, but has to be placed before `Student4` due to having a DOB. So `Student3` and `Student4` are sorted in the result. So there’s no such thing as “not grouped” nor “not sorted”. – Holger Sep 15 '20 at 11:24

0 Answers0