Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Method References | Advanced Lambda Usage
Quizzes & Challenges
Quizzes
Challenges
/
Lambda Expressions in Java

bookMethod 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::instanceMethodName or ClassName::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

Main.java

copy
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

Main.java

copy
123456789101112131415
package 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?

question mark

Which symbol is used to write a method reference in Java?

Select the correct answer

question mark

What is the main difference between a static method reference and an instance method reference?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookMethod References

Desliza para mostrar el menú

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::instanceMethodName or ClassName::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

Main.java

copy
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

Main.java

copy
123456789101112131415
package 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?

question mark

Which symbol is used to write a method reference in Java?

Select the correct answer

question mark

What is the main difference between a static method reference and an instance method reference?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 1
some-alt