0
List<Student> list = new ArrayList<Student>();
list.add( new Student(3500,5/20/2020) );
list.add( new Student(3500,4/20/2019 );
list.add( new Student(3500,3/20/2018) );
list.add( new Student(3600,6/20/2020) );
list.add( new Student(3600,5/20/2019) );
list.add( new Student(3700,5/20/2020) );
list.add( new Student(3700,5/19/2018) );

public class Student{
 int studnum;
 Date enrolldate;

}

How can I remove elements so that the arraylist contains only those elements with only one of the student numbers and the latest date for that student number? I am using JDK1.7. I am at a secure site and they are not ready to upgrade the JDK version, so removeIf will not be available.

Unique student number and there latest dates:

3500,5/20/2020
3600,6/20/2020
3700,5/20/2020
  • 2
    Welcome to SO! Show us what you've tried so far and where you're stuck. – eol Dec 17 '20 at 10:06
  • 1
    You can build a Map that maintains the latest date for each student ID. (key is student ID, value is the Student). Then you can remove all the elements of the original List which don't appear as values in the Map (or just create a new List - `new ArrayList<>(map.values())`). – Eran Dec 17 '20 at 10:09
  • Tried using removeIf, but it is not available in JDK 1.7 listclasses.removeIf(c -> c.getExpirationDate().before(now.getTime())); – Sharon Ramsey Dec 17 '20 at 10:41

0 Answers0