Real Life FP 
2 
...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 
• Base 
– Objects.equals/hashCode, Preconditions 
Primitives, CharMatcher, Splitter, Joiner, ... 
• Collections 
– groovy-like Collection Handling in Java 
• Caches, I/O, EventBus, Hashing, ... 
JUG Bielefeld, 19.11.14 8
Google Guava 
• Functions und Predicates 
– Function<A, B> 
• B apply(A input) 
• Transformation! 
– Predicate<T> 
• boolean apply(T input) 
• Filter! 
JUG Bielefeld, 19.11.14 9
Google Guava 
! 
JUG Bielefeld, 19.11.14 10
Google Guava 
Lets see some code! 
JUG Bielefeld, 19.11.14 11
Java 8 
JUG Bielefeld, 19.11.14 12
Java 8 
• Java wird funktional (unter anderem...) 
• FunctionalInterface 
• Und darauf aufbauend... 
• Streams API 
JUG Bielefeld, 19.11.14 13
Java 8 
• Erinnert ihr euch an SAM? 
– Anstatt: 
new Predicate<T>() { 
@Override 
public boolean apply(T input) { 
return input.isFoo(); 
} 
} 
– Einfach: 
( input -> input.isFoo() ) 
JUG Bielefeld, 19.11.14 14
Java 8 
• 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 
JUG Bielefeld, 19.11.14 15
Java 8 
• Beispiel Predicate: 
@FunctionalInterface 
public interface Predicate<T> { 
boolean test(T t); 
} 
• Verwendung: 
( obj -> obj.isFoo() ) 
JUG Bielefeld, 19.11.14 16
Java 8 
• Streams API 
– „A sequence of elements supporting sequential 
and parallel aggregate operations“ 
– Stream pipelines bestehen aus 
• Einer Datenquelle und 
• 0 oder mehr intermediate 
• und genau einer terminal Operation 
JUG Bielefeld, 19.11.14 17
Java 8 
Lets see some code! 
JUG Bielefeld, 19.11.14 18
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.FunctionalPro 
g/70.Java8.FunctionalProg.html 
• Code 
– https://github.com/smartsquare/RealLifeFP 
JUG Bielefeld, 19.11.14 19

Real lifefp

  • 1.
    Real Life FP 2 ...zurück in der Java Welt JUG Bielefeld, 19.11.14 1
  • 2.
    Wie finden wirein Element in einer Collection? JUG Bielefeld, 19.11.14 2
  • 3.
    Java, the goodol‘ 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
  • 4.
    Java, the goodol‘ days • ActionListener • Comparator • Runnable • Callable Gemeinsamkeiten? JUG Bielefeld, 19.11.14 4
  • 5.
    Java, the goodol‘ days • Single Abstract Method Interfaces (SAM Interfaces) • Genau eine Methodendeklaration • Wird häufig als anonyme innere Klasse verwendet JUG Bielefeld, 19.11.14 5
  • 6.
    Java, the goodol‘ 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
  • 7.
    Google Guava JUGBielefeld, 19.11.14 7
  • 8.
    Google Guava •Base – Objects.equals/hashCode, Preconditions Primitives, CharMatcher, Splitter, Joiner, ... • Collections – groovy-like Collection Handling in Java • Caches, I/O, EventBus, Hashing, ... JUG Bielefeld, 19.11.14 8
  • 9.
    Google Guava •Functions und Predicates – Function<A, B> • B apply(A input) • Transformation! – Predicate<T> • boolean apply(T input) • Filter! JUG Bielefeld, 19.11.14 9
  • 10.
    Google Guava ! JUG Bielefeld, 19.11.14 10
  • 11.
    Google Guava Letssee some code! JUG Bielefeld, 19.11.14 11
  • 12.
    Java 8 JUGBielefeld, 19.11.14 12
  • 13.
    Java 8 •Java wird funktional (unter anderem...) • FunctionalInterface • Und darauf aufbauend... • Streams API JUG Bielefeld, 19.11.14 13
  • 14.
    Java 8 •Erinnert ihr euch an SAM? – Anstatt: new Predicate<T>() { @Override public boolean apply(T input) { return input.isFoo(); } } – Einfach: ( input -> input.isFoo() ) JUG Bielefeld, 19.11.14 14
  • 15.
    Java 8 •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 JUG Bielefeld, 19.11.14 15
  • 16.
    Java 8 •Beispiel Predicate: @FunctionalInterface public interface Predicate<T> { boolean test(T t); } • Verwendung: ( obj -> obj.isFoo() ) JUG Bielefeld, 19.11.14 16
  • 17.
    Java 8 •Streams API – „A sequence of elements supporting sequential and parallel aggregate operations“ – Stream pipelines bestehen aus • Einer Datenquelle und • 0 oder mehr intermediate • und genau einer terminal Operation JUG Bielefeld, 19.11.14 17
  • 18.
    Java 8 Letssee some code! JUG Bielefeld, 19.11.14 18
  • 19.
    Quellen • GoogleGuava: – 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.FunctionalPro g/70.Java8.FunctionalProg.html • Code – https://github.com/smartsquare/RealLifeFP JUG Bielefeld, 19.11.14 19