Sometimes, we search something without going through the whole stream.
findFirst and findAny return an element in the stream if there are any.
Optional<T> findAny
Optional<T> findFirst
allMatch, anyMatch and noneMatch takes a Predicate as parameter, returns true/false.
boolean allMatch(Predicate <? super T> predicate)
boolean anyMatch(Predicate <? super T> predicate)
boolean noneMatch(Predicate <? super T> predicate)
match |
OCPJP>cat SearchDemo.java
import java.util.ArrayList;
import java.util.Optional;
import java.util.Arrays;
import java.util.List;
public class SearchDemo {
public static void main(String... args) {
List<String> list = Arrays.asList("apple", "pineapple", "plum", "Avocado", "orange");
testSearch(list);
list = new ArrayList<String>();
testSearch(list);
}
private static void testSearch(List<String> list) {
System.out.println("================" + list);
Optional<String> findAny = list.stream().findAny();
findAny.ifPresent(System.out::println);
Optional<String> findFirst = list.stream().findFirst();
if(findFirst.isPresent()) {
System.out.println(findFirst.get());
}
boolean allMatch = list.stream().allMatch(i -> i.contains("a"));
System.out.println(allMatch);
boolean anyMatch = list.stream().anyMatch(i -> i.contains("a"));
System.out.println(anyMatch);
boolean noneMatch = list.stream().noneMatch(i -> i.contains("a"));
System.out.println(noneMatch);
}
}
OCPJP>
OCPJP>
OCPJP>
OCPJP>javac SearchDemo.java
OCPJP>java SearchDemo
================[apple, pineapple, plum, Avocado, orange]
apple
apple
false
true
false
================[]
true
false
true
OCPJP>
We are not done yet, we still have to go through the primitive versions.
DoubleStream
OptionalDouble findAny()OptionalDouble FindFirst()
boolean allMatch(DoublePredicate predicate)
boolean anyMatch(DoublePredicate predicate)
boolean noneMatch(DoublePredicate predicate)
IntStream
OptionalInt findAny()OptionalInt findFirst()
boolean allMatch(IntPredicate predicate)
boolean anyMatch(IntPredicate predicate)
boolean noneMatch(IntPredicate predicate)
LongStream
OptionalLong findAny()OptionalLong findFirst()
boolean allMatch(LongPredicate predicate)
boolean anyMatch(LongPredicate predicate)
boolean nonMatch(LongPredicate predicate)
OCPJP>cat PrimitiveSearchDemo.java
import java.util.stream.*;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
public class PrimitiveSearchDemo {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(.1, .2, 1.5);
System.out.println("DoubleStream: findAny, findFirst, allMatch, anyMatch, noneMatch ");
OptionalDouble optionalDouble = doubleStream.findAny();
optionalDouble.ifPresent(System.out::println);
DoubleStream.of(.1, .2, 1.5).findFirst().ifPresent(System.out::println);
System.out.println("DoubleStream.allMatch: " + DoubleStream.of(.1, .2, 1.5).allMatch(d -> d > 0.2));
System.out.println("DoubleStream.anyMatch: " + DoubleStream.of(.1, .2, 1.5).anyMatch(d -> d > 0.2));
System.out.println("DoubleStream.noneMatch: " + DoubleStream.of(.1, .2, 1.5).noneMatch(d -> d > 0.2));
System.out.println("DoubleStream.allMatch: " + DoubleStream.empty().allMatch(d -> d > 0.2));
System.out.println("DoubleStream.anyMatch: " + DoubleStream.empty().anyMatch(d -> d > 0.2));
System.out.println("DoubleStream.noneMatch: " + DoubleStream.empty().noneMatch(d -> d > 0.2));
IntStream intStream = IntStream.of(4, 2, 3);
System.out.println("IntStream: findAny, findFirst, allMatch, anyMatch, noneMatch ");
OptionalInt optionalInt = intStream.findAny();
optionalInt.ifPresent(System.out::println);
IntStream.of(1, 2, 3).sorted().findFirst().ifPresent(System.out::println);
System.out.println("IntStream.allMatch: " + IntStream.of(1, 2, 3).allMatch(i -> i > 3));
System.out.println("IntStream.anyMatch: " + IntStream.of(1, 2, 3).anyMatch(i -> i > 3));
System.out.println("IntStream.noneMatch: " + IntStream.of(1, 2, 3).noneMatch(i -> i > 3));
System.out.println("IntStream.allMatch: " + IntStream.empty().allMatch(i -> i > 3));
System.out.println("IntStream.anyMatch: " + IntStream.empty().anyMatch(i -> i > 3));
System.out.println("IntStream.noneMatch: " + IntStream.empty().noneMatch(i -> i > 3));
LongStream longStream = LongStream.of(4L, 2L, 3L);
System.out.println("LongStream: findAny, findFirst, allMatch, anyMatch, noneMatch");
OptionalLong optionalLong = longStream.findAny();
optionalLong.ifPresent(System.out::println);
LongStream.of(4L, 2L, 3L).findFirst().ifPresent(System.out::println);
System.out.println("LongStream.allMatch: " + LongStream.of(4L, 2L, 3L).allMatch(i -> i > 3L));
System.out.println("LongStream.anyMatch: " + LongStream.of(4L, 2L, 3L).anyMatch(i -> i > 3L));
System.out.println("LongStream.noneMatch: " + LongStream.of(4L, 2L, 3L).noneMatch(i -> i > 3L));
System.out.println("LongStream.allMatch: " + LongStream.empty().allMatch(i -> i > 3L));
System.out.println("LongStream.anyMatch: " + LongStream.empty().anyMatch(i -> i > 3L));
System.out.println("LongStream.noneMatch: " + LongStream.empty().noneMatch(i -> i > 3L));
}
}
OCPJP>
OCPJP>
OCPJP>
OCPJP>javac PrimitiveSearchDemo.java
OCPJP>java PrimitiveSearchDemo
DoubleStream: findAny, findFirst, allMatch, anyMatch, noneMatch
0.1
0.1
DoubleStream.allMatch: false
DoubleStream.anyMatch: true
DoubleStream.noneMatch: false
DoubleStream.allMatch: true
DoubleStream.anyMatch: false
DoubleStream.noneMatch: true
IntStream: findAny, findFirst, allMatch, anyMatch, noneMatch
4
1
IntStream.allMatch: false
IntStream.anyMatch: false
IntStream.noneMatch: true
IntStream.allMatch: true
IntStream.anyMatch: false
IntStream.noneMatch: true
LongStream: findAny, findFirst, allMatch, anyMatch, noneMatch
4
4
LongStream.allMatch: false
LongStream.anyMatch: true
LongStream.noneMatch: false
LongStream.allMatch: true
LongStream.anyMatch: false
LongStream.noneMatch: true
OCPJP>
It is good these methods are very similar.