3 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
你可以利用toMap收集器,因?yàn)樗幸粋€(gè)合并功能,可以加入你的重復(fù)對(duì)象,例如我會(huì)在每次找到重復(fù)對(duì)象時(shí)創(chuàng)建一個(gè)新對(duì)象,但你可以只修改現(xiàn)有對(duì)象
static User join(User a, User b) {
return new User(a.getName(), a.getEmail(), a.getAge(), a.getGroup() + "," + b.getGroup());
}
和流操作。
List<User> collect = users.stream()
.collect(Collectors.collectingAndThen(Collectors.toMap(User::getEmail,
Function.identity(),
(a, b) -> join(a, b)),
map -> new ArrayList<>(map.values())));

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
你可以簡單地做:
List<User> sortedUsers = new ArrayList<>();
// group by email-id
Map<String, List<User>> collectMap =
users.stream().collect(Collectors.groupingBy(User::getEmail));
collectMap.entrySet().forEach(e -> {
String group = e.getValue().stream() // collect group names
.map(i -> i.getGroup())
.collect(Collectors.joining(","));
User user = e.getValue().get(0);
sortedUsers.add(new User(user.getName(), user.getEmail(), user.getAge(), group));
});
輸出:
[
User [name=John, email=list@test, age=21, group=n2],
User [name=Max, email=test@test, age=20, group=n1,n5],
User [name=Nancy, email=must@test, age=22, group=n3,n4]
]
確保添加 getter 和 setter,同時(shí)覆蓋toString()User 的。

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是您需要的工作示例(我希望 :))。
它將前 3 個(gè)字段的組合視為唯一鍵。然后它遍歷列表并根據(jù)鍵將用戶添加到映射并將組作為值。我使用 Map 是因?yàn)樗梢约涌鞕z索速度。在插入新用戶之前,我檢查它是否已經(jīng)在地圖中。如果是,那么我附加新組。如果不是,我將其插入當(dāng)前組。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class User {
public String name;
public String email;
public Integer age;
public String group;
public static final void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User("Max", "test@test", 20, "n1"));
users.add(new User("John", "list@test", 21, "n2"));
users.add(new User("Nancy", "must@test", 22, "n3"));
users.add(new User("Nancy", "must@test", 22, "n4"));
users.add(new User("Max", "test@test", 20, "n5"));
List<User> filtered = filter(users);
filtered.stream().forEach(System.out::println);
}
public User() {
}
public User(String key, String group) {
String[] keys = key.split("-");
this.name = keys[0];
this.email = keys[1];
this.age = Integer.parseInt(keys[2]);
this.group = group;
}
public User(String name, String email, Integer age, String group) {
this.name = name;
this.email = email;
this.age = age;
this.group = group;
}
public String toString() {
return name + " : " + email + " : " + " : " + age + " : " + group;
}
public String getUniqueKey() {
return name + "-" + email + "-" + age;
}
public static List<User> filter(List<User> users) {
Map<String, String> uniqueGroup = new HashMap<>();
for (User user : users) {
String found = uniqueGroup.get(user.getUniqueKey());
if (null == found) {
uniqueGroup.put(user.getUniqueKey(), user.group);
} else {
uniqueGroup.put(user.getUniqueKey(), found + ", " + user.group);
}
}
List<User> newUsers = new ArrayList<>();
for (String key : uniqueGroup.keySet()) {
newUsers.add(new User(key, uniqueGroup.get(key)));
}
return newUsers;
}
}
添加回答
舉報(bào)