Stream
offindFirst
Methods look for the first element in this stream asOptional
。
If there are no elements in the stream,findFirst
Return to emptyOptional
。
If the streams have no order,findFirst
Any element can be selected.
iffindFirst
The selected element isnull
, it will throwNullPointerException
。
findFirst declaration in javadoc
Optional<T> findFirst()
return: findFirst method returnsOptional
The element containing the first element in the stream.
abnormal: If a null value is selected, findFirst will throw a NullPointerException.
findFirst is a short-circuit terminal operation (short-circuiting terminal operation
), a stream operation is a set of intermediate operations and terminal operations. If the intermediate operation can generate finite current for infinite input, it is a short circuit (short-circuiting
)。
Let's take a look at a set of examples
Example 1: Suppose we have an integer stream and call itfindFirst
method.
Stream.of(50, 60, 70).findFirst()
.ifPresent(s -> System.out.println(s));
The output is 50, which is the first element of the stream.
Extended
package com.concretepage;
import java.util.Arrays;
import java.util.List;
public class FindFirstDemo1 {
public static void main(String[] args) {
List<String> list = Arrays.asList("Vijay", "Suresh", "Vinod");
String output = list.stream()
.filter(e -> e.startsWith("V")) // Vijay, Vinod
.findFirst() //Vijay
.orElse("NA");
System.out.println(output);
List<Integer> numList = Arrays.asList(31, 32, 33, 34);
numList.stream()
.filter(n -> n % 2 == 0) // 32, 34
.findFirst() //32
.ifPresent(e -> System.out.println(e));
}
}
Output
Vijay
32
Example 2: The following is the useIntStream
、LongStream
andDoubleStream
offindFirst
Example of method.
package com.concretepage;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class FindFirstDemo2 {
public static void main(String[] args) {
IntStream intStream = IntStream.of(10, 20, 30, 40);
intStream.filter(i -> i > 20).findFirst()
.ifPresent(i -> System.out.println(i));
LongStream longStream = LongStream.of(100, 200, 300);
longStream.filter(l -> l < 250).findFirst()
.ifPresent(l -> System.out.println(l));
DoubleStream doubleStream = DoubleStream.of(100.52, 200.55, 300.66);
doubleStream.filter(d -> d > 200).findFirst()
.ifPresent(l -> System.out.println(l));
}
}
Output
30
100
200.55
Example 3: Below is a findFirst example with parallel streams.
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
public class FindFirstDemo3 {
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())
.findFirst()
.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
}
Output
4000
Example 4: Below is a findFirst example with null values.
package com.concretepage;
import java.util.stream.Stream;
public class FindFirstDemo4 {
public static void main(String[] args) {
Stream.of(null, "A").
findFirst().ifPresent(s -> System.out.println(s));
}
}
The output will be NullPointerException.
References
【1】Java doc: Stream
【2】Java Stream findFirst()