×
DocsConsole

DocsConsole

Java 8 Features and Examples

By | | Updated : 2020-01-11 | Viewed : 247 times

Java 8 Features and Examples

Java 8 was released in March 2018 with different types of features using which we can resolve some of the problems in earlier releases. We will review all the important Java 8 features here.

Functional Interfaces

Functional Interface is a new concept introduced in Java 8. Please see the difference between Interface and Functional Interface.

MyInterface.java
public interface MyInterface {
	public void method();
}
Functional Interface Example:
@FunctionalInterface
public interface MyFunInterface {
	
	public void method();

	default void defaultMethod() {
		// implementation
	}
}

In Interface it has only unimplemented methods whereas FunctionalInterface has many numbers of implemented methods. So it is easy to add any number of methods which is not impacted on the subclasses. Here we can write the static default methods also.

Static Method Example: MyFunInterface1.java
@FunctionalInterface
public interface MyFunInterface1 {

	public void method();

	default void defaultMethod() {
		// implementation
	}

	static void staticMethod() {
		// implementation
	}
}

Default and Static Methods

Default Methods

Functional Interface is an interface with at least one default interface. It has introduced in Java 8 to allow the developers to extend the interface functionality.

So here developers can introduce new methods where the newly added method is not required to implement in the child classes.

MyInterface.java
public interface MyInterface {
	public void method();
}
MyClass.java
public class MyClass implements MyInterface {
		public void method();
} 

Now it is time to add one more method in the MyInterface where the problem comes into the picture. suppose a new method added as given below.

MyInterface.java

public interface MyInterface {
	
	public void method();
	public void method1();
}

Now the method method1() is required to implement in all the child classes. But we do not want to implement the method1(). To handle this problem the default method of the Functional Interface is introduced.

FunInterface.java
@FunctionalInterface
public interface FunInterface {

	void method(String name);

	default void defaultMethod(String name) {
		System.out.println("Default Method::" + name);
	}

}

Static Methods

These methods are the same as default methods in FunctionalInterface. But these static methods can not be overridden.

Example code for staticMethod
@FunctionalInterface
public interface FunInterface {

	void method(String name);

	default void defaultMethod(String name) {
		System.out.println("Default Method::" + name);
	}
	static void staticMethod(String name) {
		System.out.println("static Method::" + name);
	}

}

forEach() Method

This is method is introduced as a default method in the Iterable and Stream interfaces to iterate the collection elements. It has one argument that is the Consumer object. The business logic will be separately defined by using this Consumer Class. Please find the below-given example which is demonstrated forEach() method

Example Program for forEach()
package com.docsconsole.foreach;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class ForEachExample {

	public static void main(String[] args) {
		List<String> monthsList = new ArrayList<String>();
		monthsList = Arrays.asList("January", "Jan", "February", "March", "April", "May", "June", "July", "August",
				"September", "October", "November", "December");
		monthsList.forEach(new Consumer<String>() {
			public void accept(String month) {
				System.out.println(month);
			}
		});
		Month month = new Month();
		monthsList.forEach(month);
	}

}

class Month implements Consumer<String> {

	public void accept(String month) {
	}

}

Lambda Expressions

You might observe that the Functional Interface has at least one default method. But it has also the abstract functions. So Lambda Expression will be used for the implementation of these abstract functions only. For a clear understanding of the concept of Lambda Expression, consider the below given anonymous function in MathOperations interface.

Example of Lambda Expression
package com.docsconsole.lambdaexp;

public interface MathOperations {

	public int  add( int a, int b);

}
Example code for Lambda Expression
package com.docsconsole.lambdaexp;

public class LambdaExpExample {

	public static void main(String[] args) {
		
		MathOperations operation = (a, b) -> a + b;
		System.out.println(operation.add(20, 10));
	}
}

So if you observe the above function implementation we used the lambda expression to implement the abstract function.

Java Streams

In Java 8 To handle the collections Streams are used. The Stream can be executed Collections parallelly or sequentially.

Streams have a variety number of methods to process the collections sequentially or parallelly. Please find the below demonstrated program for Java Streams.

Example Code Java Streams
package com.docsconsole.javastrems;

import java.util.ArrayList;
import java.util.List;

public class JavaStreamsExample {
	public static void main(String[] args) {
		
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < 20; i++) {
			list.add(i);
		}
		//Sequential processing
		list.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
		
		//parallel processing
		list.parallelStream().filter(n -> n % 2 == 0).forEach(System.out::println);

	}
}
Leave A Reply