LongStream min() Method in Java



The min() method of the LongStream class in Java returns an OptionalLong describing the minimum element of this stream, or an empty optional if this stream is empty.

The syntax is as follows:

OptionalLong min()

Here, OptionalLong is a container object which may or may not contain a long value.

To use the LongStream class in Java, import the following package:

import java.util.stream.LongStream;

The following is an example to implement LongStream min() method in Java. The isPresent() method of the OptionalLong class returns true if the value is present:

Example

 Live Demo

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(20000L, 15000L, 11000L, 74000L, 50000L);
      // Get the minimum element
      OptionalLong res = longStream.min();
      System.out.println("Minimum element...");
      if (res.isPresent()) {
         System.out.println(res.getAsLong());
      } else {
         System.out.println("Nothing!");
      }
   }
}

Output

Minimum element...
11000
Updated on: 2019-07-30T22:30:25+05:30

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements