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

How does key extraction work with the Java Comparator.comparing() method?

The Java API specifies the following for the comparing() method in the Comparator interface: static > Comparator comparing (Function keyExtractor) Accepts a function that extracts a…
0
votes
0 answers

How to assign a method as an attribute to use it later on any compatible object?

I am trying to create a generic menu over an arbitrary type T implementing a Menuable interface: public interface Procedure { void invoke(); } public class Menu implements Menuable { List items; //The items of the…
0
votes
0 answers

How can a Java Predicate allow a method without argument?

From what I have studied, Predicate is a functional interface that has an abstract method boolean test(T var1);. So if I use the method reference of Java 8 and pass it to a function getAllVegeterianDish(List dish, Predicate p defined in…
Niraj Chowdhary
  • 123
  • 1
  • 9
0
votes
2 answers

How Java treat method reference of instance methods?

I'm reading Oracle's Java SE tutorial on method reference and I came across the problem of method reference's mechanism of parsing instance methods. In the tutorial, there's a snippet using method reference as a Comparator
cmhzc
  • 60
  • 1
  • 8
0
votes
2 answers

Graphics2D and GUI: ArrayList of methods, method reference error

Imagining I have an arrayList of three JButtons: b_line, b_rect, b_oval, whose functionalities are very similar - they draw line/rectangle/oval on the JFrame. Instead of writing actionListener for all three of them manually, I am thinking about…
Yige Song
  • 333
  • 6
  • 16
0
votes
2 answers

Method references on instances - why not call method directly (because how can it execute in a different context)?

I am trying to understand this example code from Oracle Learning on Lambdas and Method References: String city = "Munich"; Supplier lambda = city::toUpperCase; System.out.println(lambda.get()); Why didn't they simply…
likejudo
  • 3,396
  • 6
  • 52
  • 107
0
votes
1 answer

How a Method Reference to an instance method is working without new() in this example?

In Collectors.groupingBy(Student::getGrade,....); getGrade() method is used without new keyword. Method getGrade is not a static method. How it is working without new class TestClass { public static void main(String[] args) { var ls =…
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
0
votes
0 answers

Method Reference using pre defined functional interfaces

I am trying out few things related to method references using functional interfaces. I wanted to convert a string into the upper case using a bounded receiver and an unbounded receiver. Although I have understood print1 and print2 methods, I am not…
skgv
  • 47
  • 1
  • 3
0
votes
1 answer

Compile error for method reference in multiple exception handling

There is an interesting situation I have encountered. I have 2 exception classes MyException1 and MyException2. During multiple catch(MyException1 | MyException2 ex) SonarLint suggests using method reference (ex::getExitCode) instead of the lambda…
nikli
  • 2,281
  • 2
  • 24
  • 38
0
votes
1 answer

Java. How to prevent assigning a method reference to functional interface type argument?

Beginning with Java SE 8, if the formal parameter of a method is a functional interface, the argument can be either an object implementing that interface or a reference to some method. It means that the argument can also be a reference to a method…
Chris S
  • 151
  • 8
0
votes
0 answers

Visual Studio 2019 Community Edition method references not displayed in Editor

There is no option of references above the method, property, or any declaration in Visual Studio 2019 Community Edition. If this option available in Visual Studio 2019 Community Edition then please let me know. I also attached an image for…
0
votes
3 answers

return method refference to stream

I have a class Care class Car{ private int wheels; private int doors; ... public int getWheels(){ return wheels;} public int getDoors(){ return doors:} } And I have a collection of the cars List cars = ... I want to calculate…
Johnyb
  • 980
  • 1
  • 12
  • 27
0
votes
1 answer

How knows Stream.toArray(Book[]::new); how many elements the array has and where is the implementation of “Book[]::new”

I'm triying to understand method references and I don't know how with this "Book[]::new" it can create an array with the right number of elements. Book[] arrayBook = stBooks.toArray(Book[]::new); In order to create the array when I use the second…
Marquake
  • 191
  • 1
  • 3
  • 10
0
votes
1 answer

use of Method Reference in stream().map(Function) java 8

I have seen this code many places but I don't understand how we can pass string.toUpperCase as a method reference in "stream.map(-)" function.? see the below code: List myList = Arrays.asList("india", "australia",…
Abhishek Singh
  • 1,367
  • 1
  • 22
  • 46
0
votes
1 answer

Java Supplier vs Supplier

I know it may have something to do with Generic and Covariant return types. But I can not make it through. Given two classes where Apple extends Fruit. public class TestReturnValue { public static Supplier conFruitExt =…
SuN
  • 421
  • 5
  • 11