-
Notifications
You must be signed in to change notification settings - Fork 48
Method Reference
Hu JiaJun edited this page Nov 29, 2020
·
1 revision
Simplify existing methods in lambda expressions through method reference. The method reference is a Lambda expression, where the operator of the method reference is the double colon "::".
Print each element in the ArrayList
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
/* Before Java 8 */
for (Integer e : list) {
System.out.println(e);
}
/* From Java 8 */
list.forEach(e -> System.out.println(e));
// The content of the lambda expression just passes the parameter e to the println() method,
// so it can be further simplified
list.forEach(System.out::println);
-
System.out::println
is different fromSystem.out.println()
-
System.out::println
can be regarded as a short form of lambda expressione -> System.out.println(e)
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum