0

Q: Given the following lambda expression: (name, age, isAdult) -> new Person(name, age, isAdult), identify the build in interface.

I've managed to write my own functional interface:

@FunctionalInterface
public interface Generator<T,U,V,R> {
    R generate(T t, U u, V v);
}

But I don't know if is necessary to write this interface, or java already has a build-in interface.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • No, there's no such a FunctionalInterface that you can use out of the box. But to me your approach of introducing a new functional interface for this purpose is counter intuitive. Often times we come across with creating new Objects by passing set of fields. For that, you can merely use a constructor and don't need to define a new Functional interface. – Ravindra Ranwala Sep 24 '19 at 15:17
  • @RavindraRanwala I know that this is not a good approach, but I wanted to know if you can replace this functional interface with some build in interfaces. Maybe a combination of two functional interfaces? – jennifer lawrence Sep 24 '19 at 15:24
  • 1
    @RavindraRanwala there is no reason to add the [java-stream] tag when this question is not even remotely related to Streams. – Holger Sep 24 '19 at 15:40
  • 1
    [This answer](https://stackoverflow.com/a/42973815/2711488) contains a complete list of Java 8’s builtin interfaces which could be implemented via lambda expression, though not every implementation would be semantically correct. So even if you find an interface with the right signature (I doubt it), it’s unlikely a good one for this particular use case. So if you need such a generator interface, defining your own is the best way. – Holger Sep 24 '19 at 15:56
  • Closely related - [Java 8: Where is TriFunction (and kin) in java.util.function? Or what is the alternative?](https://stackoverflow.com/questions/18400210/java-8-where-is-trifunction-and-kin-in-java-util-function-or-what-is-the-alt/54285143) – Naman Sep 24 '19 at 18:23

1 Answers1

1

Nope. There's nothing in the java.util.function package which applies to this.

If the function only had 2 arguments, e.g.

(name, age) -> ...

then you could use BiFunction but there is no "TriFunction".

Michael
  • 41,989
  • 11
  • 82
  • 128