To find any element from the collection of elements, java provides findAny() method in Stream API.
Collection can be any list, set or map, and you just need to call the method and get the element.
The findAny() method returns an Optional value, See the running example below.
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* stream findany() method
*/
public class JExercise {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12,56,12,58,1,23);
System.out.println(list);
Optional<Integer> result = list.stream().parallel().findAny();
int element = result.get();
System.out.println(element);
}
}
Output:
[12, 56, 12, 58, 1, 23]
58
The output is not fix, it may be any value of the collection. It returns random value even in the parallel operation.
See one more example, where the method applied on non-parallel stream.
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* stream findany() method
*/
public class JExercise {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12,56,12,58,1,23);
System.out.println(list);
Optional<Integer> result = list.stream().findAny();
int element = result.get();
System.out.println(element);
}
}
Output:
[12, 56, 12, 58, 1, 23]
12
Now, let's see the method signature:
Optional<T> findAny()
It returns an Optional having elements of the stream, can be empty if the stream is empty.
The behavior of this method is explicitly nondeterministic, and it is free to select any element in the stream.
It throws NullPointerException if the selected element is null.
Let's see some more running examples.
You can use this method with fileter() as well to find the elements in the new filtered stream. See the below code.
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* stream findany() method
*/
public class JExercise {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12,56,12,58,1,23);
System.out.println(list);
Optional<Integer> result = list.stream().filter(x -> x > 25).findAny();
int element = result.get();
System.out.println(element);
}
}
Output:
[12, 56, 12, 58, 1, 23]
56
It returns empty Optional if no element is found. See the below java code.
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/*
* stream findany() method
*/
public class JExercise {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12,56,12,58,1,23);
System.out.println(list);
Optional<Integer> result = list.stream().filter(x -> x > 100).findAny();
//int element = result.get();
System.out.println(result);
}
}
Output:
[12, 56, 12, 58, 1, 23]
Optional.empty
To get a default value in case of empty Optional, we can use the orElse() method as well.
This will help to avoid any exception due to null pointer element. See the below java code.
/*
* stream findany() method
*/
import java.util.Arrays;
import java.util.List;
public class JExercise {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12,56,12,58,1,23);
System.out.println(list);
int element = list.stream().filter(x -> x > 100).findAny().orElse(0);
System.out.println(element);
}
}
Output:
[12, 56, 12, 58, 1, 23]
0