Java Stream findAny()
May 13, 2020
The findAny
method of Stream
selects any element in this stream. The behavior of findAny
method is non-deterministic and it is free to select any element in this stream. The findAny
method is useful to get maximal performance in parallel operations but it is not guaranteed to get same result for every invocation.
Find the method declaration from Java doc.
Optional<T> findAny()
java.util.Optional
, a container object which may or may not contain a non-null value.
The
findAny
throws NullPointerException
when the method selects null value in the stream.
If we want to select first element every time, use
findFirst()
method of Stream
.
Now find the
findAny
examples.
Example-1
Suppose we have a stream of some integers.
Stream.of(10, 20, 30).findAny() .ifPresent(s -> System.out.println(s));
findAny
method, it is free to select any element in the stream, it means findAny
can give output either 10 or 20 or 30.
Find one more code.
FindAnyDemo1.java
package com.concretepage; import java.util.Arrays; import java.util.List; public class FindAnyDemo1 { public static void main(String[] args) { List<String> list = Arrays.asList("Mahesh", "Suresh", "Mohit"); String output = list.stream() .filter(e -> e.startsWith("M")) .findAny() .orElse("NA"); System.out.println(output); List<Integer> numList = Arrays.asList(21, 22, 23, 24); numList.stream() .filter(n -> n % 2 == 0) .findAny() .ifPresent(e -> System.out.println(e)); } }
Mahesh 22
list.stream().filter(e -> e.startsWith("M"))
findAny
is free to select any of them. So if we run example many times, output is not necessarily same everytime.
Example-2
Find the example of
findAny
method using IntStream
, LongStream
and DoubleStream
.
FindAnyDemo2.java
package com.concretepage; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; public class FindAnyDemo2 { public static void main(String[] args) { IntStream intStream = IntStream.of(10, 20, 30, 40); intStream.filter(i -> i > 20).findAny() .ifPresent(i -> System.out.println(i)); LongStream longStream = LongStream.of(100, 200, 300); longStream.filter(l -> l < 250).findAny() .ifPresent(l -> System.out.println(l)); DoubleStream doubleStream = DoubleStream.of(100.52, 200.55, 300.66); doubleStream.filter(d -> d > 200).findAny() .ifPresent(l -> System.out.println(l)); } }
30 100 200.55
Example-3
The
findAny
method boosts the performance of parallel stream but we know that output may vary for re-run.
FindAnyDemo3.java
package com.concretepage; import java.util.ArrayList; import java.util.List; public class FindAnyDemo3 { public static void main(String[] args) { List<Employee> list = new ArrayList<>(); list.add(new Employee("Emp A", 3000)); list.add(new Employee("Emp B", 4000)); list.add(new Employee("Emp C", 5000)); list.add(new Employee("Emp D", 6000)); list.parallelStream() .filter(e -> e.getSal() >= 4000 && e.getSal() <= 5000) .mapToInt(e -> e.getSal()) .findAny() .ifPresent(s -> System.out.println(s)); } } class Employee { private String name; private int sal; public Employee(String name, int sal) { this.name = name; this.sal = sal; } //Sets and Gets }
5000
Example-4
The
findAny
method throws NullPointerException
if it selects null value.
FindAnyDemo4.java
package com.concretepage; import java.util.stream.Stream; public class FindAnyDemo4 { public static void main(String[] args) { Stream.of(null, "AA"). findAny().ifPresent(s -> System.out.println(s)); } }
NullPointerException
.