Reference to methods with different parameters in Java8

From the Oracle method references tutorial: Reference to an Instance Method of an Arbitrary Object of a Particular Type The following is an example of a reference to an instance method of an arbitrary object of a particular type: String[] stringArray = { “Barbara”, “James”, “Mary”, “John”, “Patricia”, “Robert”, “Michael”, “Linda” }; Arrays.sort(stringArray, String::compareToIgnoreCase); The … Read more

Static context cannot access non-static in Collectors

Unfortunately, the error message “Non-static method cannot be refered from a static context.” is just a place-holder for any type mismatch problem, when method references are involved. The compiler simply failed to determine the actual problem. In your code, the target type Map<Integer, Map<String, List<String>>> doesn’t match the result type of the combined collector which … Read more

What does “an Arbitrary Object of a Particular Type” mean in java 8?

The example given from the Oracle Doc linked is: String[] stringArray = { “Barbara”, “James”, “Mary”, “John”, “Patricia”, “Robert”, “Michael”, “Linda” }; Arrays.sort(stringArray, String::compareToIgnoreCase); The lambda equivalent of String::compareToIgnoreCase would be (String a, String b) -> a.compareToIgnoreCase(b) The Arrays.sort() method is looking for a comparator as its second argument (in this example). Passing String::compareToIgnoreCase creates … Read more

Java 8 chained method reference?

No, method references do not support chaining. In your example it wouldn’t be clear which of the two methods ought to receive the second parameter. But if you insist on it… static <V,T,U> BiConsumer<V,U> filterFirstArg(BiConsumer<T,U> c, Function<V,T> f) { return (t,u)->c.accept(f.apply(t), u); } … BiConsumer<MyBean, String> c = filterFirstArg(List::add, MyBean::getList); The naming of the method … Read more

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

myString::charAt would take an int and return a char, and might be used for any lambda that works that way. It translates, essentially, to index -> myString.charAt(index). String::length would take a String and return an int. It translates, essentially, to string -> string.length(). String::charAt would translate to (string, index) -> string.charAt(index).

Instance Method Reference and Lambda Parameters

I think you’re looking for JLS section 15.13.3, which includes: If the form is ReferenceType :: [TypeArguments] Identifier, the body of the invocation method similarly has the effect of a method invocation expression for a compile-time declaration which is the compile-time declaration of the method reference expression. Run-time evaluation of the method invocation expression is … Read more

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

This behaviour relies on a subtle difference between the evaluation process of method-references and lambda expressions. From the JLS Run-Time Evaluation of Method References: First, if the method reference expression begins with an ExpressionName or a Primary, this subexpression is evaluated. If the subexpression evaluates to null, a NullPointerException is raised, and the method reference … Read more

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

The idea of the unBound receiver such as String::length is you’re referring to a method of an object that will be supplied as one of the lambda’s parameters. For example, the lambda expression (String s) -> s.toUpperCase() can be rewritten as String::toUpperCase. But Bounded refers to a situation when you’re calling a method in a … Read more

What is the equivalent lambda expression for System.out::println

The method reference System.out::println will evaluate System.out first, then create the equivalent of a lambda expression which captures the evaluated value. Usually, you would useo -> System.out.println(o) to achieve the same as the method reference, but this lambda expression will evaluate System.out each time the method will be called. So an exact equivalent would be: … Read more

Is method reference caching a good idea in Java 8?

You have to make a distinction between frequent executions of the same call-site, for stateless lambda or stateful lambdas, and frequent uses of a method-reference to the same method (by different call-sites). Look at the following examples: Runnable r1=null; for(int i=0; i<2; i++) { Runnable r2=System::gc; if(r1==null) r1=r2; else System.out.println(r1==r2? “shared”: “unshared”); } Here, the … Read more

tech