Java 8 - IntSupplier interface example

Posted on December 1, 2016


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

The following example shows how to use the getAsInt() method of the IntSupplier interface with lambda expression.

IntSupplierExample.java

package com.boraji.tutorial.lambda;

import java.util.function.IntSupplier;

/**
 * @author imssbora
 */
public class IntSupplierExample {
	public static void main(String[] args) {
		IntSupplier supplier1 = () -> Integer.MAX_VALUE;
		System.out.println(supplier1.getAsInt());
		
		int a=5;
		int b=10;
		IntSupplier supplier2=()-> a*b;
		System.out.println(supplier2.getAsInt());
	}
}

Output

2147483647
50