Pages

Thursday, July 11, 2019

Java 8 - Working with IntSupplier

1. Overview


In this tutorial, We'll learn how to use IntSupplier Functional Interface and part of package java.util.function which has been introduced in java 8.

IntSupplier represents a supplier of int-valued results. This is the int-producing primitive specialization of Supplier.

As we discussed in the previous article on "Working with Supplier Functional Interface", IntSupplier comes under Supplier's category and does not take any argument.

IntSupplier has only one functional method that is getAsInt().

Java 8 - Working with IntSupplier


When we expect an int primitive value rather than Integer wrapper then we should not use directly Supplier FI. Because it does autoboxing and leads to performance issues.


To avoid automatic conversion, IntSupplier is added as part of Functional Interfaces.

@FunctionalInterface
public interface IntSupplier
{
int getAsInt();
}

2. IntSupplier getAsInt () Example to get int min value


Integer class has a static property MIN_VALUE which is int primitive type.

We can get this min value using IntSupplier.

package com.java.w3schools.blog.java8.functional.supplier;
import java.util.function.IntSupplier;
/**
 * Example program on IntSupplier
 * 
 * @author venkateshn
 *
 */
public class IntSupplierExamples {

 public static void main(String[] args) {

  IntSupplier intSupplier = () -> Integer.MIN_VALUE;
  System.out.println("Integer Min value " + intSupplier.getAsInt());

 }

}

Output:

Integer Min value -2147483648

Here, we know that Integer.MIN_VALUE returns int. So, the usage of IntSupplier is valid.

3. Example to increment int value using IntSupplier


we are now going to see how to increment int value using this Functional Interface(FI).

int[] intArray = {0};
IntSupplier incremenntSupplier = () -> ++intArray[0];

System.out.println(incremenntSupplier.getAsInt());
System.out.println(incremenntSupplier.getAsInt());
System.out.println(incremenntSupplier.getAsInt());

Output:

1
2
3

getAsInt() method can be called multiple times and does not throw any error. Each time, we call intArray value will be incremented by 1.

4. Fibonacci Series using IntSupplier


Next, we write the code to print the first 15 numbers from the Fibonacci Series.

// Declaring 3 int array's
int previous[] = { 0 };
int current[] = { 1 };
int nextValue[] = { 1 };

// writing fibonaci logic inside IntSupplier
IntSupplier fibSupplier = () -> {
 nextValue[0] = previous[0] + current[0];
 previous[0] = current[0];
 current[0] = nextValue[0];
 return previous[0];
};

//Generating IntStream by calling 15 times fibSupplier and print the value returned by fibSupplier.
IntStream.generate(fibSupplier).limit(15).peek(str -> System.out.print(" ")).forEach(System.out::print);

Output:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

5. Conclusion


In this article, We have discussed IntSupplier Functional Interface under Suppliers Category.

This is a functional interface whose functional method is getAsInt().

Further, Seen example programs on getting int min value, incrementing int value by one and printing Fibonacci series values using IntStream.getAsInt() method.

All the examples shown in this tutorial are on GitHub.

No comments:

Post a Comment

Please do not add any spam links in the comments section.