Saturday, August 10, 2019

Java 8 IntStream findFirst() Method Example

1. Overview

In this tutorial, We'll learn Java 8 IntStream API findFirst() method usage along with example programs. IntStream is part of java.util package.

We've already seen many methods of IntStream API.

This is used to get the first element from the stream. In this article, We will see its syntax, example program, and internal implementation.

API Note:

Returns an OptionalInt describing the first element of this stream, or an empty OptionalInt if the stream is empty. If the stream has no encounter order, then any element may be returned.



2. findFirst() Method Example


This method returns the first element from the integer stream. Returns OptionalInt object which holds the value of the first element.

Syntax:

OptionalInt findFirst()

Returns OptionalInt object.

This is part of short-circuiting terminal operation.

If the stream is empty then it returns empty OptionalInt object.

Example:

package com.java.w3schools.blog.java8.stream.intstream;

import java.util.OptionalInt;
import java.util.stream.IntStream;

/**
 * 
 * Java 8 IntStream findFirst() Method Example
 * 
 * @author venkateshn
 *
 */
public class IntStreamFindFirstExample {

 public static void main(String[] args) {

  IntStream values = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
  OptionalInt firstValue = values.findFirst();

  if (firstValue.isPresent()) {
   System.out.println("first value : " + firstValue.getAsInt());
  } else {
   System.out.println("No value is present");
  }

 }
}

Output:

first value : 1

3. Empty If IntStream is empty


API note saying if the stream is empty then it returns empty OptionalInt instance. We will demonstrate with the below example.

package com.java.w3schools.blog.java8.stream.intstream;

import java.util.OptionalInt;
import java.util.stream.IntStream;

/**
 * 
 * Java 8 empyt IntStream findFirst() Method Example
 * 
 * @author venkateshn
 *
 */

public class FindFirstEmptyExample {

 public static void main(String[] args) {
  IntStream emptyStream = IntStream.empty();
  OptionalInt emptyOptionalInt = emptyStream.findFirst();

  if (emptyOptionalInt.isPresent()) {
   System.out.println("Value is present");
  } else {
   System.out.println("First value is not present because stream is blank.");
  }

 }
}

Output:

First value is not present because stream is blank.

4. findFirst() to get the first even number


package com.java.w3schools.blog.java8.stream.intstream;

import java.util.OptionalInt;
import java.util.stream.IntStream;

/**
 * 
 * Java 8 empyt IntStream findFirst() Method Example
 * 
 * @author venkateshn
 *
 */

public class FindFirstParrellExample {

 public static void main(String[] args) {
  IntStream values = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).parallel();
  OptionalInt optionalInt = values.filter(value -> value % 2 == 0).findFirst();

  if (optionalInt.isPresent()) {
   System.out.println("First value is " + optionalInt.getAsInt());
  } else {
   System.out.println("First value is not present because stream is blank.");
  }

 }
}

Output:

First value is 2

5. Conclusion


In this article, We've seen how to get the first value from the IntStream using findFirst().

Many example programs are shown on findFirst() method.

This method returns OptionalInt object and if stream empty then it returns empty OptionalInt object.

All examples shown in this article are over GitHub.
API Ref


No comments:

Post a Comment