Archive

Archive for March 17, 2014

Java 8 : Double Colon operator

March 17, 2014 2 comments

Double Colon operator introduced in Java 8, We use it as method reference.

Syntax : ClassName::MethodName

for example, if we want to print a list :


List<String> strings = Arrays.asList("one","two");

strings.forEach(System.out::println);

here forEach is a new method added to the List interface in java 8. System.out is the PrintStream Class and println is method in it.

Complete Example:


import java.util.*;
import java.util.stream.*;

public class DoubleColon{

public static void display(String name){
System.out.println("Name : "+name);
}
public static void main(String[] args){
List<String> names = Arrays.asList("one","two","three");
names.forEach(DoubleColon::display);
}
}

Categories: Java