I am trying to remove duplicates from a list of Student Objects based on multiple properties while preserving the order,As shown below i have list of student objects where we have multiple students with same name with varying attendence...i need to remove the duplicate student with same name and having studentAttendence as 100 while preserving the order.
Student{studentId=1, studentName='Sam', studentAttendence=100, studentAddress='New York'}
Student{studentId=2, studentName='Sam', studentAttendence=50, studentAddress='New York'}
Student{studentId=3, studentName='Sam', studentAttendence=60, studentAddress='New York'}
Student{studentId=4, studentName='Nathan', studentAttendence=40, studentAddress='LA'}
Student{studentId=5, studentName='Ronan', studentAttendence=100, studentAddress='Atlanta'}
Student{studentId=6, studentName='Nathan', studentAttendence=100, studentAddress='LA'}
desire output After removing the duplicates:
Student{studentId=2, studentName='Sam', studentAttendence=50, studentAddress='New York'}
Student{studentId=3, studentName='Sam', studentAttendence=60, studentAddress='New York'}
Student{studentId=4, studentName='Nathan', studentAttendence=40, studentAddress='LA'}
Student{studentId=5, studentName='Ronan', studentAttendence=100, studentAddress='Atlanta'}
What i have right now is only removing duplicates based on the name and not considering the percentage(100)...and also not preserving the order..any help is greatly appreciated.(Student supplier is a simple supplier function of list of students)
studentsSupplier.get().stream()
.sorted(Comparator.comparing(Student::getStudentName))
.collect(Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Student::getStudentName))), ArrayList::new));
Note: Only duplicate records with studentName matching and percentage 100 must be removed,(Record Ronon has percentage 100 but there is no duplicate with the same studentname so that must not be removed)