Do you want to learn functional programming in Java using lambda expressions introduced in Java 8? Do you want to explore the foundational concepts to explore powerful stream API? This presentation provides an overview of lambda functions introduced in Java 8 through examples. Topics covered: What is functional programming, creating lambda expressions, functional interfaces, built-in functional interfaces, and method references.
Make sure you have JDK 8 installed for trying out the programs as you go through the self-contained programming examples.
6. One-liner #2
Pattern.compile(" ").splitAsStream("java 8 streams").forEach(System.out::println);
This code splits the input string “java 8
streams” based on whitespace and hence
prints the strings “java”, “8”, and
“streams” on the console
10. Productive programming with lambdas
public static void main(String []file) throws Exception {
// process each file passed as argument
// try opening the file with FileReader
try (FileReader inputFile = new FileReader(file[0])) {
int ch = 0;
while( (ch = inputFile.read()) != -1) {
// ch is of type int - convert it back to char
System.out.print( (char)ch );
}
}
// try-with-resources will automatically release FileReader object
}
public static void main(String []file) throws Exception {
// read the file line-by-line and print it on the console
Files.lines(Paths.get(file[0])).forEach(System.out::println);
}
20. Lambdas
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");
Consumer<String> printString = string -> System.out.println(string);
strings.forEach(printString);
Capture in a
variable
Execute later
21. What are lambda functions?
❖ (Java 8) One way to think about lambdas is
“anonymous function” or “unnamed function” - they
are functions without a name and are not associated
with any class
❖ Functions don’t change external state
22. What is functional programming?
❖ Functions are entities that accepts input(s) and produces
an output
Underlying approach:
“Evaluate an expression.
Then use the results for
something else.”
23. Composing functions
❖ Functions are connected together by their outputs to
other functions’ inputs
sediment
pre-
carbon
ultra-filter
post-
carbon
Filtered
water
24. Let’s get our hands dirty!
❖ Make sure you have JDK 8
installed
❖ Try out the programs in your
machine (using IDE of your
choice)
26. Java 8 lambdas - “Hello world!”
interface LambdaFunction {
void call();
}
class FirstLambda {
public static void main(String []args) {
LambdaFunction lambdaFunction = () -> System.out.println("Hello world");
lambdaFunction.call();
}
}
Functional interface - provides
signature for lambda functions
Lambda function/expression
Call to the lambda
Prints “Hello world” on the console when executed
27. Parts of a lambda expression
() -> System.out.println("Hello world");
No parameters, i.e., ()
Arrow operator that separates
parameters and the body
The lambda body
Return type “void” inferred from the body
37. Built-in interfaces
Predicate<T> Checks a condition and returns a
boolean value as result
In filter() method in
java.util.stream.Stream
which is used to remove elements
in the stream that don’t match the
given condition (i.e., predicate) asConsumer<T> Operation that takes an argument but
returns nothing
In forEach() method in
collections and in
java.util.stream.Stream; this
method is used for traversing all
the elements in the collection orFunction<T,
R>
Functions that take an argument and
return a result
In map() method in
java.util.stream.Stream to
transform or operate on the passed
value and return a result.
Supplier<T> Operation that returns a value to the
caller (the returned value could be
same or different values)
In generate() method in
java.util.stream.Stream to
create a infinite stream of
elements.
41. Method references
Cannot use method references when lambda functions do
more than“routing” function parameters
strings.forEach(string -> System.out.println(string.toUpperCase()));
More processing here than just
“routing” parameters