Understand Java 8 :: (double colon) operator-Method References using Simple Example | JavaInUse



Understand Java 8 -:: (double colon) operator-Method References using simple example


In this post we will understand the Method Reference using a simple example. Usually we use lambda expressions to create anonymous methods which return us the desired output. But sometimes lambda expressions do nothing but call an existing method. Because this lambda expression calls an existing method, method reference can be used here instead of Lambda function. Method reference is described using :: (double colon) symbol.
package com.javainuse;

import java.util.Arrays;
import java.util.List;

public class MethodReferenceTest {
	public static void main(String args[]) {

		List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
		System.out.println("-------Using Lambda Functions--------");
		
		//Using Lambda function to call system.out.println()
		myList.stream().map(s -> s.toUpperCase())
				.forEach(s -> System.out.println(s));

		System.out.println("-------Using Method Reference--------");
//Using Method reference to call system.out.println() myList.stream().map(String::toUpperCase).sorted() .forEach(System.out::println); } }


Lets look at another example.
Create the Person.java and Employee.java as follows
Person.java
package com.javainuse.model;

public class Person {

	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Person(String name) {
		super();
		this.name = name;
	}

}

Employee.java
package com.javainuse.model;

public class Employee {

	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Employee(String name) {
		super();
		this.name = name;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + "]";
	}

}
	

We now convert a list of Person to Employees as follows-
package com.javainuse;

import java.util.ArrayList;
import java.util.stream.Collectors;

import com.javainuse.model.Employee;
import com.javainuse.model.Person;

public class MethodReferenceTest2 {
	public static void main(String args[]) {

		MethodReferenceTest2 methodReferenceTest = new MethodReferenceTest2();

		ArrayList<Person> personList = new ArrayList();
		personList.add(new Person("person1"));
		personList.add(new Person("person2"));
		personList.add(new Person("person3"));
		personList.add(new Person("person4"));

		methodReferenceTest.convertToEmployee(personList);
	}

	private void convertToEmployee(ArrayList<Person> personList) {

		System.out.println("-------Using Lambda Functions--------");
		
	//Using Lambda function to call recruit method
		personList.stream().map(person -> this.recruit(person))
				.collect(Collectors.toList()).forEach(s -> System.out.println(s));
	
	 System.out.println("-------Using Method Reference--------"); 
	
	//Using method reference to call recruit method
	 personList.stream().map(this::recruit).collect(Collectors.toList())
	.forEach(System.out::println);
	
	 } 
	
	private Employee recruit(Person person) 
	{
	 Employee emp = new Employee(person.getName());
	  return emp; 
	}

	}
	

So the Person List has been mapped to Employee List.

See Also

Java - PermGen space vs MetaSpace Java 8 Lambda Expression- Hello World Example Java 8-Internal vs. External Iteration. Understanding Java 8 Streams using examples. Understanding Java 8 Optional using examples.