简体   繁体   English

如何组合两个流?

[英]How to combine two streams?

I'm trying to learn/understand streams in java and have this piece of code: 我正在尝试学习/理解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);
       });

Please, give me a tip how I can combine two streams into one? 请给我一个提示,告诉我如何将两个流组合成一个流?

------- Added 23.08.2018 -------- -------已添加23.08.2018 --------
If we get rid of ids variable it will improve a bit performance and code below perform as we work with Set <Integer> tagsIds , so no duplicates (eg if tagIds contains values (5,6,7,8,5,6,7) it will work only with (5,6,7,8)). 如果我们摆脱ids变量,它将改善一点性能和下面的代码,因为我们使用Set <Integer> tagsIds ,所以没有重复(例如,如果tagIds包含值(5,6,7,8,5,6,7) )它只适用于(5,6,7,8))。 Below the modified code: 修改后的代码下面:

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);
       });

This modification has disadvantages like the complexity of reading and debugging code 这种修改具有诸如读取和调试代码的复杂性之类的缺点

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());

First of all, if you have enough data working with a Set will be faster, I assume a Tag can't have duplicate ids... And you can do everything in a few steps: 首先,如果你有足够的数据使用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);

The Tag class assumed as Tag类假定为

class Tag {
    Integer id;

    Integer getId() {
        return id;
    }

    Tag(Integer id) {
        this.id = id;
    }
}

One way to improve the code may be looking for something like:- 改进代码的一种方法可能是寻找类似的东西: -

List<Integer> ids = tags.stream().map(Tag::getId).collect(Collectors.toList());
tagIds.stream().filter(tagId -> !ids.contains(tagId))
               .map(Tag::new)
               .forEach(tags::add);

Unless your Tag class is comparable based on the id and then you can actually just use a single stream as - 除非您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM