method-reference

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

[亡魂溺海] 提交于 2019-12-22 18:52:42
问题 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

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

╄→гoц情女王★ 提交于 2019-12-22 18:52:23
问题 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

How to invoke parameterized method with method reference

徘徊边缘 提交于 2019-12-22 18:36:29
问题 Consider below code, interface TestInter { public void abc(); } class DemoStatic { public static void testStatic(String abc) { System.out.println(abc); } public void runTest () { // Using lambda expression. String str = "demo string" ; TestInter demo1 = () -> DemoStatic.testStatic(str); demo1.abc(); // Using method reference. TestInter demo2 = DemoStatic::testStatic; // This line is not compiling. demo2.abc(); } } We can invoke the testStatic method as body of TestInter interface's abc()

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

半世苍凉 提交于 2019-12-22 08:35:57
问题 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.

How to get Method Reference for all methods in a class (Java)?

故事扮演 提交于 2019-12-21 21:36:30
问题 Method Reference for a specific method in Java 8 can be obtained as Class::Method . But how to get the method reference of all methods of a class? All the desired methods have different method names, but the same type signature. Also, the names of the methods is not known before hand. Example: class Test { public static double op0(double a) { ... } public static double op1(double a) { ... } public static double op2(double a) { ... } public static double op3(double a) { ... } public static

Invalid method reference for overloaded method with different arities

最后都变了- 提交于 2019-12-21 12:13:51
问题 When trying to compile the expression Comparator.comparing(String::toLowerCase) , the Java Compiler returns an error. See the following question for more information: Why Comparator.comparing doesn't work with String::toLowerCase method reference? I have tried to reduce the problem as much as possible. In particular, I have removed almost all dependencies to other classes. The main method contains two method invocations. The first statement compiles without errors, whereas the second

How does method reference casting work?

 ̄綄美尐妖づ 提交于 2019-12-19 08:59:40
问题 public class Main { interface Capitalizer { public String capitalize(String name); } public String toUpperCase() { return "ALLCAPS"; } public static void main(String[] args) { Capitalizer c = String::toUpperCase; //This works c = Main::toUpperCase; //Compile error } } Both are instance methods with same signature. Why does one work and the other doesn't? Signature of String::toUpperCase : String toUpperCase(); 回答1: There are 3 constructs to reference a method: object::instanceMethod Class:

Method references to raw types harmful?

巧了我就是萌 提交于 2019-12-19 06:04:58
问题 The code below contains a reference to Enum::name (notice no type parameter). public static <T extends Enum<T>> ColumnType<T, String> enumColumn(Class<T> klazz) { return simpleColumn((row, label) -> valueOf(klazz, row.getString(label)), Enum::name); } public static <T, R> ColumnType<T, R> simpleColumn(BiFunction<JsonObject, String, T> readFromJson, Function<T, R> writeToDb) { // ... } Javac reports a warning during compilation: [WARNING] found raw type: java.lang.Enum missing type arguments

Method references to raw types harmful?

一世执手 提交于 2019-12-19 06:04:22
问题 The code below contains a reference to Enum::name (notice no type parameter). public static <T extends Enum<T>> ColumnType<T, String> enumColumn(Class<T> klazz) { return simpleColumn((row, label) -> valueOf(klazz, row.getString(label)), Enum::name); } public static <T, R> ColumnType<T, R> simpleColumn(BiFunction<JsonObject, String, T> readFromJson, Function<T, R> writeToDb) { // ... } Javac reports a warning during compilation: [WARNING] found raw type: java.lang.Enum missing type arguments

Java compiler: How can two methods with the same name and different signatures match a method call?

我的梦境 提交于 2019-12-18 15:50:39
问题 I have this class called Container : public class Container { private final Map<String, Object> map = new HashMap<>(); public void put(String name, Object value) { map.put(name, value); } public Container with(String name, Object value) { put(name, value); return this; } public Object get(String name) { return map.get(name); } public <R> R get(String name, Function<Object, R> mapper) { Object value = get(name); if (null == value) { return null; } return mapper .apply(value); } public <R> R