I have one list which contains some String values. I want to iterate the list comparing with another String. Only if another String doesn't match with any element in the list, then I should enter the loop. I tried something like below, but it didn't worked. Any other alternate approach to do the same in Java 8?
Note: In the loop I'm adding some more elements to the same list. Hence, to avoid ConcurrentModificationException, I'm using a if-condition for my validation.
List<String> mylist = new ArrayList<>();
mylist.add("test");
mylist.add("test1");
if(mylist.stream()
.filter(str -> !(str.equalsIgnoreCase("test")))
.findFirst()
.isPresent()) {
System.out.println("Value is not Present");
}
Stream.noneMatch(). docs.oracle.com/javase/8/docs/api/java/util/stream/…