Java 8 IntSupplier Interface Example

In this blog post, I will be explaining how the Java 8 functional interface IntSupplier works. To know more about functional interfaces, you can refer this blog post.

Edit

The  IntSupplier interface provides a method called getAsInt This method does not accept any arguments. It returns an Int data type. The IntSupplier interface is a specialization of the Supplier interface that returns an int. To see an example of the Supplier interface, refer to this blog post.

IntSupplier example

Consider the following code snippet:

</pre>public class IntSupplierDemo {30public static void main(String[] args) {IntSupplier getRandom = () -> new Random().nextInt(100);System.out.println("Random number 1 = "+getRandom.getAsInt());System.out.println("Random number 2 = "+getRandom.getAsInt());}}<pre>

Here, we have implemented the IntSupplier.getAsInt method using a lambda expression.  This getAsInt method simple returns a random integer less than 100. So when this code is executed, it will print the following output:

Random number 1 = 98 Random number 2 = 89

You can get the source code for this example along with other code for other Java 8 examples at the Github repository here.


Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set