functional-interface

Java 8 lambdas execution

守給你的承諾、 提交于 2019-12-30 05:59: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 回答1: 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

Please Explain Java 8 Method Reference to instance Method using class name

☆樱花仙子☆ 提交于 2019-12-28 06:33:12
问题 public interface MyFunc<T> { boolean func(T v1, T v2); } public class HighTemp { private int hTemp; HighTemp(){ } public HighTemp(int ht) { this.hTemp = ht; } boolean sameTemp(HighTemp ht2){ return hTemp == ht2.hTemp; } boolean lessThanTemp(HighTemp ht2){ return hTemp < ht2.hTemp; } } class InstMethWithObjRef { static <T> int counter(T[] vals, MyFunc<T> f, T v){ int count = 0; for (int i = 0; i < vals.length; i++) { if(f.func(vals[i], v)) count++; } return count; } public static void main

Definition of Functional Interface in Java 8 [duplicate]

孤街浪徒 提交于 2019-12-22 05:14:41
问题 This question already has answers here : Precise definition of “functional interface” in Java 8 (6 answers) Closed 3 years ago . The definition of a functional Interface in Java 8 says: A functional interface is defined as any interface that has exactly one explicitly declared abstract method. (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is

Definition of Functional Interface in Java 8 [duplicate]

醉酒当歌 提交于 2019-12-22 05:14:00
问题 This question already has answers here : Precise definition of “functional interface” in Java 8 (6 answers) Closed 3 years ago . The definition of a functional Interface in Java 8 says: A functional interface is defined as any interface that has exactly one explicitly declared abstract method. (The qualification is necessary because an interface may have non-abstract default methods.) This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is

Equality of instance of functional interface in java [duplicate]

落爺英雄遲暮 提交于 2019-12-22 04:13:17
问题 This question already has answers here : Is there a way to compare lambdas? (3 answers) Closed 4 years ago . I am not sure how I can be sure about equality/immutability of functional interface. I guess there might be no way to assure equality when I use this syntactic sugar in java 8, please let me know any hint if you have any. I made a short code snippet for my question. public interface Element { void doSomething(int a); } and I've tried to add instance of this interface in functional way

Mockito: Spying function calls inside Functional interfaces?

北战南征 提交于 2019-12-21 23:12:11
问题 Mockito doesn't seem to be able to spy on function calls inside Functional interfaces. Suppose I have a simple Spring Boot App with a service: @Service public class TestService { Function<Integer, Integer> mapping = this::add2; Integer add2(Integer integer) { return integer + 2; } } And a test: @SpyBean TestService testService; @Test public void mockitoTest2(){ doReturn(6).when(testService).add2(2); System.out.println(testService.mapping.apply(2)); } The test will return 4 instead of 6. Is

Concept of functional interface

Deadly 提交于 2019-12-19 04:07:34
问题 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

Method Reference - passing Function to method with Consumer argument

陌路散爱 提交于 2019-12-18 05:12:07
问题 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

How can Comparator be a Functional Interface when it has two abstract methods? [duplicate]

大兔子大兔子 提交于 2019-12-18 03:50:34
问题 This question already has answers here : Precise definition of “functional interface” in Java 8 (6 answers) Closed 2 years ago . In Java 8, the @FunctionalInterface annotation is introduced to denote any interface that has exactly one abstract method as a functional interface. One of the reason for its introduction is to indicate the user (programmer), that lambda expression can be used in context of a functional interface. The Comparator interface is annotated with @FunctionalInterface . But

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

淺唱寂寞╮ 提交于 2019-12-17 16:28:25
问题 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