Java 8 - LongToIntFunction interface example

Posted on April 11, 2017


java.util.function.LongToIntFunction is a functional interface whose functional method is int applyAsInt(). The LongToIntFunction is interface represents an operation that takes a single argument of long type and returns a result of int type.

The following example shows how to use the applyAsInt () method of the LongToIntFunction is interface with lambda expression.

LongToIntFunctionExample.java

package com.boraji.tutorial.lambda;

import java.util.function.LongToIntFunction;

/**
 * @author imssbora
 */
public class LongToIntFunctionExample {

   public static void main(String[] args) {
      LongToIntFunction function = (l) -> (int) l;
      System.out.println(function.applyAsInt(1000L));
      System.out.println(function.applyAsInt(-45454L));

      LongToIntFunction function2 = (l) -> String.valueOf(l).length();
      System.out.println(function2.applyAsInt(454545L));
      System.out.println(function2.applyAsInt(Long.MAX_VALUE));
   }
}

Output

1000
-45454
6
19