英文:
Appending a string when find duplicate using stream
问题
我确实有一个使用情况,在这种情况下,我需要将一个字符串(name)与版本(name_version)附加在一起,而这个版本是我在模型层中拥有的。但这仅适用于列表中重复的名称。
**Student.java**
private class Student {
private String name;
private String value;
}
**Test.java**
public class NewTest {
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);
// 仅获取列表中的名称
l1.stream().forEach(e -> l2.add(e.getName()));
// 输出为
// {"xyz", "abc", "xyz"}
// 仅查找重复的名称
Set<String> result = l2.stream().filter(i -> Collections.frequency(l2, i) > 1).collect(Collectors.toSet());
// 输出为
// {"xyz"}
// 不确定如何继续
l1.stream().map(e -> {
if (result.contains(e.getName())) {
return e.getName() + "_" + e.getValue();
} else {
return e.getName();
}
}).forEach(System.out::println);
// 期望输出
// {"xyz_a1", "abc", "xyz_a3"}
}
}
注意:代码中的翻译并不是逐字的翻译,而是根据语境进行的适当翻译,以确保代码的逻辑和含义得以保留。
英文:
I do have a use case where i need to append a string(name) with version (name_version) which i will have in a model layer. But this is to be done only for the name which are duplicate in the list.
Student.java
private class Student{
private String name;
private String value;
}
Test.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"}
}
}
答案1
得分: 0
使用您之前问题中的列表,以下代码应该可以给您想要的结果 -
l1.stream()
.map(e -> result.contains(e.getName()) ? String.join("_", e.getName(), e.getValue()) : e.getName())
.collect(Collectors.toList());
英文:
using your previous lists from your question.. below should give you desire result -
l1.stream()
.map(e -> result.contains(e.getName())? String.join("_",e.getName(),e.getValue()) : e.getName())
.collect(Collectors.toList());
答案2
得分: 0
这是一个很好的想法,可以在你的类中覆盖 equals
和 hashCode
方法。如果你已经这样做了,以在 name
字段上进行比较,那么你就不需要使用 Set
来收集重复项。你可以按照以下方式实现:
List<String> list = l1.stream()
.map(e -> Collections.frequency(l1, e) > 1 ?
String.join("_", e.getName(), e.getValue()) :
e.getName())
.collect(Collectors.toList());
list.forEach(System.out::println);
输出结果为:
xyz_a1
abc
xyz_a3
英文:
It is a good idea to override equals
and hashCode
in your class. If you had done so to compare on the name
field you would not need the Set
to collect duplicates. You could do it as follows:
List<String> list = l1.stream()
.map(e -> Collections.frequency(l1, e) > 1 ?
String.join("_", e.getName(), e.getValue()) :
e.getName())
.collect(Collectors.toList());
list.forEach(System.out::println);
Prints
xyz_a1
abc
xyz_a3
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论