[英]How to combine two streams?
我正在尝试学习/理解java中的流并拥有这段代码:
List <Tag> tags = (classA.getTags() != null ? classA.getTags() : new ArrayList<>());
List <Integer> tagsIds = new ArrayList<>(Arrays.asList(1,2,3,4));
List<Integer> ids = tags.stream().map(Tag::getId).collect(Collectors.toList());
tagIds.stream()
.filter(tagId -> !ids.contains(tagId))
.forEach(tagId -> {
Tag tag = new Tag();
tag.setId(tagId);
tags.add(tag);
});
请给我一个提示,告诉我如何将两个流组合成一个流?
-------已添加23.08.2018 --------
如果我们摆脱ids
变量,它将改善一点性能和下面的代码,因为我们使用Set <Integer> tagsIds
,所以没有重复(例如,如果tagIds
包含值(5,6,7,8,5,6,7) )它只适用于(5,6,7,8))。 修改后的代码下面:
List <Tag> tags = (classA.getTags() != null ? classA.getTags() : new ArrayList<>());
List <Integer> tagIds = new ArrayList<>(Arrays.asList(5,6,7,8,5,6,7));
tagIds.stream()
.filter(tagId -> !tags.stream().map(Tag::getId).collect(Collectors.toList()).contains(tagId))
.forEach(tagId -> {
Tag tag = new Tag();
tag.setId(tagId);
tags.add(tag);
});
这种修改具有诸如读取和调试代码的复杂性之类的缺点
List<Tag> combinedTags = Stream
.concat( // combine streams
tags.stream(),
tagIds.stream().map(Tag::new) // assuming constructor with id parameter
)
.distinct() // get rid of duplicates assuming correctly implemented equals method in Tag
.collect(Collectors.toList());
首先,如果你有足够的数据使用Set
会更快,我认为一个Tag
不能有重复的id ...你可以通过几个步骤完成所有事情:
tagIds.removeAll(ids);
// assuming there is a Tag constructor that takes an Integer
List<Tag> newTags = tagIds.stream().map(Tag::new).collect(Collectors.toList())
tags.addAll(newTags);
Tag
类假定为
class Tag {
Integer id;
Integer getId() {
return id;
}
Tag(Integer id) {
this.id = id;
}
}
改进代码的一种方法可能是寻找类似的东西: -
List<Integer> ids = tags.stream().map(Tag::getId).collect(Collectors.toList());
tagIds.stream().filter(tagId -> !ids.contains(tagId))
.map(Tag::new)
.forEach(tags::add);
除非您的Tag
类基于id
具有可比性,否则您实际上只需使用单个流 -
List<Tag> tags = Arrays.asList(new Tag(5),new Tag(6)); //example
List <Integer> tagIds = Arrays.asList(1,2,3,4); // from question
tagIds.stream().map(Tag::new)
.filter(tag -> !tags.contains(tag))
.forEach(tags::add);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.