JAVA Features – Functions, forEach, Consumer Interface

This Blog explains about Java 8 – New Features – Functions, forEach, Consumer Interface.

Demo_Impl.java

package function_consumer;

public class Demo_Impl implements Demo_Interface{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Demo_Impl di = new Demo_Impl(); 
		di.display();

	}
	public void display()
	{
		System.out.println("LED Display");
	}

}

Demo_Interface.Java

package function_consumer;

public interface Demo_Interface {

	default void display()
	{
		System.out.println("LCD Display");
	}
}

Demo.java

package function_consumer;

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

public class Demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		Contract cc = new Contract_Implementation(); 
//		cc.rule1();
		List<Integer> l = Arrays.asList(10,4,5,3);
		
		//Consumer<Integer> cc = (t) -> System.out.println(t);
			
			
				//cc.accept(100);
		l.forEach((no) -> System.out.println(no));
				
		
	}
	

}

Consumer_Implementation.java

package function_consumer;

import java.util.function.Consumer;

public class Consumer_Implementation implements Consumer<Integer>{

	@Override
	public void accept(Integer no) {
		// TODO Auto-generated method stub
		System.out.println(no);
		
	}

}

Contract.Java

package function_consumer;

@FunctionalInterface
public interface Contract {
	
	public void rule1(); 

}

Contract_Implementation

package function_consumer;

public class Contract_Implementation implements Contract{

	@Override
	public void rule1() {
		// TODO Auto-generated method stub
		System.out.println("Hi");
	}
		
	}