functional-interface

Understand the compile time error with Method Reference

雨燕双飞 提交于 2021-01-04 06:41:39
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine

Understand the compile time error with Method Reference

扶醉桌前 提交于 2021-01-04 06:41:31
问题 As per the Documentation, Method Reference is absolutely not a static call. It works on both static and non- static methods. When we define our own non-static method in a given class and try to use it using Method Reference then the compile-time-error "cannot make static reference to non static method" is NOT seen in case of Function but only seen in case of Supplier, Consumer and Predicate. Why is that so? class Demo{ private Function<Student, Integer> p= Student::getGradeLevel; // fine

Callable vs Supplier interface in java

為{幸葍}努か 提交于 2020-12-29 09:23:54
问题 The Callable and Supplier functional interfaces in java.util.concurrent and java.util.function packages respectively have the following signature- public interface Callable<V> { V call() throws Exception; } public interface Supplier<T> { T get(); } Are there some specific use case where each one of them fit more than the other? 回答1: Their difference in usage can be seen from their respective documentation: Callable: A task that returns a result and may throw an exception . Implementors define

Callable vs Supplier interface in java

倾然丶 夕夏残阳落幕 提交于 2020-12-29 09:22:06
问题 The Callable and Supplier functional interfaces in java.util.concurrent and java.util.function packages respectively have the following signature- public interface Callable<V> { V call() throws Exception; } public interface Supplier<T> { T get(); } Are there some specific use case where each one of them fit more than the other? 回答1: Their difference in usage can be seen from their respective documentation: Callable: A task that returns a result and may throw an exception . Implementors define

What decides which functional interface to create from a lambda?

回眸只為那壹抹淺笑 提交于 2020-12-05 07:27:58
问题 Please consider this example: import java.util.function.Consumer; public class Example { public static void main(String[] args) { Example example = new Example(); example.setConsumer(test -> System.out.println("passed string is " + test)); //uses MyConsumer, why ? example.getConsumer().accept("Test 1"); example.setConsumer((MyConsumer<String>)test -> System.out.println("passed string is " + test)); //uses MyConsumer example.getConsumer().accept("Test 2"); example.setConsumer((Consumer<String>

What decides which functional interface to create from a lambda?

流过昼夜 提交于 2020-12-05 07:27:20
问题 Please consider this example: import java.util.function.Consumer; public class Example { public static void main(String[] args) { Example example = new Example(); example.setConsumer(test -> System.out.println("passed string is " + test)); //uses MyConsumer, why ? example.getConsumer().accept("Test 1"); example.setConsumer((MyConsumer<String>)test -> System.out.println("passed string is " + test)); //uses MyConsumer example.getConsumer().accept("Test 2"); example.setConsumer((Consumer<String>

Using Java Predicate and Lambda

爷,独闯天下 提交于 2020-06-11 21:53:47
问题 Why does the below code return Predicate<String> and not boolean ? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean . So in my lambda should my nonEmptyStringPredicate not be of type boolean ? Obviously, it's not, I'm just trying to understand why it's not. Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); 回答1: A Predicate gets in this case a String as parameter and returns a boolean .

Using Java Predicate and Lambda

笑着哭i 提交于 2020-06-11 21:51:07
问题 Why does the below code return Predicate<String> and not boolean ? My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t); The return type here is boolean . So in my lambda should my nonEmptyStringPredicate not be of type boolean ? Obviously, it's not, I'm just trying to understand why it's not. Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); 回答1: A Predicate gets in this case a String as parameter and returns a boolean .

Creating a parser of Class name + String value to a typed value

谁都会走 提交于 2020-05-30 08:12:11
问题 I am trying to write a method that can take in a String classname and a String value, and return the value represented as that String. Example inputs: parse("java.lang.String", "abc") -> String "ABC" parse("java.lang.Boolean", "FALSE") -> Boolean FALSE parse("java.lang.Integer", "123") -> Integer 123 parse("com.me.Color", "RED") -> enum Color.RED I have found that if I use an if block containing assignableFrom calls, I can achieve this. But would prefer writing something more extendable, so