method-reference

is there a lambda function that cannot be a method reference

那年仲夏 提交于 2019-12-07 05:21:10
问题 in a lecture I gave at my company I suggested converting any complex lambda to a method reference (more readable and better debug and testing) and was asked if it is always possible. I searched and could not find a lambda that cannot be replaced with method reference. am I right? (lambda can always be replaced with method reference) 回答1: Method reference cannot capture variables. So a capturing lambda cannot be directly converted to a method reference. For example, int x = 1; numbers

Java Compile Error: Method reference in combination with overloading

て烟熏妆下的殇ゞ 提交于 2019-12-06 19:36:33
问题 I have the following class with an overloaded method: import java.util.ArrayList; import java.util.concurrent.Callable; public abstract class Test { public void test1 () { doStuff (ArrayList::new); // compilation error } public void test2 () { doStuff ( () -> new ArrayList<> ()); } public abstract void doStuff (Runnable runable); public abstract void doStuff (Callable<ArrayList<String>> callable); } The method test1 results in a compilation error with the error message The method doStuff

Composition of method reference

最后都变了- 提交于 2019-12-06 19:02:54
问题 This is related to this question: How to do function composition? I noticed that a method reference can be assigned to a variable declared as Function , and so I assume it should have andThen or compose function, and hence I expect that we can compose them directly. But apparently we need to assign them to a variable declared as Function first (or type-cast before invocation) before we can call andThen or compose on them. I suspect I might have some misconception about how this should work.

Why does a method reference to ctor that “throws” … throw as well?

扶醉桌前 提交于 2019-12-06 16:58:59
问题 I am looking for an elegant way to create a factory for dependency injection. In my case, the factory simply has to call a one-argument constructor. I found this answer outlining how to use a Function<ParamType, ClassToNew> for such purposes. But my problem is: in my case, my ctor declares to throw some checked exception. What I don't get: creating that Function using a method reference to that constructor doesn't work. As in: import java.util.function.Function; public class Mcve { public

Why class/object name must be explicitly specified for method references?

不羁岁月 提交于 2019-12-06 11:16:55
When I want to refer to the method in the current scope I still need to specify class name (for static methods) or this before :: operator. For example, I need to write: import java.util.stream.Stream; public class StreamTest { public static int trimmedLength(String s) { return s.trim().length(); } public static void main(String[] args) { System.out.println(Stream.of(" aaa ", " bb ", " c ") .mapToInt(StreamTest::trimmedLength).sum()); } } It's not so big problem for this , but sometimes look overcrowded for static methods as the class name can be quite long. It would be nice if compiler

Differences between using a method reference and function object in stream operations?

北城余情 提交于 2019-12-06 09:27:49
问题 When using Java 8 streams I often find that I need to refactor a multi-statement lambda expression. I will illustrate this with a simple example. Assume I have started writing this code: Stream.of(1, 3).map(i -> { if (i == 1) { return "I"; } else if (i == 3) { return "E"; } return ""; }).forEach(System.out::println); Now I am not very fond of the large lambda expression in the map call. Hence, I want to refactor it out of there. I see two options, either I make an instance of Function in my

How to turn a List<Item> to a Map<Item, completeablefuture<xyz>>

…衆ロ難τιáo~ 提交于 2019-12-06 06:08:12
I have an async method with a completeablefuture result: public CompletableFuture<DogLater> asyncDogLater(String dogName){} I have a list of dogs: List<Dog> dogs; Now, I want to create a map from the dog's name to the Completeablefuture: Map<String, CompletableFuture<DogLater>> map; After checking this and this I was trying to do so: Map<String, CompletableFuture<DogLater>> completableFutures = dogs.stream() .collect( Collectors.toMap(Dog::getName, asyncDogLater(Dog::getName ))); But the compiler complains that the first Dog::getName is problematic since: Non-static method cannot be referenced

Rewrite Java code in Kotlin using Function Reference occurs SAM types conflict

无人久伴 提交于 2019-12-05 15:10:47
I have an example Java code using method reference that I want to rewrite to Kotlin. Java version is using method reference, solution is short and clear. But on the other hand I cannot use method reference in Kotlin. The only version that I've managed to write is one presented below. It seems like Function3 { s: String, b: Boolean, i: Int -> combine(s, b, i) } could be written in cleaner way (if possible method reference would be perfect). I'm new to Kotlin so I'll be grateful for any clues. Java import io.reactivex.Observable; public class TestJava { Observable<String> strings() { return

How can I create a list of method references?

青春壹個敷衍的年華 提交于 2019-12-05 13:02:16
I have a need to work through a list and for each item call a different method on a target object. It seems elegant that I could just create a list of method references to do this, so for each index in the list, I could call the appropriate method reference that corresponds to it. private final static List<Consumer<String>> METHODS = (List<Consumer<String>>) Arrays.asList( TargetClass::setValue1, TargetClass::setValue2, TargetClass::setValue3, TargetClass::setValue4, TargetClass::setValue5); However, Eclipse is flagging these with the error The target type of this expression must be a

is there a lambda function that cannot be a method reference

笑着哭i 提交于 2019-12-05 09:51:51
in a lecture I gave at my company I suggested converting any complex lambda to a method reference (more readable and better debug and testing) and was asked if it is always possible. I searched and could not find a lambda that cannot be replaced with method reference. am I right? (lambda can always be replaced with method reference) Method reference cannot capture variables. So a capturing lambda cannot be directly converted to a method reference. For example, int x = 1; numbers.replaceAll(n -> n + x); In some cases, if only one variable is captured, it might be possible to convert lambda to a