Questions tagged [method-reference]

Method references, introduced in Java 8, are a way to refer to a method by its name instead of using a lambda expression.

Method references were introduced in alongside lambda expressions.

The programmer can use a method reference as a syntactic sugar for a lambda expression that would merely call that method, when its signature is compatible with the type.

The syntax of a method reference closely matches a method invocation expression, but with a double colon (::) replacing the dot (.).

// static method reference:
ToIntFunction<String> parseInt = Integer::parseInt;
int i = parseInt.applyAsInt("123");

// instance method reference:
Consumer<Object> println = System.out::println;
println.accept("Hello world!");

// constructor method reference:
Supplier<Object> newObject = Object::new;
Object o = newObject.get();

// array creation method reference:
IntFunction<Number[]> newArray = Number[]::new;
Number[] a = newArray.apply(10);

When working with generics, type arguments may be provided explicitly or inferred.

Supplier<List<String>> listSupplier;

// explicit type argument for a class:
listSupplier = ArrayList<String>::new;
// inferred type argument for a class:
listSupplier = LinkedList::new;
//             ^^^^^^^^^^
//             note: not a raw type in this context!
//             LinkedList<String> will be inferred.

// explicit type argument for a method:
listSupplier = Collections::<String>emptyList;
// inferred type argument for a method:
listSupplier = Collections::emptyList;

See also:

567 questions
0
votes
1 answer

Data Structures Containing Polymorphic Method References

The line associations.put("test1",B::setBeta); below does not compile. I'm not clear why it won't work since B extends A. Is there a way to make this work? I'm trying to build a map of method references from an inheritance family. import…
KevinRethwisch
  • 237
  • 2
  • 13
0
votes
1 answer

Java 8 unbound reference syntax struggle

I'm trying to create a method that puts a Function's results in to a Consumer you using unbound references (I think). Here's the scenario. With JDBC's ResultSet you can get row values by index. I have a Bean instance I want to place selected…
nwillc
  • 103
  • 1
  • 6
0
votes
1 answer

Comparator: Method references not supported at this language level?

I wrote the following line of code for a Java 8 project: Comparator oldestPerson= Comparator.comparing(Person::getOldestBirthday); It is a simple comparator, however I am trying to use this code In a project that only uses Java 7, therefore…
java123999
  • 6,974
  • 36
  • 77
  • 121
0
votes
2 answers

Java Lambda vs Method Reference - One does not receive the local vars of the caller

Trying to refactor from a lambda to a method reference I realized that there seems to be a difference in method references not getting the local variables of the caller (the lexical scope?). When using a lambda as its inline code there isn't problem…
xetra11
  • 7,671
  • 14
  • 84
  • 159
0
votes
1 answer

Are there method references to the base math functions?

For example, I can specify Math::cbrt to tell the lambda to take the cube root of the value. Can I refer to multiplication and division functions using method references? Obviously I can make my own lambda but it would be nice to have consistency.
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0
votes
0 answers

Functional reference in foreach with more than one arguments

I have some situation where I want to pass method reference of a method which have more than one arguments in forEach, I'm able to pass using single arg method which is execute(Map page) but I do not know how could I pass execute(Map page, Integer…
Assad
  • 31
  • 2
0
votes
0 answers

Method reference giving run time error, where as Supplier Interface works

I have following code to generate summary for the list of values(Integer for this example). When I use method reference to get the values it is failing at run time giving ArrayIndexOutOfBoundException, but supplier is working fine. static void…
Chota Bheem
  • 1,106
  • 1
  • 13
  • 31
0
votes
1 answer

Java: Getting object from bound-method-reference

TL;DR Is there some way to get my_object from within a function do_something that is called with a method-reference: other_object.do_something(my_object::some_method); Full Story... Background: I'm making a system wherein modules communicate by…
Peter
  • 12,274
  • 9
  • 71
  • 86
0
votes
1 answer

Java8 expressing conditionals as an array of method reference

I am representing a huge swath of objects (specifically MIPS32-instructions). My minimum working example will be describing an instruction in the R-format. MIPS32 background (R-type instruction) An R-type instruction is determined uniquely by the…
0
votes
0 answers

Constructor method reference for IntSupplier implementation for a Generator causes compilation error

Why does this code not compile? package com.heather; import java.util.function.IntSupplier; import java.util.stream.IntStream; public class MainClass { public static void main(String[] args) { class IntSupplierImpl implements…
tafoo85
  • 869
  • 8
  • 12
0
votes
0 answers

error: incompatible types: invalid method reference?

Learning Method References in java 8 public class ReferenceToStaticMethod { public static void main(String[] args) { // TODO code application logic here List numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,15,16); …
user3198603
  • 5,528
  • 13
  • 65
  • 125
0
votes
1 answer

In Java why cant I cast to a subtype, using a lambda expression within a forEach?

I've written some code to try to better explain what it is I'm trying to achieve. public interface Car extends ViewCar { static Car getInstance(String make, String model){ NewCar newCar = new ConcreteCar(); …
Adam.J
  • 2,519
  • 3
  • 14
  • 12
0
votes
4 answers

Is there a method reference in JDK for subtracting doubles?

Double::sum is a method reference for adding doubles, ie. (a,b) -> a+b. Why is there not a method reference for minus in the JDK? I.e. (a,b) -> a-b?
Graeme Moss
  • 7,995
  • 4
  • 29
  • 42
0
votes
1 answer

Difference between the two method references to instance methods

import java.util.List; import java.util.function.*; interface SomeTest { boolean test(T n, T m); } class MyClass { boolean myGenMeth(T x, T y) { boolean result = false; // ... return result; …
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
0
votes
1 answer

Java 8 method references - dereferenced only once?

I'm confused with method references. Consider the following script. public class Main { static interface I { void m(); } static class A implements I { @Override public void m() { …
user3479249