I have some object Tag
public class Tag {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
return this;
}
}
And have two lists List<Tag> firstList
and List<Tag> secondList
I have to compare them only by name and create thirdList
which will contain unique objects from the firstList
.
How can I do this by means of stream?
SOLUTION:
List<Tag> thirdList = firstList.stream()
.filter(f -> secondList.stream()
.noneMatch(t -> t.getName().equals(f.getName())))
.collect(Collectors.toList());