functional-interface

Method reference is ambiguous for Thread.sleep

时光毁灭记忆、已成空白 提交于 2019-11-29 11:40:04
问题 I've come across a weird problem where a method reference to Thread::sleep is ambiguous but a method with the same signature is not. package test; public class Test { public static void main(String[] args) { foo(Test::sleep, 1000L); //fine foo((FooVoid<Long>)Thread::sleep, 1000L); //fine foo(Thread::sleep, 1000L); //error } public static void sleep(long millis) throws InterruptedException { Thread.sleep(millis); } public static <P, R> void foo(Foo<P, R> function, P param) {} public static <P>

Method Reference - passing Function to method with Consumer argument

人走茶凉 提交于 2019-11-29 07:50:31
I'm learning about Method References from Java 8 and I have difficulties understanding why does this work? class Holder { private String holded; public Holder(String holded) { this.holded = holded; } public String getHolded() { return holded; } } private void run() { Function<Holder, String> getHolded = Holder::getHolded; consume(Holder::getHolded); //This is correct... consume(getHolded); //...but this is not } private void consume(Consumer<Holder> consumer) { consumer.accept(null); } As you can see in run method - Holder::getHolded returns unbound method reference which you can invoke by

Implementing an interface with two abstract methods by a lambda expression

杀马特。学长 韩版系。学妹 提交于 2019-11-29 00:31:58
问题 In Java 8 the lambda expression is introduced to help with the reduction of boilerplate code. If the interface has only one method it works fine. If it consists of multiple methods, then none of the methods work. How can I handle multiple methods? We may go for the following example public interface I1() { void show1(); void show2(); } Then what will be the structure of the main function to define the methods in the main itself? 回答1: Lambda expressions are only usable with functional

Thread.sleep inside infinite while loop in lambda doesn't require 'catch (InterruptedException)' - why not?

蓝咒 提交于 2019-11-28 19:28:36
问题 My question is about InterruptedException , which is thrown from the Thread.sleep method. While working with ExecutorService I noticed some weird behaviour that I don't understand; here is what I mean: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> { while(true) { //DO SOMETHING Thread.sleep(5000); } }); With this code, the compiler doesn't give me any error or message that InterruptedException from Thread.sleep should be caught. But when I am trying to

Why can't @FunctionalInterface be applied to a SAM abstract base class

别来无恙 提交于 2019-11-27 22:58:15
I'm just starting to learn Camel and the first thing I see is context.addRoutes(new RouteBuilder() { public void configure() { from("file:data/inbox?noop=true").to("file:data/outbox"); } }); which I (reasonably IMHO) try to replace with context.addRoutes(()->from("file:data/inbox?noop=true").to("file:data/outbox")); but that is not valid. As I dig, I discover that lambdas apply to functional interfaces (which is be implied, if the interface qualifies) but that the @FunctionalInterface annotation can only be applied to interfaces (fair enough) and there is, as far as I can tell, no equivalent

Java 8 Supplier with arguments in the constructor

依然范特西╮ 提交于 2019-11-27 19:13:51
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")) 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. Brian Goetz But, a 1-arg constructor for T that takes a String is compatible with Function<String,T> : Function<String, Foo> fooSupplier = Foo::new; Which constructor is selected is treated as an overload selection problem, based on the shape of

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

六月ゝ 毕业季﹏ 提交于 2019-11-27 15:32:26
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 BImplementor(); carryOutWork(() -> implementsB.doWork()); } } @FunctionalInterface interface

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

扶醉桌前 提交于 2019-11-27 13:34:37
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." Evaluation of a lambda expression produces an instance of a functional interface. Thus, the purpose of the

Lambda can only be used with functional interface?

混江龙づ霸主 提交于 2019-11-27 13:10:31
问题 I did this: public class LambdaConflict { public static void main(String args[]){ //* System.out.println(LambdaConflict.get( (str) -> "Hello World!! By ME?" )); /*/ System.out.println(LambdaConflict.get(new Intf<String> (){ @Override public String get1(String str){ return "Hello World!! By get1 " + str; } })); /*****/ } public static String get(Intf<String> i, boolean b){ return i.get1("from 1"); } } interface Intf<T> { public T get1(T arg1); public T get2(T arg1); } and get this exception:

Why isn't @FunctionalInterface used on all the interfaces in the JDK that qualify?

拈花ヽ惹草 提交于 2019-11-27 09:13:43
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 added @FunctionalInterface still missing it? There is a bit of a vague hint in the annotations