streams or loops? java 8 stream api by niki petkov - proxiad bulgaria
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 BulgariaTRANSCRIPT
![Page 1: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/1.jpg)
Stream or LoopJava 8 Stream API
Nikolay Petkov
![Page 2: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/3.jpg)
Functional Interface
![Page 4: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/4.jpg)
Lambda Expressions
![Page 5: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/5.jpg)
Lambda Expressions
![Page 6: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/7.jpg)
Static method reference
![Page 8: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/8.jpg)
Instance method reference
![Page 9: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/19.jpg)
A lot more follows
Questions?
![Page 20: Streams or Loops? Java 8 Stream API by Niki Petkov - Proxiad Bulgaria](https://reader034.vdocuments.mx/reader034/viewer/2022042614/559b1c601a28ab2e128b477a/html5/thumbnails/20.jpg)