- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement an instance method of an arbitrary object using method reference in Java?
A method reference is a new feature in Java 8, which is related to Lambda Expression. It can allow us to reference constructors or methods without executing them. The method references and lambda expressions are similar in that they both require a target type that consists of a compatible functional interface.
Syntax
Class :: instanceMethodName
Example
import java.util.*; import java.util.function.*; public class ArbitraryObjectMethodRefTest { public static void main(String[] args) { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Raja", 30)); persons.add(new Person("Jai", 25)); persons.add(new Person("Adithya", 20)); persons.add(new Person("Surya", 35)); persons.add(new Person("Ravi", 32)); List ages = ArbitraryObjectMethodRefTest.listAllAges(persons, Person :: getAge); System.out.println("Printing out all ages:
" + ages); } private static List listAllAges(List person, Function<Person, Integer> f) { List result = new ArrayList(); person.forEach(x -> result.add(f.apply((Person)x))); return result; } private static class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } }
Output
Printing out all ages: [30, 25, 20, 35, 32]
- Related Articles
- How to implement reference to an instance method of a particular object in Java?
- How to implement an instance method reference using a class name in Java?
- Reference to an instance method using method references in Java8
- How to implement an action listener using method reference in Java?
- How to implement LongPredicate using lambda and method reference in Java?
- How to implement LongSupplier using lambda and method reference in Java?
- How to implement DoubleUnaryOperator using lambda and method reference in Java?
- How to implement DoublePredicate using lambda and method reference in Java?
- How to implement LongBinaryOperator using lambda and method reference in Java?
- How to implement LongFunction using lambda and method reference in Java?
- How to implement DoubleSupplier using lambda and method reference in Java?
- How to implement IntToDoubleFunction using lambda and method reference in Java?
- How to implement IntToLongFunction using lambda and method reference in Java?
- How to implement LongToIntFunction using lambda and method reference in Java?
- How to implement LongToDoubleFunction using lambda and method reference in Java?

Advertisements