check if an element is in a list java code example

Example 1: check if a list contains a string java

String search = "A";
for(String str: myList) {
    if(str.trim().contains(search))
       return true;
}
return false;

Example 2: java find if element of list in present in another list

list1.stream()
   .map(Object1::getProperty)
   .anyMatch(
     list2.stream()
       .map(Object2::getProperty)
       .collect(toSet())
       ::contains)

// Example with predicate
Predicate<Object> notInList1 = object -> list1.stream().noneMatch(object::equals);
List<Object> missingInList1 = list2.stream().filter(notInList1).collect(Collectors.toList());

Tags:

Java Example