Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java Programming

for-each Method in Collection Utility Classes



Using the Lambda Expression for the forEach() method provided in the Java8 version to provide the capability to iterate without any external iterator.

package LambdaExpression;

import java.util.HashSet;

public class LambdaTestInterator {

	
	
	public static void main(String[] args) {
	
		HashSet intSet=new HashSet<>();
		
		intSet.add(1);
		intSet.add(2);
		intSet.add(3);

		intSet.forEach(intVal -> System.out.println(intVal));
		
	}
	
	
}