KEMBAR78
Java 8 - An Introduction by Jason Swartz | PDF
Java 8
An Introduction by
Jason Swartz
Agenda
1. Lambda Expressions
2. Streaming Collections
3. Monadic Collections
1. Lambda Expressions
Math Function
f(x) = x * x
Java Function
int sqr(int x) { return x * x; }
Anonymous Function
x -> x * x
Java Anonymous Function
x -> x * x
Java Anonymous Function
(x) -> x * x
Java Anonymous Function
(x, y) -> x + y
Java Anonymous Function
() -> 42
Java Anonymous Function
f = x -> x * x;
int y = f.apply(5);
Java Anonymous Function
f = (x, y) -> x + y;
int y = f.applyAsInt(5, 7);
Java Anonymous Function
r = () -> {
System.out.println("Hello");
};
r.run();
Java Method Reference
c = System.out::println;
c.accept("Hello");
Now You Know Lambdas
2. Streaming Collections
Doubling Items In A List
List<Integer> nums = Arrays
.asList(1, 2, 3);
int sqr(int x) { return x * x; }
Doubling Items In A List
nums2 = new ArrayList<Integer>();
for (int i : nums) {
nums2.add(sqr(i));
}
Doubling Items In A List
nums.stream()
Doubling Items In A List
nums.stream()
.map(this::sqr)
Doubling Items In A List
nums = nums.stream()
.map(this::sqr)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.stream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.parallelStream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.clusterStream()
.map(x -> x * x)
.collect(Collectors.toList());
Doubling Items In A List
nums = nums.dataCenterStream()
.map(x -> x * x)
.collect(Collectors.toList());
Welcome to
Declarative
Programming
Mapping To A List of Lists
nums.stream()
.map(x -> Arrays.list(x, x * x))
.collect(Collectors.toList());
Flatmap That List
nums = nums.stream()
.flatMap(x -> Arrays.list(x, x * x))
.collect(Collectors.toList());
Filtering Items In A List
nums = nums.stream()
.filter(x -> x % 2 == 0)
.collect(Collectors.toList());
Reducing A List
sum = nums.stream()
.reduce((a,i) -> a + i)
.get();
In Summary
sum = nums.stream()
.map(x -> x * x)
.flatMap(x -> Arrays.list(x, x * x))
.filter(x -> x % 2 == 0)
.reduce((a,i) -> a + i)
.get();
map()
flatmap()
reduce()
filter()
Now You Know Streams
3. Monadic Collections
Have you done this?
User u = getUser(“Fred”);
String name = u.getName();
Exception in thread "main" java.lang.
NullPointerException
Null values stink
Use Optional<T>, not null
Optional<User> u = getUser("Fred");
if (! u.isPresent()) return;
String name = u.get().name;
Use Optional<T>, not null
Optional<User> u = getUser("Fred");
Optional<String> name = u
.map(user -> user.name);
Another Example
Checking a user name
User u = getUser(“Fred”);
Boolean result = null;
if (u != null)
Result = checkName(u.getName());
else
Result = false;
Checking a user name
Optional<User> u = getUser("Fred");
Boolean result;
if (u.isPresent())
result = confirmName(u.get().name);
else
result = false;
Confirming a user name with map()
Boolean result = getUser("Fred")
.map(u -> u.name)
.map(this::confirmName)
.orElse(false);
Optional is just a monad, a
monoid in the category of
endofunctors.
What’s the problem?
Optional is just a monad, a
monoid in the category of
endofunctors.
What’s the problem?
Optional is a zero- or one-
sized collection that helps
you cleanly handle
missing values.
More Monadic
Collections?
CompletableFuture is a
zero- or one-sized
collection that helps you
cleanly handle future
values.
Javaslang’s Try is a zero-
or one-sized collection that
helps you cleanly handle
exception-thrown values.
Javaslang’s Lazy is a zero-
or one-sized collection that
helps you cleanly postpone
calculating values.
Now You Know
Monadic Collections
Thanks For Watching!

Java 8 - An Introduction by Jason Swartz