Posts

Java 9 - Try with resources Improvement

Java 9   -   Try with resources Improvement In this blog, I’ll discuss about try with resources which was introduced with JDK 7 and what enhancement has been done in JDK 9. Try with resources is a great feature which was introduced in JDK 7 that helps in closing resources automatically after being used. Any class can be used as resources if that class implements AutoClosable Interface. Advantages As try with resources closes all the resources file Automatically which prevents memory leaks. M ore readable code as you don’t have to write unnecessary code.   Let’s understand it with an example import java.io.FileNotFoundException; import java.io.FileOutputStream; /**  * This class will explain the try with resources which was introduced with  * JDK7.  *  * @author AbdulWaheed18 @gmail.com  *  */ public class ExmapleWithJava7 {        public static void...

Supplier Functional Interface - JAVA 8

Supplier Functional Interface Supplier is an in-built functional interface introduced in Java 8 in the  java.util.function  package. Supplier does not expect any input but returns the output. This interface can be handy where you want to generate some data like OTP, currentTime or even GUID The functional method of Supplier is get(T t). Here is a simple source code of java.util.function.Supplier @FunctionalInterface public interface Supplier<T> {     /**      * Gets a result.      * @return a result      */     T get(); } Where T get () is an abstract method where T is the return type of the function. Example 1: import java.util.Date; import java.util.function.Supplier; public class SupplierExample {        public static void main(String[] args ) {    ...

Consumer and BiConsumer Functional Interface - JAVA 8

Consumer and BiConsumer Functional Interface Consumer<T>  is an in-built functional interface introduced in Java 8 in the  java.util.function  package. It can be used in all contexts where an object needs to be consumed, i.e. taken as input, and some operation is to be performed on the object without returning any result . Common example of such an operation is  printing  where an object is taken as input to the printing function and the value of the object is printed . The functional method of Consumer is accept(T t). @FunctionalInterface Public interface Consume<T, > { …} Here is a simple source code of java.util.function.Consumer package java.util.function;      import java.util.Objects;      @FunctionalInterface public interface Function<T> {     public void accept(T t);   } Where accept (T t) is an abstract method where T is the type of the...