Functional Java 8 Static
Functional Java 8 Static
Nick Maiorano
ThoughtFlow Solutions1 Inc.
About me
Developer
Software
Architect
Independent
Consultant
ThoughtFlow Solutions
Modularization
Repeating
New
annotations
Date/Time
Concurrency API
updates
Java 8
Lambdas
GC
updates
Nashorn
JavaScript
Engine
Church
Performance Collections
improvements
Functional Concurrency
libraries
Java 8
Lambdas
Church Collections
Java Classes
Vs.
FP Functions
Polymorphism, encapsulation,
Java inheritance, dynamic binding
Vs.
Vs.
Declarative: binding functions together
FP without necessarily specifying their
contents
Vs.
FP Avoid state
Vs.
FP Emphasis on immutability
Vs.
Organize processing into parallel workflows
FP each dedicated to a core and avoid state,
shared resources and locks
Vs.
FP Rudimentary
Vs.
Lambda blocks
(Parameter declaration) -> {Lambda body}
Lambda blocks
(Parameter declaration) -> {Lambda body}
Lambda expressions
Parameter name -> Lambda expression
Lambda expressions
Parameter name -> Lambda expression
i -> System.out.println(i);
Lambda expressions
Parameter name -> single statement;
@FunctionalInterface
public interface Calculator
{
int calculate(int x, int y);
}
Supplier<T>
Functional Function <T, R>
Supplier Function
T get() Interfaces R apply(T t);
Predicate
Predicate<T>
boolean test(T t);
@FunctionalInterface
public interface Calculator
{
int calculate(int x, int y);
Functional
Java
Streams Collections
List<String> stooges =
Arrays.asList("Larry", "Moe", "Curly");
List<String> stooges =
Arrays.asList("Larry", "Moe", "Curly");
stooges.forEach(System.out::println);
stooges.replaceAll(feminize);
Predicate<String> moeRemover =
s -> Moe".equals(s);
stooges.removeIf(moeRemover);
movieDb.putIfAbsent
(1930, new LinkedList<>());
movieDb.getOrDefault
(1930, new LinkedList<>());
Peek Filter
Stream
operations
Iterate Map
Reduce
return sum == n;
}
List<Long> perfectNumbers =
LongStream.rangeClosed(1, 8192).
filter(PerfectNumberFinder::isPerfect).
collect(ArrayList<Long>::new, ArrayList<Long>::add, ArrayList<Long>::addAll);
List<Long> perfectNumbers =
LongStream.rangeClosed(1, 8192).parallel().
filter(PerfectNumberFinder::isPerfect).
collect(ArrayList<Long>::new, ArrayList<Long>::add, ArrayList<Long>::addAll);