method-reference

What's the difference between instance method reference types in Java 8?

筅森魡賤 提交于 2019-11-27 01:27:38
So Java 8 introduces method references and the docs describe the four types . My question is what's the difference between the two instance types? Reference to an instance method of a particular object. Reference to an instance method of an arbitrary object of a particular type. Both refer to references but what's significantly different? Is it that the type inference used to resolve them is different? Is it significant that (in their examples) one is a closure and the other is a lambda? Is it something to do with the number of arguments on a method? Louis Wasserman 1) myString::charAt would

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

谁说我不能喝 提交于 2019-11-26 21:42:20
问题 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

Instance Method Reference and Lambda Parameters

白昼怎懂夜的黑 提交于 2019-11-26 20:58:07
I am having trouble understanding the syntax for a method reference, where there are two parameters a and b , and the reference is to a method of a on b . For example I understand how Arrays.sort(personArray, comparators::compareByName); is equivalent to Arrays.sort(personArray, (o1, o2) -> comparators.compareByName(o1, o2)); because in that case the lambda parameters match the method call parameters (o1, o2) . Howevever for this lambda stream.sorted((o1, o2) -> o1.compareToIgnoreCase(o2)); my IDE tells me that is equivalent to: stream.sorted(String::compareToIgnoreCase); and I am not finding

Java 8 chained method reference?

大兔子大兔子 提交于 2019-11-26 20:56:39
Suppose there is a typical Java Bean: class MyBean { void setA(String id) { } void setB(String id) { } List<String> getList() { } } And I would like to create a more abstract way of calling the setters with the help of a BiConsumer: Map<SomeEnum, BiConsumer<MyBean, String>> map = ... map.put(SomeEnum.A, MyBean::setA); map.put(SomeEnum.B, MyBean::setB); map.put(SomeEnum.List, (myBean, id) -> myBean.getList().add(id)); Is there a way to replace the lambda (myBean, id) -> myBean.getList().add(id) with a chained method reference, something like (myBean.getList())::add or myBean::getList::add or

Java 8: Difference between method reference Bound Receiver and UnBound Receiver

﹥>﹥吖頭↗ 提交于 2019-11-26 20:38:31
I am trying to use Java 8 method references in my code. There are four types of method references available. Static method reference. Instance Method (Bound receiver). Instance Method (UnBound receiver). Constructor reference. With Static method reference and Constructor reference i have no problem, but Instance Method (Bound receiver) and Instance Method (UnBound receiver) really confused me. In Bound receiver, we are using an Object reference variable for calling a method like: objectRef::Instance Method In UnBound receiver we are using Class name for calling a method like: ClassName:

java.lang.NullPointerException is thrown using a method-reference but not a lambda expression

僤鯓⒐⒋嵵緔 提交于 2019-11-26 20:22:55
I've noticed something weird about unhandled exceptions using Java 8 method reference. This is my code, using the lambda expression () -> s.toLowerCase() : public class Test { public static void main(String[] args) { testNPE(null); } private static void testNPE(String s) { Thread t = new Thread(() -> s.toLowerCase()); // Thread t = new Thread(s::toLowerCase); t.setUncaughtExceptionHandler((t1, e) -> System.out.println("Exception!")); t.start(); } } It prints "Exception", so it works fine. But when I change Thread t to use a method-reference (even IntelliJ suggests that): Thread t = new Thread

Why can method reference use non-final variables?

*爱你&永不变心* 提交于 2019-11-26 13:59:05
问题 I had some confusion about inner classes and lambda expression, and I tried to ask a question about that, but then another doubt arose, and It's probable better posting another question than commenting the previous one. Straight to the point: I know (thank you Jon) that something like this won't compile public class Main { public static void main(String[] args) { One one = new One(); F f = new F(){ //1 public void foo(){one.bar();} //compilation error }; one = new One(); } } class One { void

What&#39;s the difference between instance method reference types in Java 8?

自闭症网瘾萝莉.ら 提交于 2019-11-26 09:39:41
问题 So Java 8 introduces method references and the docs describe the four types. My question is what\'s the difference between the two instance types? Reference to an instance method of a particular object. Reference to an instance method of an arbitrary object of a particular type. Both refer to references but what\'s significantly different? Is it that the type inference used to resolve them is different? Is it significant that (in their examples) one is a closure and the other is a lambda? Is

Java 8 chained method reference?

和自甴很熟 提交于 2019-11-26 07:47:17
问题 Suppose there is a typical Java Bean: class MyBean { void setA(String id) { } void setB(String id) { } List<String> getList() { } } And I would like to create a more abstract way of calling the setters with the help of a BiConsumer: Map<SomeEnum, BiConsumer<MyBean, String>> map = ... map.put(SomeEnum.A, MyBean::setA); map.put(SomeEnum.B, MyBean::setB); map.put(SomeEnum.List, (myBean, id) -> myBean.getList().add(id)); Is there a way to replace the lambda (myBean, id) -> myBean.getList().add(id

Java 8: Difference between method reference Bound Receiver and UnBound Receiver

拈花ヽ惹草 提交于 2019-11-26 07:39:49
问题 I am trying to use Java 8 method references in my code. There are four types of method references available. Static method reference. Instance Method (Bound receiver). Instance Method (UnBound receiver). Constructor reference. With Static method reference and Constructor reference i have no problem, but Instance Method (Bound receiver) and Instance Method (UnBound receiver) really confused me. In Bound receiver, we are using an Object reference variable for calling a method like: objectRef: