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

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

How do Java 8 array constructor references work?

You can find out yourself by decompiling the java bytecode: javap -c -v -p MyClass.class The compiler desugars array constructor references Foo[]::new to a lambda (i -> new Foo[i]), and then proceeds as with any other lambda or method reference. Here’s the disassembled bytecode of this synthetic lambda: private static java.lang.Object lambda$MR$new$new$635084e0$1(int); descriptor: (I)Ljava/lang/Object; flags: … 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

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

Horrendous performance & large heap footprint of Java 8 constructor reference?

In the first case (ArrayList::new) you are using the constructor which takes an initial capacity argument, in the second case you are not. A large initial capacity (index in your code) causes a large Object[] to be allocated, resulting in your OutOfMemoryErrors. Here are the two constructors’ current implementations: public ArrayList(int initialCapacity) { if (initialCapacity … Read more

Java Pass Method as Parameter

Edit: as of Java 8, lambda expressions are a nice solution as other answers have pointed out. The answer below was written for Java 7 and earlier… Take a look at the command pattern. // NOTE: code not tested, but I believe this is valid java… public class CommandExample { public interface Command { public … Read more