I want to filter by the object field
List<Book> = Arrays.asList<book1, book2, book3>;
class Book {
String author;
List<Language>;
}
class Language {
String locale;
int code;
}
Book book1 = new Book("author1", Arrays.asList(new Language("en", 1), new Language("fr", 3)));
Book book2 = new Book("author2", Arrays.asList(new Language("ca", 2), new Language("fr", 3)));
Book book3 = new Book("author3", Arrays.asList(new Language("en", 1)));
I want to have a list of the books which not contain Language en.
The result should be :
List<Book> = Arrays.asList<book1, book2>;
Book book1 = new Book("author1", Arrays.asList(new Language("fr", 3)));
Book book2 = new Book("author2", Arrays.asList(new Language("ca", 2), new Language("fr", 3)));
I tried to do this : but it not remove 'en' from book1
return books
.stream()
.filter(book -> book.getLanguages()
.stream()
.allMatch(value -> !"en".equals(value.getLanguage())))
.collect(Collectors.toList());
First you can use filter
to check any Language
object has value of en
and then use findFirst
to get the matching Language
object
return books.stream()
.filter(book -> !book.getLanguages()
.stream()
.filter(value -> "en".equals(value.getLanguage()))
.findFirst().isPresent())
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With