7

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");
}
2
  • 8
    Your logic is wrong: it tests if at least one element of the list is different from "test". Use Stream.noneMatch(). docs.oracle.com/javase/8/docs/api/java/util/stream/… Commented Mar 23, 2019 at 10:40
  • 3
    The using isPresent method in the title really makes this an XY problem. Look in the link to see how you can improve your next questions on Stack Overflow (and elsewhere). Commented Mar 23, 2019 at 11:00

3 Answers 3

9

You should be using Stream#noneMatch for this. It will make your code more readable and more concise. Also, try to avoid putting to much logic inside of your if statement, extract a max in readable variables

List<String> mylist = new ArrayList<>();
mylist.add("test");
mylist.add("test1");

Predicate<String> equalsIgnoreCasePredicate = str -> str.equalsIgnoreCase("test");
boolean noneMatchString = mylist.stream().noneMatch(equalsIgnoreCasePredicate);

if (noneMatchString) {
    System.out.println("Value is not Present");
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don’t think that there’s “too much logic” inside the if statement when you say if(mylist.stream().noneMatch(str -> str.equalsIgnoreCase("test"))); actually, it’s the creation of the additional variables that make it look like being much code.
@Holger I personally don't care about how many lines of code, I care about time invested in debugging and re-reading code when needed. It it can be readable and shorter, be it. But the if statement wasn't directly readable.
The original statement wasn’t, but mylist.stream().noneMatch(str -> str.equalsIgnoreCase("test")) says more about what is actually tested than noneMatchString. The latter would require rereading more code.
@Holger Fair enough
9

You should use noneMatch()

if (mylist.stream().noneMatch(str -> str.equalsIgnoreCase(testString))) {
    System.out.println("Value is not Present");
}

2 Comments

Correct. No need for filter, findFirst nor the low-level isPresent here.
Or, using a method reference: mylist.stream().noneMatch(testString::equalsIgnoreCase).
-1

The above can be achieved without using the Stream API. Below is a possible solution

String searchValue = "COW";
List<String> list = Arrays.asList("CAT", "DOG");

if(!list.contains(searchValue)){
    System.out.println("Value is not Present");
}

2 Comments

Hard to make work with equalsIgnoreCase. Otherwise a good thought. I understand the downvote (but didn’t do it).
Agreed .. I missed that requirement here .. Thanks for the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.