Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Lambda Expression Stream Pipeline Operations
This section provides a tutorial example of using lambda expressions in stream pipeline operations. LambdaStream.java uses calendar dates as a stream and filters out Friday the 13th dates with lambda expressions.
The best place to use lambda expressions is probably in stream pipeline operations. See the example program, LambdaOnStream.java, below:
/* LambdaStream.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*;; import java.text.*;; import java.util.stream.Stream; class LambdaStream { public static void main(String[] arg) { Calendar today = new GregorianCalendar(); Stream .iterate(today, d->{ Calendar n = (Calendar)d.clone(); n.add(Calendar.DATE,1); return n; }) .filter(d->d.get(Calendar.DATE)==13 && d.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY ) .limit(10) .map(d->d.getTime()) .forEach(d->System.out.println( new SimpleDateFormat("dd-MMM-yyyy").format(d) )); } }
LambdaOnStream.java uses several pipeline operations from the "Stream" interface introduced in JDK 1.8 to manipulate a stream of calendar objects:
Overall, the program is trying to find the next 10 Friday the 13th dates. Here they are generated by the above program with JDK 1.8:
13-Jun-2014 13-Feb-2015 13-Mar-2015 13-Nov-2015 13-May-2016 13-Jan-2017 13-Oct-2017 13-Apr-2018 13-Jul-2018 13-Sep-2019
Year 2015 is pretty bad. It has 3 Friday the 13th dates!
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
Generic Methods and Type Inference
►Lambda Expressions and Method References
LambdaCalculator.java - Lambda Expression Example
Lambda Expression Syntax Options
Lambda Expression as Method Reference
Method Reference Example - LambdaMethodRefernce.java
►Lambda Expression Stream Pipeline Operations
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs