Answer
Method References in Java 8
A method reference is a compact shorthand for a lambda expression that calls an existing method. Uses :: operator.
Syntax
CODE
ClassName::methodName or objectName::methodName
4 Types of Method References
1. Static Method Reference — ClassName::staticMethod
Java
// Lambda
Function<String, Integer> parseInt = s -> Integer.parseInt(s);
// Method reference — cleaner
Function<String, Integer> parseInt2 = Integer::parseInt;
System.out.println(parseInt2.apply("42")); // 42
// With streams
List<String> numbers = Arrays.asList("1", "2", "3", "4");
List<Integer> ints = numbers.stream()
.map(Integer::parseInt) // same as s -> Integer.parseInt(s)
.collect(Collectors.toList());
2. Instance Method Reference (specific object) — object::instanceMethod
Java
String prefix = "Hello, ";
// Lambda
Function<String, String> greet = name -> prefix.concat(name);
// Method reference (specific object: prefix)
Function<String, String> greet2 = prefix::concat;
System.out.println(greet2.apply("Alice")); // "Hello, Alice"
// Print each element
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(System.out::println); // System.out is the specific object
3. Instance Method Reference (arbitrary type) — ClassName::instanceMethod
Java
// Lambda
Comparator<String> comp = (s1, s2) -> s1.compareToIgnoreCase(s2);
// Method reference — first param is the instance
Comparator<String> comp2 = String::compareToIgnoreCase;
List<String> names = Arrays.asList("Charlie", "alice", "BOB");
names.sort(String::compareToIgnoreCase);
// [alice, BOB, Charlie] — case-insensitive sort
// Getting string length
List<String> words = Arrays.asList("hello", "world", "java");
List<Integer> lengths = words.stream()
.map(String::length) // calls .length() on each String
.collect(Collectors.toList());
// [5, 5, 4]
4. Constructor Reference — ClassName::new
Java
// Lambda
Supplier<ArrayList<String>> listFactory = () -> new ArrayList<>();
// Constructor reference
Supplier<ArrayList<String>> listFactory2 = ArrayList::new;
ArrayList<String> list = listFactory2.get();
// BiFunction constructor
BiFunction<String, Integer, StringBuilder> sbFactory =
(s, n) -> new StringBuilder(s);
// Convert list of strings to list of objects
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<Employee> employees = names.stream()
.map(Employee::new) // calls new Employee(String name)
.collect(Collectors.toList());
Quick Reference
| Lambda | Method Reference |
|---|---|
s -> Integer.parseInt(s) | Integer::parseInt |
s -> s.toUpperCase() | String::toUpperCase |
() -> new ArrayList<>() | ArrayList::new |
s -> System.out.println(s) | System.out::println |
(a, b) -> a.compareTo(b) | String::compareTo |
In Automation Testing
Java
List<WebElement> buttons = driver.findElements(By.tagName("button"));
// Get text of all buttons
List<String> buttonTexts = buttons.stream()
.map(WebElement::getText) // method reference
.collect(Collectors.toList());
// Filter only displayed buttons
List<WebElement> visibleButtons = buttons.stream()
.filter(WebElement::isDisplayed) // method reference
.collect(Collectors.toList());
// Click each button
visibleButtons.forEach(WebElement::click);
// Sort by text
buttonTexts.sort(String::compareTo);
