Skip to main content

Posts

Showing posts from June, 2020

Stream API in Java 8

Stream API is another powerful feature added in Java 8 which changes the whole programming style. The interfaces and classes  related to  Stream API  are present in the package " java.util.stream " which are used for processing sequences of objects. Before moving ahead it is highly recommended that you should have basic knowledge of Lambda expression , Functional Interfaces , and Method Reference . Please go through these above topic links before moving ahead. What are Streams in Java 8? Streams are sequence or series which are obtained from a different type of sources for example Collections, Arrays, group of values or from an input/output source. Stream API is used for processing the collection of objects or groups of values efficiently and allows us to execute multiple operations on it. Streams and collections differ from each other in many ways .  Collections are based on some data structures that store elements while streams are not a data structure. ...

Double (::) Operator or Method Reference Operator

In Java 8, a new special type of operator is added named as double (::) operator or method reference operator.  Why we called it a method reference operator because this double (::) operator or method reference operator is used to call methods by the help of the class in which the method is declared. As we know that lambda expression is one way to write more readable and concise code. Now with the help of method reference, you can write one step more readable and concise code. The main purpose of method reference is to write clean, concise, and more readable code.  Method reference also supports the code reusability. Let's take an example and understand it. Functional Interface: InfectedCountry.java A concrete class contains some methods: Covid19Stats.java A demo class for method reference: MethodReference.java In the above example, for  InfectedCountry.java functional interface we have written three lambda expressions. In the first lambda expression, we d...

Default and Static methods inside Interface

As we all know before Java 8 the interface is the only thing that was purely abstract. It means you were not able to define concrete methods inside the interface. Now interfaces are no longer as 100% pure abstract.  According to the Java 8 interface definition, you can define concrete methods inside the interface. Java 8 allows you to define default and static methods inside the interface. What are the default and static methods? The default method declares with the default keyword at the beginning of the method and having an implementation body. Default methods are only allowed inside the interface. The static method is just like the method present at the class level. A method declares with the static keyword at the beginning of the method and having an implementation body. Below is an example of default and static method. One thing you should always remember that anything defines inside the interface by default public and variables present inside the interface are implici...