Real lifefp
-
Upload
daniel-rosowski -
Category
Software
-
view
290 -
download
0
description
Transcript of Real lifefp
Real Life FP2
...zurück in der Java Welt
JUG Bielefeld, 19.11.14 1
Wie finden wir ein Element in einer Collection?
JUG Bielefeld, 19.11.14 2
Java, the good ol‘ days
• Collections, wie war das noch?
• < Java5
– Iterator
– while(iterator.hasNext())
• > Java5
– for-each Loop
– for(Typ typ : typCollection)
JUG Bielefeld, 19.11.14 3
Java, the good ol‘ days
• ActionListener
• Comparator
• Runnable
• Callable
Gemeinsamkeiten?
JUG Bielefeld, 19.11.14 4
Java, the good ol‘ days
• Single Abstract Method Interfaces (SAM Interfaces)
• Genau eine Methodendeklaration
• Wird häufig als anonyme innere Klasse verwendet
JUG Bielefeld, 19.11.14 5
Java, the good ol‘ days
• Funktionale „Vorfahren“ in Java:
findElement(Collection, Predicate)
findElement(people, new Predicate() {
public boolean test(Person person) {
return person.getName().startsWith(...);
}
}
JUG Bielefeld, 19.11.14 6
Google Guava
JUG Bielefeld, 19.11.14 7
Google Guava
JUG Bielefeld, 19.11.14 8
• Base
– Objects.equals/hashCode, PreconditionsPrimitives, CharMatcher, Splitter, Joiner, ...
• Collections
– groovy-like Collection Handling in Java
• Caches, I/O, EventBus, Hashing, ...
Google Guava
JUG Bielefeld, 19.11.14 9
• Functions und Predicates
– Function<A, B>
• B apply(A input)
• Transformation!
– Predicate<T>
• boolean apply(T input)
• Filter!
Google Guava
JUG Bielefeld, 19.11.14 10
!
Google Guava
JUG Bielefeld, 19.11.14 11
Lets see some code!
Java 8
JUG Bielefeld, 19.11.14 12
Java 8
JUG Bielefeld, 19.11.14 13
• Java wird funktional (unter anderem...)
• FunctionalInterface
• Und darauf aufbauend...
• Streams API
Java 8
JUG Bielefeld, 19.11.14 14
• Erinnert ihr euch an SAM?– Anstatt:
new Predicate<T>() {
@Override
public boolean apply(T input) {
return input.isFoo();
}
}
– Einfach:( input -> input.isFoo() )
Java 8
JUG Bielefeld, 19.11.14 15
• Wie geht das?
FunctionalInterface
– Ein funktionales Interface darf genau EINE Methode deklarieren
– (...mit Ausnahme von default Methoden)
– Mittels der Annotation @FunctionalInterface wird ein normales Interface funktional
Java 8
JUG Bielefeld, 19.11.14 16
• Beispiel Predicate:
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
• Verwendung:
( obj -> obj.isFoo() )
Java 8
JUG Bielefeld, 19.11.14 17
• Streams API
– „A sequence of elements supporting sequentialand parallel aggregate operations“
– Stream pipelines bestehen aus
• Einer Datenquelle und
• 0 oder mehr intermediate
• und genau einer terminal Operation
Java 8
JUG Bielefeld, 19.11.14 18
Lets see some code!
Quellen
• Google Guava:– https://code.google.com/p/guava-libraries/wiki/GuavaExplained
– Und insbesondere:
– https://code.google.com/p/guava-libraries/wiki/FunctionalExplained
• Java 8– http://www.oracle.com/technetwork/articles/java/lambda-1984522.html
– http://www.angelikalanger.com/Articles/EffectiveJava/70.Java8.FunctionalProg/70.Java8.FunctionalProg.html
• Code– https://github.com/smartsquare/RealLifeFP
JUG Bielefeld, 19.11.14 19