Print

Double colon operator ( :: ) in Java 8

Double colon operator ( :: ) in Java 8 is really useful operator which may help you to reduce some code.

We can used the :: operator as shorthand for lambdas calling a specific method – by name.

For example, will create Comaprator:

Comparator c = (Computer c1, Computer c2) -> c1.getAge().compareTo(c2.getAge());

Other way, using type inference:

Comparator c = (c1, c2) -> c1.getAge().compareTo(c2.getAge());

But we can do it more readable:

Comparator c = Comparator.comparing(Computer::getAge);

We’re looking at a method reference to the method getAge defined in the Computer class.

We can then operate with that function:

Function<Computer, Integer> getAge = Computer::getAge;

Integer computerAge = getAge.apply(c1);

1. Static method invocation


List inventory = Arrays.asList(
                               new Computer(2015, "HP", "laptop"),  
                               new Computer(2017, "MacBook", "laptop"),  
                               new Computer(2018, "MacBook Pro", "laptop")));
inventory.forEach(ComputerUtils::repair);

2. Like An Instance Method of an Existing Object


inventory.forEach(ComputerUtils::repair);
Computer c1 = new Computer(2015, "HP", "laptop");
Computer c2 = new Computer(2017, "MacBook", "laptop");
Computer c3 = new Computer(2018, "MacBook Pro", "laptop"));
Arrays.asList(c1, c2, c3).forEach(System.out::print);

2. Like An Instance Method of an Existing Object


Computer c1 = new Computer(2015, "HP", "laptop");
Computer c2 = new MacbookPro(2018, "laptop");
List inventory = Arrays.asList(c1, c2);
inventory.forEach(Computer::Shutdown);

3. Supermethod invocation

Computer{
int year;
String type;
String model;
public Computer (int year, String type, String model){
        this.year = year;
        this.type = type;
        this.model = model;
}
public void Shutdown() {System.out.println("Shutdown");}
public Double calculate(Double initialValue) {
    return initialValue/1.50;
}
}

MacBook extends Computer{
public MacBook (int year, String model){
        this.year = year;
        this.type = "MacBook";
        this.model = model;
}
@Override
public Double calculate(Double initialValue){
    Function<Double, Double> function = super::calculate;
    Double pcValue = function.apply(initialValue);
    return pcValue + (initialValue/10) ;
}
}

4. New instance creation

@FunctionalInterface
public interface InterfaceComputer {
    Computer create();
}
 
InterfaceComputer c = Computer::new;
Computer computer = c.create();

If we have parameters:


BiFunction<Integer, String, MacBook> cFunction = MacBook::new; 
MacBook c = cFunction.apply(2013, "desktop");

If parameters are three or more you have to define a new Functional interface:


FunctionalInterface
interface TriFunction<A, B, C, R> { 
    R apply(A a, B b, C c); 
    default  TriFunction<A, B, C, V> andThen( Function<? super R, ? extends V> after) { 
        Objects.requireNonNull(after); 
        return (A a, B b, C c) -> after.apply(apply(a, b, c)); 
    } 
}
TriFunction <Integer, String, String, Computer> c6Function = Computer::new;
Computer c3 = c6Function.apply(2008, "HP", "desktop");

5. New array creation


Function <Integer, Computer[]> computerCreator = Computer[]::new;
Computer[] computerArray = computerCreator.apply(1995);

As you may see Double colon operator ( :: ) in Java 8 may be really useful.