Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

20
Stream or Loop Java 8 Stream API Nikolay Petkov

description

Presentation from the visit of Proxiad Bulgaria - partner for the Java Course in Hack Bulgaria. The topic is the new Stream API in Java 8 and the presenter - Nikolay Petkov from Proxiad Bulgaria

Transcript of Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Page 1: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Stream or LoopJava 8 Stream API

Nikolay Petkov

Page 2: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Java 8 Goodies

● Lambda expressions aka functional interfaces

● Method references● Default methods

Page 3: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Functional Interface

Page 4: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Lambda Expressions

Page 5: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Lambda Expressions

Page 6: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Method Reference

● Static method● Instance method of particular instance● Instance method of particular type● Constructor

Page 7: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Static method reference

Page 8: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Instance method reference

Page 9: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Default Method

public interface Developer {void writeCode();

default void writeTest() {throw new ImPerfectException(

“Why bother? My code is perfect”);}

}

Page 10: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

External vs Internal Iteration

for (Car car : cars) {if (“BMW”.equals(car.getBrand()) {

car.setOwner(“Alex”) ;}

}

cars.forEach((car) -> {if (“BMW”.equals(car.getBrand()) {

car.setOwner(“Alex”) ;}

});

Page 11: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Stream API

Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections.

int oldestBmwAge = cars.stream().filter((car) -> “BMW”.equals(car.getBrand())).mapToInt((car) -> car.getAge()).max();

Page 12: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Streams vs Collections

● Not a storage● Functional by design● Possibly unbounded● Lazy by nature● Use and discard

Page 13: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Stream pipeline

A stream pipeline consists of:● a source (such as a Collection, an array, a

generator function, or an I/O channel);● followed by zero or more intermediate

operations such as Stream.filter or Stream.map;

● and a terminal operation such as Stream.forEach or Stream.reduce.

Execution begins when the terminal operation is invoked, and ends when the terminal operation completes

Page 14: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Source

● Collection.stream()● Arrays.stream(Object[])● Stream.of(Object[])● IntStream.range(int, int)● BufferedReader.lines();● Files.list(Path dir)● etc.

Page 15: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Intermediate - filter

Returns a stream consisting of the elements of this stream that match the given predicate.

Stream<T> filter(Predicate<? super T> predicate)

filter((String s) -> s.lenght() < 20)filter((String s) -> String::isEmpty))

Page 16: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Intermediate - sorted

Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

sorted(Comparator<? super T> comparator)

sorted(comparing(Person::getLastName)sorted((s1, s2) -> s1.lenght() > s2.lenght())

Page 17: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Intermediate - map

Returns a stream consisting of the results of applying the given function to the elements of this stream. The map operation is kind of transformation of the stream elements to another elements.

map(Function<T,R> mapper)

mapToInt((String s) -> s.lenght())map((User u) -> User::getRole())

Page 18: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

Terminal – max/min/sum/count

Returns the maximum/minimum element of this stream according to the provided Comparator

max(Comparator<? super T> comparator)

max(comapring(Persone::getAge))stream.mapToInt(Persone::getAge).sum()

Page 19: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria

A lot more follows

Questions?

Page 20: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria