Posts

Showing posts with the label Java8FunctionalInterfaces

Java 8 LongSupplier explained with code samples

In this blog post, I will be explaining how the Java 8 functional interface LongSupplier works. To know more about functional interfaces, you can refer this blog post. Edit The LongSupplier interface provides a method called getAsLong. This method does not accept any arguments. It returns a Long data type. The LongSupplier interface is a specialization of the Supplier interface that returns an Long. To see an example of the Supplier interface, refer to this blog post. LongSupplier example Consider the following code snippet: LongSupplier getRandom = () -> new Random().nextLong();System.out.println("Random number 1 = "+getRandom.getAsLong());System.out.println("Random number 2 = "+getRandom.getAsLong());   Here, we have implemented the LongSupplier.getAsLong method using a lambda expression. This getAsLong method simple returns a random integer less than 100. So when this code is executed, it will print output similar to the following: Random number 1 ...

Java 8 DoubleSupplier Example

In this blog post, I will be explaining how the DoubleSupplier Java 8 functional interface works. To know more about functional interfaces, you can refer this blog post. Edit The  DoubleSupplier interface provides a method called getAsDouble This method does not accept any arguments. It returns a Double data type. The DoubleSupplier interface is a specialization of the Supplier interface that returns a double value. To see an example of the Supplier interface, refer to this blog post. DoubleSupplier example Consider the following code snippet: public class DoubleSupplierDemo { public static void main(String[] args) { DoubleSupplier getRandom = () -> new Random().nextDouble(); System.out.println("Random number 1 = "+getRandom.getAsDouble()); System.out.println("Random number 2 = "+getRandom.getAsDouble()); }}   Here, we have implemented the DoubleSupplier.getAsDouble method using a lambda expression.  This getAsDouble method simple returns a ra...