functional-interface

Use method reference with parameter

感情迁移 提交于 2019-11-27 07:34:44
I just started learning Java streams and faced a problem. Please take a look at a the following example. This is part of a Node class: private Map<String, Node> nodes; public Optional<Node> child(String name) { return Optional.<Node>ofNullable(nodes.get(name)); } private void findChildren(String name, List<Node> result) { child(name).ifPresent(result::add); nodes.values().stream() // .map(Node::findChildren(name, result)) // .forEach(Node::findChildren(name, result)) .forEach(node -> node.findChildren(name, result)); } My intent was to call #findChildren with the name and result parameters on

Why Functional Interfaces in Java 8 have one Abstract Method?

☆樱花仙子☆ 提交于 2019-11-27 04:49:22
As we know in Java 8, the concept of functional interfaces are introduced. A Functional Interface has one abstract method and several default or static methods are possible. But why should a Functional interface have only one abstract method? If Interface has more then one abstract method, why is this not a Functional Interface? AdityaKeyal The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one

Should 'Comparable<T>' be a 'Functional interface'?

北城以北 提交于 2019-11-27 01:48:53
问题 The definition of a functional interface is "A functional interface is an interface that has just one abstract method (aside from the methods of Object ), and thus represents a single function contract." According to this definition, the Comparable<T> is definitely a functional interface. The definition of a lambda expression is "A lambda expression is like a method: it provides a list of formal parameters and a body - an expression or block - expressed in terms of those parameters."

Can you call the parent interface's default method from an interface that subclasses that interface? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 17:12:28
问题 This question already has an answer here: Explicitly calling a default method in Java 4 answers In java 8 I have something like this: package test; public class SimpleFuncInterfaceTest { public static void carryOutWork(AFunctionalInterface sfi){ sfi.doWork(); } public static void main(String[] args) { carryOutWork(() -> System.out.println("Do work in lambda exp impl...")); AImplementor implementsA = new AImplementor(); //carryOutWork(() -> implementsA.doWork()); BImplementor implementsB = new

Why isn&#39;t @FunctionalInterface used on all the interfaces in the JDK that qualify?

天大地大妈咪最大 提交于 2019-11-26 14:35:15
问题 Java 8 gave us many fun ways to use functional interfaces and with them a new annotation: @FunctionalInterface. Its job is to tell the compiler to yell at us if we fail to stick to the rules of a functional interface (only one abstract method that needs overriding please). There are 43 interfaces in the java.util.function package with this annotation. A search of jdk.1.8.0/src for @FunctionalInterface only turns up 57 hits. Why are the other interfaces (such as AutoCloseable) that could have

Precise definition of “functional interface” in Java 8

扶醉桌前 提交于 2019-11-26 13:21:42
Recently I started exploring Java 8 and I can't quite understand the concept of "functional interface" that is essential to Java's implementation of lambda expressions. There is a pretty comprehensive guide to lambda functions in Java, but I got stuck on the chapter that gives definition to the concept of functional interfaces . The definition reads: More precisely, a functional interface is defined as any interface that has exactly one abstract method. An then he proceeds to examples, one of which is Comparator interface: public interface Comparator<T> { int compare(T o1, T o2); boolean

Java 8 Supplier with arguments in the constructor

倾然丶 夕夏残阳落幕 提交于 2019-11-26 13:09:56
问题 Why do suppliers only support no-arg constructors? If the default constructor is present, I can do this: create(Foo::new) But if the only constructor takes a String, I have to do this: create(() -> new Foo("hello")) 回答1: That's just a limitation of the method reference syntax -- that you can't pass in any of the arguments. It's just how the syntax works. 回答2: But, a 1-arg constructor for T that takes a String is compatible with Function<String,T> : Function<String, Foo> fooSupplier = Foo::new

Why do I need a functional Interface to work with lambdas?

走远了吗. 提交于 2019-11-26 08:10:00
问题 I think this question is already somewhere out there, but I wasn\'t able to find it. I don\'t understand, why it\'s necessary to have a functional interface to work with lambdas. Consider the following example: public class Test { public static void main(String...args) { TestInterface i = () -> System.out.println(\"Hans\"); // i = (String a) -> System.out.println(a); i.hans(); // i.hans(\"Hello\"); } } public interface TestInterface { public void hans(); // public void hans(String a); } This

Precise definition of “functional interface” in Java 8

杀马特。学长 韩版系。学妹 提交于 2019-11-26 03:39:05
问题 Recently I started exploring Java 8 and I can\'t quite understand the concept of \"functional interface\" that is essential to Java\'s implementation of lambda expressions. There is a pretty comprehensive guide to lambda functions in Java, but I got stuck on the chapter that gives definition to the concept of functional interfaces. The definition reads: More precisely, a functional interface is defined as any interface that has exactly one abstract method. An then he proceeds to examples, one