Using java8 how can we sort elements starting from the second element to till last?
list.stream().filter(student -> student.getAge()!=15).sorted(Comparator.comparing(student->student.getAge())).collect(Collectors.toList());
The final sorted list should contain all the elements including first one.
I have tried above code snippet:
Here i dont want to touch first element.The above code is sorting from 2nd element to till last element.the problem is i am not getting first element(age =15) in output.
If i dont apply filter it will sort all the elements and i will loose first element, which should not happen.
Please help me on this.Thanks in advance.
List<Student> result = Stream.concat(
list.stream().findFirst().map(Stream::of).orElse(Stream.empty()),
list.stream().skip(1).sorted(Comparator.comparing(Student::getAge)))
.collect(Collectors.toList());
System.out.println(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With