Close

Java 8 Streams - Stream.noneMatch Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Method:

boolean noneMatch(Predicate<? super T> predicate)

This terminal operation returns whether no elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determining the result, hence, it is a short circuiting operation. If the stream is empty then true is returned and the predicate is not evaluated.


Examples


package com.logicbig.example.stream;

import java.util.Arrays;
import java.util.stream.Stream;

public class NoneMatchExample {

public static void main(String... args) {
String[] array = {"one", "two", "three", "four"};

Stream<String> stream = Arrays.stream(array);
boolean b = stream.noneMatch(s -> s.startsWith("z"));
System.out.println(b);

Stream<String> stream2 = Arrays.stream(array);
boolean b2 = stream2.noneMatch(s -> s.startsWith("f"));
System.out.println(b2);
}
}

Output

true
false




package com.logicbig.example.stream;

import java.util.stream.Stream;

public class NoneMatchExample2 {

public static void main(String... args) {
boolean b = Stream.empty()
.noneMatch(e -> false);
System.out.println(b);
}
}

Output

true




This terminal-short-circuiting operation of Stream interface, returns 'false' when the very first element matches the provided predicate and terminating the rest of the traversal.

package com.logicbig.example;


import java.util.stream.Stream;

public class NoneMatchExample {
public static void main (String[] args) {
Stream<String> stream = Stream.of("one", "two", "three", "four");
boolean match = stream.noneMatch(s -> s.length() > 0 &&
Character.isUpperCase(s.charAt(0)));
System.out.println(match);
}
}

Output

true
Original Post




See Also