Java 8 - LongSupplier interface example

Posted on December 30, 2016


java.util.function.LongSupplier is a functional interface whose functional method is  long getAsLong(). The LongSupplier interface represents an operation that takes no arguments and returns a result of long type.

The following example shows how to use the long getAsLong () method of the LongSupplier interface with lambda expression.

LongSupplierExample.java

package com.boraji.tutorial.lambda;

import java.util.function.LongSupplier;

/**
 * @author imssbora
 */
public class LongSupplierExample {
  public static void main(String[] args) {
    LongSupplier supplier = () -> 10l;

    System.out.println(supplier.getAsLong());

    double value = 451.456;
    LongSupplier supplier2 = () -> new Double(value).longValue();

    System.out.println(supplier2.getAsLong());

  }
}

Output

10
451