IntStream findAny() with examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report IntStream findAny() returns an OptionalInt (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty OptionalInt if the stream is empty. Syntax : OptionalInt findAny() Where, OptionalInt is a container object which may or may not contain a non-null value and the function returns an OptionalInt describing some element of this stream, or an empty OptionalInt if the stream is empty. Note : findAny() is a terminal-short-circuiting operation of Stream interface. This method returns any first element satisfying the intermediate operations. This is a short-circuit operation because it just needs 'any' first element to be returned and terminate the rest of the iteration. Example 1 : findAny() method on Integer Stream. Java // Java code for IntStream findAny() // which returns an OptionalInt describing // some element of the stream, or an // empty OptionalInt if the stream is empty. import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(6, 7, 8, 9); // Using IntStream findAny() to return // an OptionalInt describing some element // of the stream OptionalInt answer = stream.findAny(); // if the stream is empty, an empty // OptionalInt is returned. if (answer.isPresent()) { System.out.println(answer.getAsInt()); } else { System.out.println("no value"); } } } Output : 6 Note : The behavior of IntStream findAny() operation is explicitly non-deterministic i.e, it is free to select any element in the stream. Multiple invocations on the same source may not return the same result. Example 2 : findAny() method to return the elements divisible by 4, in a non-deterministic way. Java // Java code for IntStream findAny() // which returns an OptionalInt describing // some element of the stream, or an // empty OptionalInt if the stream is empty. import java.util.OptionalInt; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // Creating an IntStream IntStream stream = IntStream.of(4, 5, 8, 10, 12, 16) .parallel(); // Using IntStream findAny(). // Executing the source code multiple times // may not return the same result. // Every time you may get a different // Integer which is divisible by 4. stream = stream.filter(num -> num % 4 == 0); OptionalInt answer = stream.findAny(); if (answer.isPresent()) { System.out.println(answer.getAsInt()); } } } Output : 16 Comment More infoAdvertise with us Next Article Java Stream findAny() with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream java-intstream +2 More Practice Tags : JavaMisc Similar Reads Java Stream findAny() with examples Stream findAny() returns an Optional (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty Optional if the stream is empty. Syntax of findAny()Optional<T> findAny() Parameters1. Optional is a container object which may or may not 4 min read Stream findFirst() in Java with examples Stream findFirst() returns an Optional (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned. Syntax : Optional<T> findF 2 min read std::search_n with example in C++ Prerequisite: std::search std::search_n is an STL algorithm defined inside the header file , which is used to search whether a given element satisfies a predicate (equality if no such predicate is defined ) a given no. of times consecutively with the container elements. It searches the range [first, 4 min read List contains() method in Java with Examples The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not. Example:Java// Java Program to Demonstrate // List contains() Method import java.util.*; class GFG { public static void main (String[] args) { List<Integer> l = new Ar 3 min read Collection contains() method in Java with Examples The contains(Object element) of java.util.Collection interface is used to check whether the element 'element' exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Syntax: Collection.co 3 min read Collections.binarySearch() in Java with Examples java.util.Collections.binarySearch() method is a java.util.Collections class method that returns the position of an object in a sorted list.// Returns index of key in a sorted list sorted in// ascending orderpublic static int binarySearch(List slist, T key)// Returns index of key in a sorted list so 4 min read Like