Real lifefp

19
Real Life FP 2 ...zurück in der Java Welt JUG Bielefeld, 19.11.14 1

description

Functional Java

Transcript of Real lifefp

Page 1: Real lifefp

Real Life FP2

...zurück in der Java Welt

JUG Bielefeld, 19.11.14 1

Page 2: Real lifefp

Wie finden wir ein Element in einer Collection?

JUG Bielefeld, 19.11.14 2

Page 3: Real lifefp

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

Page 4: Real lifefp

Java, the good ol‘ days

• ActionListener

• Comparator

• Runnable

• Callable

Gemeinsamkeiten?

JUG Bielefeld, 19.11.14 4

Page 5: Real lifefp

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

Page 6: Real lifefp

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

Page 7: Real lifefp

Google Guava

JUG Bielefeld, 19.11.14 7

Page 8: Real lifefp

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, ...

Page 9: Real lifefp

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!

Page 10: Real lifefp

Google Guava

JUG Bielefeld, 19.11.14 10

!

Page 11: Real lifefp

Google Guava

JUG Bielefeld, 19.11.14 11

Lets see some code!

Page 12: Real lifefp

Java 8

JUG Bielefeld, 19.11.14 12

Page 13: Real lifefp

Java 8

JUG Bielefeld, 19.11.14 13

• Java wird funktional (unter anderem...)

• FunctionalInterface

• Und darauf aufbauend...

• Streams API

Page 14: Real lifefp

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() )

Page 15: Real lifefp

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

Page 16: Real lifefp

Java 8

JUG Bielefeld, 19.11.14 16

• Beispiel Predicate:

@FunctionalInterface

public interface Predicate<T> {

boolean test(T t);

}

• Verwendung:

( obj -> obj.isFoo() )

Page 17: Real lifefp

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

Page 18: Real lifefp

Java 8

JUG Bielefeld, 19.11.14 18

Lets see some code!

Page 19: Real lifefp

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