Slide 1

Slide 1 text

Java 8 What could possibly go wrong? Grzegorz Rozniecki (@xaerxess) September 20, 2016

Slide 2

Slide 2 text

About me @xaerxess Important: Images used in this presentation (and the presentation itself) are free to use under Creative Commons Attribution License. Code (reviews) addict Open Source advocate I’m a software engineer who enjoys learning and likes sharing his knowledge. Big GNU/Linux fan and Open Source Software advocate. I’ve developed software in various languages, including Perl, JavaScript, Java, Python, PHP and Lisp (and experimented with few others). ● Being a programmer is mostly reading code, not writing ● You are not your code! ● Freedom is important ● You can read how libraries, languages, even operating systems are built - and learn! ● Contribute, it’s easy!

Slide 3

Slide 3 text

Who? Why? What? Where? What now?

Slide 4

Slide 4 text

WHO uses Java 8 Quick journey from 1995 to 2016

Slide 5

Slide 5 text

Java 8 adoption Java versions as of March 2016 October 2014 27% 38% 64% May 2015 March 2016

Slide 6

Slide 6 text

WHY Java 8 (rhetorical)

Slide 7

Slide 7 text

lambdas streams Date API Optional CompletableFuture ... method references WHY it’s important? default methods

Slide 8

Slide 8 text

Programming styles ...? Why he’s talking about…

Slide 9

Slide 9 text

Programming styles Imperative Declarative Functional The focus is on what steps the computer should take rather than what the computer will do (ex. C, C++, Java). The focus is on what the computer should do rather than how it should do it (ex. SQL). A subset of declarative languages that has heavy focus on recursion.

Slide 10

Slide 10 text

Optional To be, or not to be… ● OptionalResult ● Optional#getOrElseThrowNoSuchElementException() ● Implements Serializable? ● Optional#stream() Sigh, why does everything related to Optional have to take 300 messages? ” Brian Goetz (2013-10-23 on lambda-libs-spec-experts) “ Junior Developer taking advice before starting work on a legacy project (aka avoiding null)

Slide 11

Slide 11 text

Date API java.time The main API for dates, times, instants, and durations. (...) They are based on the ISO calendar system, which is the de facto world calendar following the proleptic Gregorian rules. All the classes are immutable and thread-safe. “

Slide 12

Slide 12 text

Streams java.util.stream Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations. ● Sequential ● Parallel ● Unordered? “

Slide 13

Slide 13 text

Parallel streams Tasks in ForkJoinPool into smaller pieces split new subtasks fork computed tasks join 1 2 3 4 results from pieces compose

Slide 14

Slide 14 text

Parallel streams List primes = IntStream .range(1, 1_000_000) .parallel() .filter(PrimesPrint::isPrime) .collect(toList()); // how many threads is this using? // how do I configure that? Defaluts // see ForkJoinPool Javadoc -Djava.util.concurrent.ForkJoinPool.common.parallelism=4

Slide 15

Slide 15 text

Parallel streams ForkJoinPool forkJoinPool = new ForkJoinPool(2); forkJoinPool.submit(() -> IntStream.range(1, 1_000_000).parallel() .filter(PrimesPrint::isPrime) .collect(toList()) ).get(); Custom ForkJoinPool

Slide 16

Slide 16 text

WHAT should I use? Effective Java, Item 47: Know and use your libraries

Slide 17

Slide 17 text

WHERE should I find answers? Contribute!

Slide 18

Slide 18 text

That’s all (questions?) Thank you for your participation!