Find the difference between two collections in Java 8?

I am trying to make a List all books in one Collection that are not in another. My problem is that I need to compare based on the book id, so I canโ€™t just check if the first book is in the second, I have to determine if any book in the second collection has the same identifier as the book in the first one.

I have the code below to compare two collections of books and filter the first collection:

 List<Book> parentBooks = listOfBooks1.stream().filter(book-> !listOfBooks2.contains(book)).collect(Collectors.toList()); 

The code does not work correctly because I am comparing the objects themselves. I need to compare objects based on bookId, not the entire book object. How can I change the code so that it can perform a comparison based on bookId (book.getId ())?

+5
source share
4 answers
 List<Book> books1 = ...; List<Book> books2 = ...; Set<Integer> ids = books2.stream() .map(Book::getId) .collect(Collectors.toSet()); List<Book> parentBooks = books1.stream() .filter(book -> !ids.contains(book.getId())) .collect(Collectors.toList()); 
+11
source
 boolean test = book1.stream().allMatch(t->book2.contains(t)); 
0
source

The problem is complex, but it comes down to one thing, it knows your data. Is it imutables, objects with id, duplicate entries, etc.? The code below works for immutable values โ€‹โ€‹with single values โ€‹โ€‹(and with possible duplicates). First, he tries to delete all entries in the previous list (from the copied list after the list). It remains to add the added elements. Those from the list that can be removed from the list are immutable. The rest are deleted.

 package scrap; import java.util.ArrayList; import java.util.List; public class ListDiffer<T> { private List<T> addedList = new ArrayList<>(); private List<T> unchangedList = new ArrayList<>(); private List<T> removedList = new ArrayList<>(); public ListDiffer(List<T> beforeList, List<T> afterList) { addedList.addAll(afterList); // Will contain only new elements when all elements in the Before-list are removed. beforeList.forEach(e -> { boolean b = addedList.remove(e) ? unchangedList.add(e) : removedList.add(e); }); } public List<T> getAddedList() { return addedList; } public List<T> getUnchangedList() { return unchangedList; } public List<T> getRemovedList() { return removedList; } } 
0
source
 Set<Integer> books1ids = listOfBooks1.stream().map(Book::getId).collect(Collectors.toSet()); Set<Integer> books2ids = listOfBooks2.stream().map(Book::getId).collect(Collectors.toSet()); return books1ids.equals(books2ids); 
-1
source

All Articles