functional-interface

How does java differentiate Callable and Runnable in a Lambda?

跟風遠走 提交于 2020-05-25 17:29:49
问题 I got this little code to test out Callable . However, I find it pretty confusing how the Compiler could know if the Lambda is for the Interface Callable or Runnable since both don't have any parameter in their function. IntelliJ, however, shows that the Lambda employs the code for a Callable. public class App { public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(() ->{ System.out

How does java differentiate Callable and Runnable in a Lambda?

馋奶兔 提交于 2020-05-25 17:24:22
问题 I got this little code to test out Callable . However, I find it pretty confusing how the Compiler could know if the Lambda is for the Interface Callable or Runnable since both don't have any parameter in their function. IntelliJ, however, shows that the Lambda employs the code for a Callable. public class App { public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(() ->{ System.out

How does java differentiate Callable and Runnable in a Lambda?

一世执手 提交于 2020-05-25 17:22:14
问题 I got this little code to test out Callable . However, I find it pretty confusing how the Compiler could know if the Lambda is for the Interface Callable or Runnable since both don't have any parameter in their function. IntelliJ, however, shows that the Lambda employs the code for a Callable. public class App { public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool(); executorService.submit(() ->{ System.out

Implementing a proper C# event (not delegate) in java

大憨熊 提交于 2020-01-06 06:40:26
问题 As already mentioned in this SO answer and the other posts in the same question, C# delegates can be implemented using interfaces or Java FuncationInterfaces. However I am looking to implement a proper event model and not a delegate model in Java. For a brief on the difference of the two, please see this. Especially the first comment. Below is what I have tried so far: Event.java public class Event { public interface EventHandler{ void invoke(); } private Set<EventHandler> mEventHandlers =

Function interface in Kotlin 1.4

左心房为你撑大大i 提交于 2020-01-04 06:13:05
问题 This feature will be coming Kotlin 1.4 . Here is an excerpt from KotlinConf'19 . fun interface Action { fun run() } fun runAction(a: Action) = a.run() runAction{ println("Hello") } It looks nice, but I still don't know what it does. What is the function interface? What is its practical value? What specific scenarios can it be used for? 回答1: This is about functional interfaces — interfaces with a Single Abstract Method (also called SAM interfaces). To understand the point, I'll need to cover a

Create custom Predicate with Set<String> and String as parameter

邮差的信 提交于 2020-01-02 01:36:48
问题 I have a String as "ishant" and a Set<String> as ["Ishant", "Gaurav", "sdnj"] . I need to write the Predicate for this. I have tried as below code, but it is not working Predicate<Set<String>,String> checkIfCurrencyPresent = (currencyList,currency) -> currencyList.contains(currency); How can I create a Predicate which will take Set<String> and String as a parameter and can give the result? 回答1: A Predicate<T> which you're currently using represents a predicate (boolean-valued function) of one

Why can't we overload a abstract method in a functional interface? (Java)

早过忘川 提交于 2020-01-01 12:08:14
问题 So I am familiar with functional interfaces in java, and their use with lambda expressions. A functional interface can only contain one abstract method. When using this lonely method from a lambda expression, you do not need to specify its name - since there is only one abstract method in the interface, the compiler knows that's the method you are referencing. Example: // Functional Interface: @FunctionalInterface public interface Ball { void hit(); } // Lambda to define, then run the hit

Why can't we overload a abstract method in a functional interface? (Java)

亡梦爱人 提交于 2020-01-01 12:07:13
问题 So I am familiar with functional interfaces in java, and their use with lambda expressions. A functional interface can only contain one abstract method. When using this lonely method from a lambda expression, you do not need to specify its name - since there is only one abstract method in the interface, the compiler knows that's the method you are referencing. Example: // Functional Interface: @FunctionalInterface public interface Ball { void hit(); } // Lambda to define, then run the hit

How create a new map from the values in an existing map

*爱你&永不变心* 提交于 2019-12-30 15:02:07
问题 Having the next original map: G1=[7,8,45,6,9] G2=[3,9,34,2,1,65] G3=[6,5,9,1,67,5] Where G1, G2 and G3 are groups of people's ages, How can I create a new map like this: 45=[7,8,45,6,9] 65=[3,9,34,2,1,65] 67=[6,5,9,1,67,5] Where the new keys are the max people's age in each group. I have tried this: Map<Integer, List<Integer>> newMap = originalMap.entrySet().stream() .collect(Collectors.toMap(Collections.max(x -> x.getValue()), x -> x.getValue())); But the compiler say me: "The target type of

What is the use of inheriting object class methods in functional interface eg- toString, equals

回眸只為那壹抹淺笑 提交于 2019-12-30 12:47:05
问题 I found following code, What is use of inherited equals() and toString() method. @FunctionalInterface public interface FunInterface<T> { // An abstract method declared in the functional interface int test(T o1, T o2); // Re-declaration of the equals() method in the Object class boolean equals(Object obj); String toString(); } 回答1: The main reason to (re)declare such a method, is to extend and document the contract. In case of Comparator.equals(…), it’s not that obvious, as it doesn’t specify