method-reference

Java Compile Error: Method reference in combination with overloading

匆匆过客 提交于 2019-12-05 02:06:31
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(Runnable) is ambiguous for the type Test . I've added a third method test3 which looks like this: public

Why 'T.super.toString()' and 'super::toString' use a synthetic accessor method?

走远了吗. 提交于 2019-12-04 23:54:33
Consider the following set of expressions: class T {{ /*1*/ super.toString(); // direct /*2*/ T.super.toString(); // synthetic Supplier<?> s; /*3*/ s = super::toString; // synthetic /*4*/ s = T.super::toString; // synthetic }} Which gives the following result: class T { T(); 0 aload_0 [this] 1 invokespecial java.lang.Object() [8] 4 aload_0 [this] 5 invokespecial java.lang.Object.toString() : java.lang.String [10] 8 pop // ^-- direct 9 aload_0 [this] 10 invokestatic T.access$0(T) : java.lang.String [14] 13 pop // ^-- synthetic 14 aload_0 [this] 15 invokedynamic 0 get(T) : java.util.function

Composition of method reference

天涯浪子 提交于 2019-12-04 23:03:06
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. So my questions: Why do we need to type-cast or assign it to a variable first before we can call the

Java Lambda Expression

泪湿孤枕 提交于 2019-12-04 22:10:21
I am currently learning lambda expressions on JDK 1.8. I have come across some code I have found that I do not understand. Here is the code: import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.lang.Comparable; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { List<String> list = Arrays.asList("a", "b", "c"); sort(list, Comparable::<String>compareTo); } interface MyComparable { public <T extends Comparable<T>> int compare(T obj1, T obj2 ); } public static <T extends Comparable<T>> void sort(List<T> list,

Generic way to update pojos via getters and setters

大憨熊 提交于 2019-12-04 20:19:49
问题 Let's say I have POJO with getters and setters of different types. I want to write some generic algorithm for updating data from one to another based on just defining getters and setters via lambdas. I'm trying to create it this way private static final Map<Function<Entity, Object>, BiConsumer<Entity, Object>> ACCESSORS = new HashMap <Function<Entity, Object>, BiConsumer<Entity, Object>>() {{ put(Entity::getAreaCode, Entity::setAreaCode); }}); Then I go through all entries applying target

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

ε祈祈猫儿з 提交于 2019-12-04 16:28:47
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 double op4(double a) { ... } } The method reference to a known method op0 can be obtained as:

Why does invokeLater execute in the main thread?

我们两清 提交于 2019-12-04 08:18:01
问题 I just encountered this "bug", but I'm not sure if this is intended: Code: public static Object someMethod(){ assert SwingUtilities.isEventDispatchThread(); return new Object(); } public static void main(String[] args){ SwingUtilities.invokeLater(() -> someMethod().toString());//First Example SwingUtilities.invokeLater(someMethod()::toString);//Second Example } In the first example someMethod is being executed on the swing Thread, but in the second example it is not, although it should be in

Invalid method reference for overloaded method with different arities

帅比萌擦擦* 提交于 2019-12-04 06:17:59
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 statement produces an error. interface Fun<T, R> { R apply(T t); } public final class Foo { public static void

Limits of static method references in Java 8

我的梦境 提交于 2019-12-04 04:23:36
I'm trying to use method references to capture method invocations and am hitting some limitations. This works fine: <T> void capture(Function<T, ?> in) { } private interface Foo { String getBar(); } capture(Foo::getBar); But if I change the signature of Foo.setBar to something like this: private interface Foo { void setBar(String bar); } capture(Foo::setBar); I get an error: Cannot make a static reference to the non-static method setBar(String) from the type MyTest.Foo It's not clear to me what the restriction is. Ideally I'd like to use method references to capture invocations on standard

Java 8 reference to a static method vs. instance method

a 夏天 提交于 2019-12-04 02:34:12
say I have the following code public class A { int x; public boolean is() {return x%2==0;} public static boolean is (A a) {return !a.is();} } and in another class... List<A> a = ... a.stream().filter(b->b.isCool()); a.stream().filter(A::is); //would be equivalent if the static method is(A a) did not exist the question is how do I refer to the instance method version using the A::is type notation? Thanks a lot nosid In your example, both the static and the non-static method are applicable for the target type of the filter method. In this case, you can't use a method reference, because the