Java 8 Stream findFirst() and findAny()
In Java 8 Stream, the findFirst()
returns the first element from a Stream, while findAny()
returns any element from a Stream.
1. findFirst()
1.1 Find the first element from a Stream of Integers.
Java8FindFirstExample1.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class Java8FindFirstExample1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 2, 1);
Optional<Integer> first = list.stream().findFirst();
if (first.isPresent()) {
Integer result = first.get();
System.out.println(result); // 1
} else {
System.out.println("no value?");
}
Optional<Integer> first2 = list
.stream()
.filter(x -> x > 1).findFirst();
if (first2.isPresent()) {
System.out.println(first2.get()); // 2
} else {
System.out.println("no value?");
}
}
}
Output
1
2
1.2 Find the first element from a Stream of String which is not equal to "node".
Java8FindFirstExample2.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class Java8FindFirstExample2 {
public static void main(String[] args) {
List<String> list = Arrays.asList("node", "java", "python", "ruby");
Optional<String> result = list.stream()
.filter(x -> !x.equalsIgnoreCase("node"))
.findFirst();
if (result.isPresent()) {
System.out.println(result.get()); // java
} else {
System.out.println("no value?");
}
}
}
Output
java
2. findAny()
2.1 Find any element from a Stream of Integers. If we run the below program, most of the time, the result is 2, it looks like findAny()
always returns the first element? But, there is no guaranteed for this, findAny()
may return any element from a Stream.
Java8FindAnyExample1.java
package com.mkyong.java8;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class Java8FindAnyExample1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Optional<Integer> any = list.stream().filter(x -> x > 1).findAny();
if (any.isPresent()) {
Integer result = any.get();
System.out.println(result);
}
}
}
Output
2 // no guaranteed
References
About Author
Comments
Subscribe
0 Comments