我确实有一个用例,需要在模型层中附加一个带有version(name\u version)的字符串(name)。但这只适用于列表中重复的名称。
学生.java
private class Student{
private String name;
private String value;
}
测试.java
public class NewTes {
public static void main(String[] args){
Student s1 = new Student("xyz","a1");
Student s2 = new Student("abc","a2");
Student s3 = new Student("xyz","a3");
List<String> l2 = new ArrayList<>();
List<Student> l1 = new ArrayList<Student>();
l1.add(s1);
l1.add(s2);
l1.add(s3);
//Get only names from the list
l1.stream().forEach(e -> l2.add(e.getName()));
// Output is
//{"xyz","abc","xyz"}
//Finding only the duplicate ones
Set<String> result = l2.stream().filter(i -> Collections.frequency(l2, i) > 1).collect(Collectors.toSet());
//Output is
//{"xyz"}
//Not sure how to proceed from here
l1.stream().map(e -> e.getName()).flatMap(x -> result.contains(x) ? Stream.of(x + ))
//expected output
//{"xyz_a1", "abc" , "xyz_a3"}
}
}
2条答案
按热度按时间vvppvyoh1#
使用您之前的问题列表。。下面应该给你想要的结果-
sbdsn5lh2#
重写是个好主意
equals
以及hashCode
在你们班上。如果你这样做是为了比较name
你不需要Set
收集复制品。你可以这样做:印刷品