[JAVA] Item 43: Prefer method references to lambdas

43. Use method references rather than lambda expressions

//Method reference
service.execute(GoshThisClassNameIsHumongous::action);
//Lambda expression
service.execute(() -> action());
Method Ref Type Example Lambda Equivalent
Static Integer::parseInt str -> Integer.parseInt(str)
Bound Instant.now()::isAfter Instant then = Instant.now(); t -> then.isAfter(t)
Unbound String::toLowerCase str -> str.toLowerCase()
Class Constructor TreeMap<K,V>::new () -> new TreeMap<K,V>
Array Constructor int[]::new len -> new int[len]

Recommended Posts

Item 43: Prefer method references to lambdas
Item 42: Prefer lambdas to anonymous classes
Item 28: Prefer lists to arrays
Item 65: Prefer interfaces to reflection
Item 39: Prefer annotations to naming patterns
Item 85: Prefer alternatives to Java serialization
Item 58: Prefer for-each loops to traditional for loops
Item 23: Prefer class hierarchies to tagged classes
Item 61: Prefer primitive types to boxed primitives
Method to search
Item 81: Prefer concurrency utilities to wait and notify
Item 80: Prefer executors, tasks, and streams to threads
Item 89: For instance control, prefer enum types to readResolve
Item 47: Prefer Collection to Stream as a return type
16 Corresponds to method invocation
Item 51: Design method signatures carefully
20 Corresponds to static method invocation
Easy to use array.map (&: method)
How to create a method