Method References
Method references provide a concise way to refer to existing methods or constructors as alternatives to writing full lambda expressions. They use a double colon (::) syntax to point directly to a method or constructor, making your code more readable and often less verbose. There are three main kinds of method references in Java:
- Static method references: refer to static methods of a class using the syntax
ClassName::staticMethodName; - Instance method references: refer to instance methods of a particular object or of an arbitrary object of a particular type using the syntax
instance::instanceMethodNameorClassName::instanceMethodName; - Constructor references: refer to a class constructor using the syntax
ClassName::new.
Using method references can make your code easier to read and maintain, especially when the lambda expression simply calls an existing method.
Main.java
12345678910111213141516171819202122232425262728293031// File: Main.java package com.example; import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; public class Main { public static void main(String[] args) { // Static method reference Function<String, Integer> parseInt = Integer::parseInt; System.out.println(parseInt.apply("123")); // prints 123 // Instance method reference of a particular object String myString = "hello"; Supplier<String> toUpper = myString::toUpperCase; System.out.println(toUpper.get()); // prints HELLO // Instance method reference of an arbitrary object of a particular type List<String> words = Arrays.asList("apple", "banana", "cherry"); words.forEach(System.out::println); // Constructor reference Supplier<StringBuilder> stringBuilderSupplier = StringBuilder::new; StringBuilder sb = stringBuilderSupplier.get(); sb.append("Lambda"); System.out.println(sb.toString()); // prints Lambda } }
Method references are often used as a shorthand for lambda expressions that just call a single method. For example, list.forEach(s -> System.out.println(s)); can be replaced with list.forEach(System.out::println);. Method references can only be used when the parameter types of the functional interface match the method signature being referenced. In general, you should use a method reference when it improves clarity and conciseness, but stick with a lambda when you need to add logic or pass parameters in a way that does not directly match a method signature.
Main.java
123456789101112131415package com.example; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "kiwi", "cherry"); // Using a method reference to sort by string length fruits.sort(Comparator.comparingInt(String::length)); System.out.println(fruits); // prints [kiwi, apple, banana, cherry] } }
1. Which symbol is used to write a method reference in Java?
2. What is the main difference between a static method reference and an instance method reference?
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 5.56
Method References
Swipe um das Menü anzuzeigen
Method references provide a concise way to refer to existing methods or constructors as alternatives to writing full lambda expressions. They use a double colon (::) syntax to point directly to a method or constructor, making your code more readable and often less verbose. There are three main kinds of method references in Java:
- Static method references: refer to static methods of a class using the syntax
ClassName::staticMethodName; - Instance method references: refer to instance methods of a particular object or of an arbitrary object of a particular type using the syntax
instance::instanceMethodNameorClassName::instanceMethodName; - Constructor references: refer to a class constructor using the syntax
ClassName::new.
Using method references can make your code easier to read and maintain, especially when the lambda expression simply calls an existing method.
Main.java
12345678910111213141516171819202122232425262728293031// File: Main.java package com.example; import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; public class Main { public static void main(String[] args) { // Static method reference Function<String, Integer> parseInt = Integer::parseInt; System.out.println(parseInt.apply("123")); // prints 123 // Instance method reference of a particular object String myString = "hello"; Supplier<String> toUpper = myString::toUpperCase; System.out.println(toUpper.get()); // prints HELLO // Instance method reference of an arbitrary object of a particular type List<String> words = Arrays.asList("apple", "banana", "cherry"); words.forEach(System.out::println); // Constructor reference Supplier<StringBuilder> stringBuilderSupplier = StringBuilder::new; StringBuilder sb = stringBuilderSupplier.get(); sb.append("Lambda"); System.out.println(sb.toString()); // prints Lambda } }
Method references are often used as a shorthand for lambda expressions that just call a single method. For example, list.forEach(s -> System.out.println(s)); can be replaced with list.forEach(System.out::println);. Method references can only be used when the parameter types of the functional interface match the method signature being referenced. In general, you should use a method reference when it improves clarity and conciseness, but stick with a lambda when you need to add logic or pass parameters in a way that does not directly match a method signature.
Main.java
123456789101112131415package com.example; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { List<String> fruits = Arrays.asList("apple", "banana", "kiwi", "cherry"); // Using a method reference to sort by string length fruits.sort(Comparator.comparingInt(String::length)); System.out.println(fruits); // prints [kiwi, apple, banana, cherry] } }
1. Which symbol is used to write a method reference in Java?
2. What is the main difference between a static method reference and an instance method reference?
Danke für Ihr Feedback!