How to use a method reference on a static import?

Let’s look at the relevant part of the Java Language Specification, 15.13. Method Reference Expressions. It lists the following ways to a create method reference: MethodReference: ExpressionName :: [TypeArguments] Identifier ReferenceType :: [TypeArguments] Identifier Primary :: [TypeArguments] Identifier super :: [TypeArguments] Identifier TypeName . super :: [TypeArguments] Identifier ClassType :: [TypeArguments] new ArrayType :: new … 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

Java8 method reference used as Function object to combine functions

You can write a static method to do this: import java.util.function.*; class Test { public static void main(String[] args) { Function<String, Integer> function = combine(String::length, n -> n * 2); System.out.println(function.apply(“foo”)); } public static <T1, T2, T3> Function<T1, T3> combine( Function<T1, T2> first, Function<T2, T3> second) { return first.andThen(second); } } You could then put … Read more

Why is lambda return type not checked at compile time?

In the first example, MyInterface::getLength and “I am NOT an Integer” helped to resolve the generic parameters T and R to MyInterface and Serializable & Comparable<? extends Serializable & Comparable<?>>respectively. // it compiles since String is a Serializable Function<MyInterface, Serializable> function = MyInterface::getLength; Builder.of(MyInterface.class).with(function, “I am NOT an Integer”); MyInterface::getLength is not always a Function<MyInterface, … Read more

Comparator.reversed() does not compile using lambda

This is a weakness in the compiler’s type inferencing mechanism. In order to infer the type of u in the lambda, the target type for the lambda needs to be established. This is accomplished as follows. userList.sort() is expecting an argument of type Comparator<User>. In the first line, Comparator.comparing() needs to return Comparator<User>. This implies … 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

Use method reference with parameter

You can’t use method references for this purpose. You have to resort to lambda expressions. The reason why the bind2 method of the linked question doesn’t work is that you are actually trying to bind two parameters to convert a three-arg function into a one-arg function. There is no similarly simple solution as there is … Read more

New object instantiation when using Java 8 streams

Generally there’s no difference. NewClass::new produces less bytecode as in lambda version an auto-generated private method is created by java compiler from the lambda body while NewClass:new directly links to the constructor method handle. So using method references you may have slightly less class file size. No significant performance difference is expected though. Another difference … Read more

tech