JavaZone 2014 - Iteration 2.0: Stream

Preview:

DESCRIPTION

Slide pack for my JavaZone talk Iteration 2.0: Stream. Official page: http://2014.javazone.no/presentation.html?id=640bf7b0

Citation preview

Martin SkarsauneJava Developer and Co-Owner

Iteration 2.0

Stream

Iteration Problem

Root

x

y z

u

v

public class Node { private Object data; private Map<String, Node> children;}

Recursion

• Need to traverse model

• recursiveY()• recursiveZ()• ...

void recursiveX() { x(); for (final Node child : children.values()) child.recursiveX(); }

External Iteration : Iterator

• Any way to implement recursion once and for all ?

• Iterator pattern (Java >= 1.2 )• Wikipedia:

” the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements”

Client In Control

Iterator Behaviour

Root

x

y z

u

v

ClientIterator()

Iterator

next()

Iterator Issues

• The iterator instance has state– Strong references to provider and internal

state

• implementation often difficult• Thread unsafe by design

Internal Iteration - Visitor

• The visitor pattern• Wikipedia:

” the visitor design pattern is a way of separating an algorithm from an object structure on which it operates”

Provider in Control

Visitor Behaviour

Root

x

y z

u

v

Client

Visitor

visitor()visit(Node)

visit(Node)

visit(Node)

accept(Visitor)

Visitor Implementation

interface Visitor { void visit(Node node); }

void recursiveX() { x(); for (final Node child : children.values()) child.recursiveX(); }

Visitor Implementation

interface Visitor { void visit(Node node); }

void accept(Visitor visitor) { visitor.visit(this); for (final Node child : children.values()) child.accept(visitor); }

Visitor Usage

node.accept(n -> n.x());

node.accept(n -> n.y());

node.accept(n -> n.z());

Visitor – Limitations (Lambda)• Limited access to local scope– Final and effectively final variables

• Early exit not possible– break– return

Internal iteration in Java 8 - Stream• java.util.Stream• Not limited to collections• Many powerful methods• Potential gains

– Lazy evaluation– Parallell execution

• The default implementation (StreamSupport) is based on (spl)iterators

Stream In Control

(Spl)Iterator

StreamSupport

Default Stream Implementation

Root

x

y z

u

v

Clientstream()

next()

callback()

COMPLEXITY

LIMITATIONS

Visitor Stream Implementationinterface Visitor { void visit(Node node); }

Visitor Stream Implementation - GEneralizaion

interface Visitor<T> { void visit(T node); }

interface Visitable<T> { void accept(Visitor<T> visitor); }

Visitor Stream Implementation - Concreteclass Node { void accept(Visitor visitor){ ... visitor.accept(this); ... } }

Visitor Stream Implementation - Concrete

class Node { void accept(Visitor<Node> visitor){ ... visitor.accept(this); ... } }

Visitor Stream Implementation - Concreteclass Node implements Visitable<Node> { void accept(Visitor<Node> visitor){ ... visitor.accept(this); ... } }

Visitor Stream Implementation - Concreteclass VisitorStream<T> implements Stream<T> {

private Visitable<T> source;

public VisitorStream(Visitable<T> source) { this.source = source; } ...

}

Stream Method Categories

Reduce

• max• min• reduce• count

Iterator

• iterator• spliterator

Concurrency

• sequential• paralell

Output

• toArray

Pipeline

• filter• map• flatMap• skip(long)

Stateful

• sorted• distinct• ordered

Short circuit

• *Match(Predicate)

• limit(int)• find*

Iteration

• forEach✔

✔ ✔

✔ ✔✔

public void forEach(Consumer<? super T> action) { this.source.accept(item -> action.accept(item)); }

interface SplitVisitable<T> { Collection<Visitable<T>> split();}

interface Visitor<T> { void visit(T node);

}

default boolean isDone(){ return false; }

Certified 100%

Iterator Free

Provider VisitorStream Filter Map

implements SortedVisitable ... implements DistinctVisitable ...implements OrderedVisitable ...

Summary

• Many advantages to internal iteration– Control– Simplified implementation– Nice abstraction

• Streams are powerful– Create your own – it is not that hard

Thank You for Your Time!

Martin SkarsauneJava Developer and Co-Owner

Recommended