Java Stream How to - Find the first in a Stream and return








Question

We would like to know how to find the first in a Stream and return.

Answer

/*  ww  w.  j av  a 2  s.  c om*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws Exception {
    Arrays.asList("a1", "a2", "a3")
    .stream()
    .findFirst()
    .ifPresent(System.out::println);
  }

}

The code above generates the following result.