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

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

Please Explain Java 8 Method Reference to instance Method using class name

Equivalent lambda expression of HighTemp::lessThanTemp is (highTemp1, highTemp2) -> { return highTemp1.lessThanTemp(highTemp2); } This is one of the features of Java8 named Reference to an Instance Method of an Arbitrary Object of a Particular Type Consider following example, interface FIface<T> { int testMethod(T a, T b); } class Test2 { private String str; Test2(String str) … 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

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 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 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

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).