我有用戶對象的列表,定義如下:Java流:獲得最新版本的用戶記錄
public class User {
private String userId; // Unique identifier
private String name;
private String surname;
private String otherPersonalInfo;
private int versionNumber;
}
public User(String userId, String name, String surname, String otherPersonalInfo, int version) {
super();
this.name = name;
this.surname = surname;
this.otherPersonalInfo = otherPersonalInfo;
this.version = version;
}
}
列表示例:
List<User> users = Arrays.asList(
new User("JOHNSMITH", "John", "Smith", "Some info", 1),
new User("JOHNSMITH", "John", "Smith", "Updated info", 2),
new User("JOHNSMITH", "John", "Smith", "Latest info", 3),
new User("BOBDOE", "Bob", "Doe", "Personal info", 1),
new User("BOBDOE", "Bob", "Doe", "Latest info", 2)
);
我需要一種方法來過濾此列表等我只得到每個用戶的最新版本,即:
{"JOHNSMITH", "John", "Smith", "Latest info", 3},
{"BOBDOE", "Bob", "Doe", "Latest info", 2}
什麼是通過使用Java8 Stream API實現此目的的最佳方法?
您是否嘗試過使用'Comparator'對'list'進行排序? –