functional-interface

Behavior of Functional Interface and Method Reference

我是研究僧i 提交于 2019-12-01 04:11:02
问题 What happens when the reference of a method which belongs to a variable is destroyed? public class Hey{ public double bar; public Hey(){ bar = 2.0d; } public double square(double num){ return Math.pow(num , bar); } } Function<Double, Double> square; whatsGonnaHappen: { Hey hey = new Hey(); square = hey::square; }//is hey still kept around because its method is being referenced? double ans = square.apply(23d); 回答1: Scope is a compile time concept that governs where names in source code can be

Is a class being instantiated in a lambda expression? [duplicate]

霸气de小男生 提交于 2019-12-01 02:57:52
问题 This question already has an answer here : What is a Java 8 Lambda Expression Compiled to? [duplicate] (1 answer) Closed last year . I have the following method invocation, in which I am passing a lambda expression. Is a class being instantiated implicitly here? printStudents( roster, (Student s) -> s.getGender() == Student.Sex.MALE && s.getAge() >= 18 && s.getAge() <= 25 ); Method signature: printStudents(List<Student> roster, CheckStudent checkstudet) interface CheckStudent { boolean test

Method reference with a full constructor call as a lambda expression in Java

烂漫一生 提交于 2019-12-01 02:42:30
问题 I have encountered a short time ago with a competitive answer better than mine that uses a quite new method reference to me as replacement of lambda. Stream.generate(new AtomicInteger(1)::getAndIncrement)... I looked the Oracle specifications about the Method references and there are defined 4 types: Reference to a static method ContainingClass::staticMethodName Reference to an instance method of a particular object containingObject::instanceMethodName Reference to an instance method of an

Concept of functional interface

自闭症网瘾萝莉.ら 提交于 2019-11-30 23:27:22
When I'm shooting a glance at lambda expressions, the book touches on a functional interface that has only one abstract method. My issue addresses on that quiz question /* Which of these interfaces are functional interfaces? */ public interface Adder{ int add(int a, int b); } public interface SmartAdder extends Adder{ int add(double a, double b); } public interface Nothing{ } I know the last one is not, but I think the first and second ones should be functional interface. But the book says second one is not. Why? Doesn't it overrides add method? So even in second, isn't there only one abstract

Purpose of Functional Interfaces in Java8

做~自己de王妃 提交于 2019-11-30 21:17:11
I've come across many questions in regards of Java8 in-built Functional Interfaces, including this , this , this and this . But all ask about "why only one method?" or "why do I get a compilation error if I do X with my functional interface" and alike. My question is: what is the existential purpose of these new Functional Interfaces, when I can use lambdas anyway in my own interfaces ? Consider the following example code from oracle documentation : // Approach 6: print using a predicate public static void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) { for (Person p

Java 8 lambdas execution

99封情书 提交于 2019-11-30 18:18:48
How can I do something like this in Java 8? boolean x = ((boolean p)->{return p;}).apply(true); Right now I get the following error: The target type of this expression must be a functional interface As per the JLS section 15.27 : It is a compile-time error if a lambda expression occurs in a program in someplace other than an assignment context (§5.2), an invocation context (§5.3), or a casting context (§5.5). It is also possible to use a lambda expression in a return statement . We can then rewrite your example in four different ways: By creating an assignment context: Function<Boolean,

Why is this Java method call considered ambiguous?

青春壹個敷衍的年華 提交于 2019-11-30 11:22:33
I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String s); } public static void load(Supplier s) {} public static void load(Processor p) {} public static int genuinelyAmbiguous() { return 4; } public static String genuinelyAmbiguous(String s) { return "string"; } public static int notAmbiguous() { return 4; } public static String notAmbiguous(int x, int y) { return "string"; } public static int strangelyAmbiguous() { return 4; }

Purpose of Functional Interfaces in Java8

让人想犯罪 __ 提交于 2019-11-30 05:25:00
问题 I've come across many questions in regards of Java8 in-built Functional Interfaces, including this, this, this and this. But all ask about "why only one method?" or "why do I get a compilation error if I do X with my functional interface" and alike. My question is: what is the existential purpose of these new Functional Interfaces, when I can use lambdas anyway in my own interfaces ? Consider the following example code from oracle documentation: // Approach 6: print using a predicate public

Implementing an interface with two abstract methods by a lambda expression

落花浮王杯 提交于 2019-11-30 02:55: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? Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the

Why is this Java method call considered ambiguous?

懵懂的女人 提交于 2019-11-29 17:02:54
问题 I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String s); } public static void load(Supplier s) {} public static void load(Processor p) {} public static int genuinelyAmbiguous() { return 4; } public static String genuinelyAmbiguous(String s) { return "string"; } public static int notAmbiguous() { return 4; } public static String