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
-1
votes
1 answer

How to get Constructor Method Reference using reflection in Java

I have a library that I am using in a project. It has a class which can be instantiated as A object = new A(new B(value), C::new, D::new); None of the classes A, B, C, D are public, although the constructors are. So, I want to use Reflection to…
Ashok Bommisetti
  • 314
  • 1
  • 3
  • 15
-2
votes
2 answers

What is exactly a reference to an Instance Method of an Arbitrary Object of a Particular Type?

What is exactly an arbitrary object of a particular type? I didn't understand the part of code (String::compareToIgnoreCase), How did we made this reference? String[] stringArray = { "Barbara", "James", "Mary","John","Patricia", "Robert"};…
Ouiame
  • 21
  • 5
-2
votes
1 answer

Method reference - non-static method cannot be referenced from a static context

I'm trying to create a list of suppliers. I have ClassA with instance methods getOne(), getTwo(). Then In ClassB, I have a method to return a list of suppliers as below: public List> getData(){ return Arrays.asList( …
iPhoneJavaDev
  • 821
  • 5
  • 33
  • 78
-2
votes
1 answer

Java 8 method references with javafx

I just developed a JavaFX applications this twenty different pages. Each page has a table and I wanted to place a context menu on each table. Basically its always the same code for placing the context menu to the table but I am hoping that method…
Hauke
  • 1,405
  • 5
  • 23
  • 44
-2
votes
2 answers

Java Lambda Expression

I am currently learning lambda expressions on JDK 1.8. I have come across some code I have found that I do not understand. Here is the code: import java.util.Arrays; import java.util.Comparator; import java.util.List; import…
shodz
  • 344
  • 3
  • 13
-3
votes
1 answer

Why is the double colon operator needed in Java?

I'm coming from a JavaScript background and I'm seeing the :: operator for the first time. I understand what it does, but I'm wondering why it's necessary. In JavaScript, something like this is perfectly valid: [1, 2,…
NoBullsh1t
  • 483
  • 4
  • 14
-3
votes
1 answer

function.Function as parameter in print method

I dont have any clue how to approach the problem i am facing. We have to print out an array using function.Function as a parameter and using lambas to print out 1. the even and 2. the odd numbers. final class Main { private Main() {} public…
noob
  • 1
  • 1
-3
votes
1 answer

Using method references of Java 8

I am using predicates supported in Java 8, in which I am using lambdas, but I want to use method references in place of lambdas. Please advise how to achieve this. Below is my code. public class Employee { public Employee(Integer id, Integer…
user1620642
  • 79
  • 10
-4
votes
1 answer

Return type mismatch and Java compiles

I am curious as to how the below code compiles @Bean(name = "ErrorDecoder") public ErrorDecoder streamHubErrorDecoder() { return FeignException::errorStatus; } The type of ErrorDecoder…
Veeraraghavan N
  • 839
  • 1
  • 10
  • 19
-4
votes
1 answer

Where I could find an explation about use the word "or" in the signature of a java method?

I have to explain my subject because I don't know how to search this on google... I need just a hint. I'm doing a tutorial of Java about Reference Methods and Lamdba. "How to filter a person in a list of people". I'm not worried about how I can do…
Marquake
  • 191
  • 1
  • 3
  • 10
-4
votes
2 answers

Java 8 method reference to class instance method NPE

import java.util.function.Function; public class Playground { public static void main (String[] args) { Object o = null; System.out.println(o); Function toStringFunc = Object::toString; String s =…
mpsrig
  • 23
  • 2
-5
votes
1 answer

Java 8 removeif with method reference

I am going over some Java 8 code and observed something like below, can someone please help me understand what it actually means? hashSetA.removeif(hasSetB::remove);
1 2 3
37
38