Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Twitter: @speakjava
How
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Lambda Expression Types
• Single-method interfaces are used extensively in Java
– Definition: a functional interface is an interface with one abstract method
– Functional interfaces are identified structurally
– The type of a lambda expression will be a functional interface
• Lambda expressions provide implementations of the abstract method
class DataProcessor {
private int currentValue;
class DataProcessor {
private int currentValue;
FileFilter x = File::canRead;
Replace with
Factory<List<String>> f = ArrayList<String>::new;
myList.forEach(System.out::println);
myList.replaceAll(String::toUpper);
logger.finest(produceComplexMessage());
logger.finest(() -> produceComplexMessage());
The equals(Object)
method is implicit
from the Object class
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 23
Stream Overview
Pipeline
• A stream pipeline consists of three types of things
– A source
– Zero or more intermediate operations
– A terminal operation
• Producing a result or a side-effect
Source
int total = transactions.stream()
.filter(t -> t.getBuyer().getCity().equals(“London”))
.mapToInt(Transaction::getPrice)
.sum();
Intermediate operation
Terminal operation
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Stream Sources
Many Ways To Create
• From collections and arrays
– Collection.stream()
– Collection.parallelStream()
– Arrays.stream(T array) or Stream.of()
• Static factories
– IntStream.range()
– Files.walk()
• Roll your own
– java.util.Spliterator
1-to-1 mapping
Input Stream Output Stream
Map
1-to-many mapping
Input Stream Output Stream
FlatMap
if (gpsData != null) {
Position p = gpsData.getPosition();
if (p != null) {
Latitude latitude = p.getLatitude();
if (latitude != null)
direction = latitude.getDirection();
}
}
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Optional Class
Helping To Eliminate the NullPointerException
• Terminal operations like min(), max(), etc do not return a direct result
• Suppose the input Stream is empty?
• Optional<T>
– Container for an object reference (null, or real object)
– Think of it like a Stream of 0 or 1 elements
– use get(), ifPresent() and orElse() to access the stored reference
– Can use in more complex ways: filter(), map(), etc
– gpsMaybe.filter(r -> r.lastReading() < 2).ifPresent(GPSData::display);
if (x != null) {
print(x);
}
if (x != null) {
String t = x.trim();
if (t.length() > 1)
print(t);
}
opt.map(String::trim)
.filter(t -> t.length() > 1)
.ifPresent(this::print);
Optional<String> tryFindSimilar(String s)
class Position {
public Optional<Latitude> getLatitude() { ... }
}
class Latitude {
public String getString() { ... }
}
sourceKeySet.stream()
.forEach(c ->
sourceCount.add(streamReturningMethodMap.get(c).size()));
sourceKeySet.stream()
.mapToInt(c -> streamReturningMethodMap.get(c).size())
.sum();
functionalParameterMethodMap.get(c).stream()
.forEach(m -> {
output.println(m);
if (isNewMethod(c, m))
newMethodCount.increment();
});
return newMethodCount.intValue();
if (isNewMethod(c, m))
newMethod = 1;
reader.lines()
.mapToInt(String::length)
.max()
.getAsInt();
if (index == l.size() - 1) {
if (s.length() > l.get(index).length())
return s;
return l.get(index);
}
reader.lines()
.max(comparingInt(String::length))
.get();
Twitter: @speakjava