Reputation: 115
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 (e.g. 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
Upvotes: 3
Views: 209
Reputation: 11930
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());
Upvotes: 3
Reputation: 121028
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);
Upvotes: 2
Reputation: 32036
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);
Upvotes: 1