0

As I'm learning how to use various new tools, I ran upon this syntax (Function x -> ...) which I'm having trouble understanding, and I'd love for someone to write equivalent code if possible so that I can understand it better.

Function<String, HashSet<String>> asSet = (String x) ->
      new HashSet<String>() {{
        do_something(x);
      }};

Any block of code using more traditional syntax and not that weird Function would be greatly appreciated and useful in helping me better my understanding of Java!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • `Function` is just a normal interface (https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html); the `->` syntax isn't particularly related to it. – ruakh Mar 30 '19 at 23:16
  • Possible duplicate of [Java8 Lambdas vs Anonymous classes](https://stackoverflow.com/questions/22637900/java8-lambdas-vs-anonymous-classes) – LppEdd Mar 30 '19 at 23:17
  • The reason, this code is not understandable, is not the lambda expression. It’s due to the use of the [Double Brace Initialization anti-pattern](https://stackoverflow.com/q/1958636/2711488), which has been maxed out by using it for a statement entirely unrelated to the constructed map. The code is doing the same as `Function> asSet = x -> { do_something(x); return new HashSet<>(); }`, except that it is more verbose and creates an unintended subclass of `HashSet`, including memory leaks if the containing method is not `static`. – Holger Apr 01 '19 at 08:35

3 Answers3

1

It can be replaced with anonymous class:

Function<String, HashSet<String>> asSet = new Function<>() {
    @Override
    public HashSet<String> apply(String s) {
        return new HashSet<>() {{
            do_something(s);
        }};
    }
};

You just implement apply method from Function which is functional interface:

Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

See more What is use of Functional Interface in Java 8?

Ruslan
  • 6,090
  • 1
  • 21
  • 36
0

Very basically and without explaining the advantages of using Functions you can imagine your function to be an analogy of:

HashSet<String> anonymousMethod(String x) {
    return doSomething(x);
}

...which resides anonymously in your function object.

mle
  • 2,466
  • 1
  • 19
  • 25
0

This syntax is called Lambda Expressions it's used to simplify implementing Functional Interfaces.

Functional Interfaces: are interfaces with one function to implement for example you can write:

// Java program to demonstrate functional interface

class Test {
    public static void main(String args[]) {
        // create anonymous inner class object 
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("New thread created");
            }
        }).start();
    }
}

or

// Java program to demonstrate Implementation of
// functional interface using lambda expressions

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

        // lambda expression to create the object 
        new Thread(() - > {
            System.out.println("New thread created");
        }).start();
    }
}