简体   繁体   中英

How to combine two streams?

I'm trying to learn/understand streams in java and have this piece of code:

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 --------
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)). 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:

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

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 -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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