Here is the scenario, in my source list, it contains all the Users object. Each user object will have id
, event
, and timestamp
. I need to create a destination list to contains all the user object that which have latest timestamp record for each id. Like example below
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.vincent.object.User;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
User u1 = new User("1", "55", dateFormat.parse("2017-10-01 10:11:01.111"));
User u2 = new User("1", "105", dateFormat.parse("2017-10-01 10:11:02.111"));
User u3 = new User("2", "55", dateFormat.parse("2017-10-01 10:11:03.111"));
List<User> sources = new ArrayList<>();
sources.add(u1);
sources.add(u2);
sources.add(u3);
List<User> destination = new ArrayList<>();
// I want my destination array only contains following 2 result:
// u2 and u3 from the source
}
}
How can I approach this?
EDIT: Here is the User class
import java.util.Date;
public class User {
private String id;
private String reason;
private Date date;
public User(String id, String reason, Date date) {
super();
this.id = id;
this.reason = reason;
this.date = date;
}
// getter setter
}