Useful Java 8 Features Examples

Useful Java 8 Features Examples

Java 8 pass method as parameter

class One{

    public static void main(String[] args) {
        AnotherOne a = new AnotherOne();
        a.processLine(One::removeBob);
    }
    private static String removeBob(String str){
        return str.replaceAll("Bob", "");
    }

}

UnaryOperator here takes one argument and returns one argument of the same type.

class AnotherOne{

    public void processLine(UnaryOperator<String> fn) {
        this.lines = lines.stream().map(fn).collect(Collectors.toList());
    }

}