forked from bentolor/java8-lightningtalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
624 lines (482 loc) · 21.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Java 8: A quick overview over the good parts - Writing Java has never been so fun</title>
<meta name="description" content="Java 8: A quick overview over the good parts">
<meta name="author" content="Benjamin Schmid | @bentolor">
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/lt.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write('<link rel="stylesheet" href="css/print/' + ( window.location.search.match(/print-pdf/gi) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">');
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1 style="margin: 50px 0 .3em">Java 8<br/>The good parts</h1>
<h3 style="font-size:1.2em;margin-bottom:2em">Writing Java has never been so fun</h3>
<p style="text-align: right">
<a href="https://github.com/bentolor/java8-lightningtalk">Based on https://github.com/bentolor/java8-lightningtalk</a>
</p>
</section>
<section>
<h2>Table of contents</h2>
<p style="font-size: 76%; text-align: center; color: #666; margin: -1.5em 0 1em 0">Usage hint: Use mouse scoll or cursor right & <strong>down</strong> to navigate!</p>
<div id="table-of-contents"></div>
</section>
<section class="glow">
<section>
<h2 class="slide-header">Java 8 is <em>big</em></h2>
<h5>Language</h5>
<ul>
<li>Lambdas & Functional Interfaces</li>
<li>Method References</li>
<li>Default Methods</li>
<li>Better annotations</li>
<li>Better Type Inference</li>
</ul>
</section>
<section>
<h2>Java 8 is <em>big</em></h2>
<h5>Runtime library</h5>
<ul>
<li>Streams</li>
<li>Optional</li>
<li>Multicore Support (Parallel)</li>
<li>Native Javascript</li>
<li>Many Goodies</li>
</ul>
</section>
<section>
<h2>Java 8 is <em>big</em></h2>
<h5>Compiler & Tools</h5>
<ul>
<li>Reflection: Parameter names</li>
<li>CLI: jjs (Nashorn)</li>
<li>CLI: jdeps</li>
<li>Death of PermGen</li>
</ul>
</section>
</section>
<section>
<h2 class="slide-header" style="font-size:3em;margin-top: 50px">Language changes</h2>
</section>
<section>
<section>
<h2 class="slide-header">Lambdas</h2>
<p>Basic form</p>
<pre><code class="java">(params) -> expression
(params) -> statement
(params) -> { statements }</code></pre>
<p class="tgap">A simple example (no parameters):</p>
<pre><code class="java">Runnable r = () -> System.out.println("Hello!");
r.run();</code></pre>
<p class="tgap">Example with parameter (Type inferred!)<p>
<pre><code class="java">Arrays.asList( "a", "b", "d" ).forEach( e -> System.out.println( e ) );</code></pre>
</section>
<section>
<h2>Lambdas</h2>
<p>Lambdas can access outer field / variables if they are <em>(effectively)</em> final</p>
<pre><code class="java">String separator = ",";
Arrays.asList( "a", "b", "d" )
.forEach( (String e) -> System.out.print(e+separator) );
//separator = ";" // would break!</code></pre>
<p class="tgap">Lambdas may return values (inline/explicit)</p>
<pre><code class="java">Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) );
// or:
Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> {
return e1.compareTo( e2 );
} ); </code></pre>
</section>
<section>
<h2>Lambdas</h2>
<p>More succint expression for deferred evaluation of parameters</p>
<pre><code class="java">// Java 6
if (logger.isDebugEnabled()) {
logger.debug(generateExpensiveLogMessage());
}
// Java 8
logger.debug(() -> generateExpensiveLogMessage());</code></pre>
<p>Logging library updated with:</p>
<pre><code class="java">public void debug(Supplier<String> messageSupplier) {
if (isLoggerEnabled(LogLevel.DEBUG)) {
debug(messageSupplier.get());
}
}</code></pre>
</section>
</section>
<section>
<section>
<h2 class="slide-header">Functional interfaces</h2>
<pre><code class="java">Comparator <Apple> c =
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());</code></pre>
<h5 class="fragment tgap">Wait, wait, wait... Comparator? How does this work?</h5>
<pre class="fragment"><code class="java">@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
}</code></pre>
</section>
<section>
<h2><code>@FunctionalInterface</code></h2>
<p> Conceptually, a functional interface has exactly <strong>one abstract
method.</strong> As such, it may be implicitly converted to a lambda expression.</p>
<p>Note that <strong>instances of functional interfaces</strong> can be created with
<em>lambda expressions</em> or method/constructor references. </p>
<p><strong>But:</strong> To avoid breakage by accidently adding further methods , these
interfaces <strong><u>should</u></strong> be explicitly tagged with <code>@FunctionalInterface</code>.
</p>
</section>
</section>
<section>
<section>
<h2 class="slide-header">Method references</h2>
<p>Given the following interface, we can use a lambda</p>
<pre><code class="java mb-30">public interface MyFormatter<T> {
String format(T object);
}
public <T> String format(T o, MyFormatter<T> formatter) {
return formatter.format(o);
}
format( 32, i -> Integer.toHexString(i) );</code></pre>
<div class="fragment tgap">
<p>Or we can be more succinct and use a method reference</p>
<pre><code class="java mb-30">format( 32, Integer::toHexString );</code></pre>
</div>
</section>
<section>
<h2 >Supported method references</h2>
<ul>
<li><strong>static method:</strong> <code>Class::staticMethodName</code></li>
<li><strong>instance method of an <em>arbitrary</em> object</strong> <code>Type::methodName</code></li>
<li><strong>instance method of a <em>particular</em> object</strong> <code>object::methodName</code></li>
<li><strong>constructor:</strong> <code>ClassName::new</code></li>
</ul>
</section>
<section>
<h2 >Method reference examples</h2>
<pre><code class="java">// static method: (String) => void
Consumer<String> println = System.out::println;
println.accept("Hello");
// instace method of an arbitrary object: (String, String) => String
BinaryOperator<String> concat = String::concat;
System.out.println( concat.apply("today", " is great") );
// instace method of a particular object: (String) => String
UnaryOperator<String> concatInstance = "yes"::concat;
System.out.println( concatInstance.apply(", it is") );
// constructor with no params, () => Date, and int param, (Integer) => Date
Supplier<Date> date = Date::new;
Function<Integer, Date> dateMillis = Date::new;
System.out.println( "Today: " + date.get() + ", Epoch: " + dateMillis.apply(0) );</code></pre>
</section>
<section>
<h2 >Progression from lambdas to method references</h2>
<pre><code class="java">List<File> files = new ArrayList<>();
files.sort(new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return file1.getName().compareTo(file2.getName());
}
});</code></pre>
<pre class="fragment"><code class="java">files.sort( (file1, file2) -> file1.getName().compareTo(file2.getName()) );</code></pre>
<pre class="fragment"><code class="java">// Comparator::comparing creates Comparator based on Comparable sort key
import static java.util.Comparator.comparing;
files.sort( comparing(file -> file.getName()) );</code></pre>
<pre class="fragment"><code class="java">files.sort( comparing(File::getName) );</code></pre>
</code></pre>
</section>
</section>
<section>
<section>
<h2 class="slide-header">Interface <code>default</code> methods</h2>
<p>In Java 8 you can write:</p>
<pre><code class="java">List<Integer> numbers = Arrays.asList(3, 5, 1, 2, 6);
numbers.sort(Comparator.naturalOrder());</code></pre>
<p class="tgap fragment">Huh? <code>java..util.List</code> now requires a <code>sort()</code>-Method?<br/>Will all of my Java 6 list implementations break now?</p>
<pre class="fragment"><code class="java">public interface List<E> extends Collection<E> {
// ...
default void sort(Comparator<? super E> c) {
// ...
}
}</code></pre>
</section>
<section>
<h2 class="slide-header">Interface <code>static</code> methods</h2>
<pre><code class="java">private interface PersonFactory {
// Interfaces now allow static methods
static Person createPerson( String firstname, String lastname ) {
return new Person(firstname, lastname);
}
}</code></pre>
</section>
</section>
<section>
<h2 class="slide-header" style="font-size:3em;margin-top: 50px">Runtime library enhancement</h2>
</section>
<section>
<section>
<h2 class="slide-header">Streams</h2>
<pre><code class="java mb-30">Collection<Task> tasks = getTasks();
long totalPointsOfOpenTasks = tasks
.stream()
.filter( task -> task.getStatus() == Status.OPEN )
.mapToInt( Task::getPoints )
.sum();
System.out.println( "Total points: " + totalPointsOfOpenTasks );</code></pre>
</section>
<section>
<h2 class="slide-header">Stream operations</h2>
<h4>Are divided into</h4>
<ul class="mb-30">
<li><em>Intermediate operations</em> (returning another <code>Stream<T></code>)</li>
<li><em>Terminal operations</em> (returning the final result)</li>
</ul>
<h4>Parallel processing</h4>
<p>Out-of-the box support of parallel processing (Multi-threading!)</p>
<p><pre><code>stream.parallel()</code></pre></p>
</section>
<section>
<h2 class="slide-header">Streams are lazy and can be infinite</h2>
<p>Stream of all primes (not efficient!)</p>
<pre><code class="java" >Stream<Integer> primes = Stream.iterate(2, i -> i + 1)
.filter( i -> IntStream.range(2, i).noneMatch( prime -> i % prime == 0 ) );
System.out.println("First 100 primes: " + primes.limit(100).collect( toList() ));</code></pre>
</section>
<section>
<h2 class="slide-header">Streams for Grouping</h2>
<p>Partition into files/directories</p>
<pre><code class="java" >Map<Boolean, List<File>> filesByType = files.stream()
.collect( Collectors.groupingBy(File::isFile) );
</code></pre>
<p>Further partition by first letter of file</p>
<pre><code class="java" >Map<Boolean, Map<String, List<File>>> filesByTypeByLetter = files.stream()
.collect( groupingBy(File::isFile,
groupingBy( (File f) -> f.getName().substring(0, 1) ));
</code></pre>
</section>
<section>
<h2 class="slide-header">Complex stream example</h2>
<pre><code class="java mb-30">// Calculate the weight of each tasks (as percent of total points)
final Collection<String> result = tasks
.stream() // Stream<String>
.mapToInt( Task::getPoints ) // IntStream
.asLongStream() // LongStream
.mapToDouble( points -> points / totalPoints ) // DoubleStream
.boxed() // Stream<Double>
.mapToLong( weight -> ( long )( weight * 100 ) ) // LongStream
.mapToObj( percentage -> percentage + "%" ) // Stream<String>
.collect( Collectors.toList() ); // List<String>
System.out.println( result );</code></pre>
</section>
</section>
<section>
<h2 class="slide-header">Optional</h2>
<p>Allows you to commit to a non-<code>null</code> convention in your project
<br/> & offers some convenience. </p>
<pre><code class="java mb-30">Optional<String> fullName = Optional.ofNullable( null );</code></pre>
<pre><code class="java mb-30">log.info( "Full Name is set? " + fullName.isPresent() );
log.info( "Full Name: " + fullName.orElseGet( () -> "[none]" ) );
log.info( fullName.map( s -> "Hey " + s + "!" ).orElse( "Hey Stranger!" ) );</code></pre>
<p>No more <code>NullPointerException</code>?</p>
</section>
<section>
<h2 class="slide-header">Native Javascript Support (Nashorn)</h2>
<h5>Use / Run Javascript in your code:</h5>
<pre><code class="java mb-30">ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName( "JavaScript" );
System.out.println( engine.eval( "function f() { return 1; }; f() + 1;" ) );</code></pre>
<h5>From command line</h5>
<pre><code class="mb-30">jjs server.js</code></pre>
</section>
<section>
<section>
<h2 class="slide-header" style="font-size:3em;margin-top: 50px">Smaller goodies</h2>
</section>
<section>
<h2 class="slide-header"> Date/Time API (JSR 310)</h2>
<ul>
<li>Immutable, time-zone less data types (<code>LocalDate, LocalTime, LocalDateTime</code>) </li>
<li>More convenience & easier operations.</li>
</li>
</ul>
<pre><code class="java" >import static java.time.temporal.TemporalAdjusters.*;
LocalDate date1 = LocalDate.of(2014, 3, 18); // 2014-03-18
LocalDate date2 = date1.with(nextOrSame(DayOfWeek.SUNDAY)); // 2014-03-23
LocalDate date3 = date2.with(lastDayOfMonth()); // 2014-03-31</code></pre>
</section>
<section>
<h2 class="slide-header" style="font-size:3em;margin-top: 50px">Better type inference</h2>
<p>Diaomond operator, <>, introduced in Java 7</p>
<pre><code class="java" >Map<String,File> filesByName = new HashMap<>();</code></pre>
<p>In Java8, method arguments now used to infer generic types</p>
<pre><code class="java" >import static java.util.Collections.emptyList;
processList(emptyList());
static void processList(List<String> list) {
list.forEach(System.out::println);
}</code></pre>
</section>
<section>
<h2 class="slide-header">Better Annotations</h2>
<h5>Repeatable</h5>
<pre><code class="java mb-30">@Filter( "filter1" )
@Filter( "filter2" )
public interface Filterable { }</code></pre>
<h5>... assigned to variables, constructors, maps, casts, ...</h5>
<pre><code class="java mb-30">@NotNull String myString = "Foo"
new @Interned MyObject()
myString = (@NonNull String) myObject;</code></pre>
<h5>... used in Generics</h5>
<pre><code class="java mb-30">List<@Email String> emails = ...</code></pre>
<h5>... and more!</h5>
</section>
<section>
<h2 class="slide-header">Parameter names</h2>
<p>If compiled with <code>-parameters</code> option...<p>
<pre><code class="java mb-30">Method method = ParameterNames.class.getMethod( "main", String[].class );
for( final Parameter parameter: method.getParameters() ) {
System.out.println( "Parameter: " + parameter.getName() );
}</code></pre>
</section>
<section>
<h2 class="slide-header"><code>java.util.Base64</code></h2>
</section>
<section>
<h2 class="slide-header">Better Multithreading support</h2>
<ul>
<li><code>list.parallelStream()</code></li>
<li><code>stream.parallel()</code></li>
<li><code>ConcurrentMap.parallelStream()</code></li>
<li><code>Arrays.parallelSort()</code></li>
<li><code>Arrays.parallelSetAll()</code></li>
</ul>
</section>
<section>
<h2 class="slide-header">Small enhancements in <code>java.util.concurrent</code></h2>
</section>
</section>
<section>
<section>
<h2 class="slide-header" style="font-size:3em;margin-top: 50px">Tools & JVM</h2>
</section>
<section>
<h2 class="slide-header">jdeps</h2>
<p>Class dependency analyzer</p>
<pre><code class="mb-30">$ jdeps org.springframework.core-3.0.5.jar
org.springframework.core-3.0.5.jar -> lib\rt.jar
org.springframework.core (org.springframework.core-3.0.5.jar)
-> java.io
-> java.lang
-> java.lang.annotation
-> java.lang.ref
-> java.lang.reflect
-> java.util
-> java.util.concurrent
-> org.apache.commons.logging not found
-> org.springframework.asm not found
org.springframework.core.annotation (org.springframework.core-3.0.5.jar)
-> java.lang
-> java.lang.annotation
-> java.lang.reflect
-> java.util</code></pre>
</section>
<section>
<h2 class="slide-header">PermGen is now <br/><a href="http://www.javacodegeeks.com/2013/02/java-8-from-permgen-to-metaspace.html">MetaSpace</a></h2>
</section>
</section>
<section>
<h2 class="slide-header">Resources</h2>
<ul>
<li><a href="http://www.javacodegeeks.com/2014/05/java-8-features-tutorial.html">Java 8 Features – The ULTIMATE Guide</a></li>
<li><a href="https://www.techempower.com/blog/2013/03/26/everything-about-java-8/">Everything about Java 8</a></li>
<li><a href="http://java.dzone.com/articles/whats-wrong-java-8-currying-vs">Whats wrong with Java 8</a> (series of 7 articles)</li>
<li><a href="http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps">Streams API</a></li>
<li><a href="https://github.com/java8/Java8InAction/">Java 8 in Action</a></li>
</ul>
</section>
<section>
<h3 style="font-size:3em;margin-top:30px;margin-bottom:.5em">Thank you :)</h3>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: false,
slideNumber: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: 'linear', // default/cube/page/concave/linear(2d)
mouseWheel: true,
parallaxBackgroundImage: 'http://demo.thinkupthemes.com/spirits/wp-content/themes/Spirits/demo/images/background/background5.jpg',
parallaxBackgroundSize: '1626px 1229px', // 1229px // CSS syntax, e.g. "2100px 900px"
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/highlight.js', async: true, callback: function () {
window.hljs.initHighlightingOnLoad();
} },
{ src: 'lib/js/classList.js', condition: function () {
return !document.body.classList;
} },
{ src: 'lib/js/showdown.js', condition: function () {
return !!document.querySelector('[data-markdown]');
} },
{ src: 'lib/js/data-markdown.js', condition: function () {
return !!document.querySelector('[data-markdown]');
} },
{ src: 'plugin/zoom-js/zoom.js', condition: function () {
return !!document.body.classList;
} },
{ src: '/socket.io/socket.io.js', async: true, condition: function () {
return window.location.host === 'localhost:1947';
} },
{ src: 'plugin/speakernotes/client.js', async: true, condition: function () {
return window.location.host === 'localhost:1947';
} }
]
});
</script>
<script src="js/zepto.min.js"></script>
<script>
Zepto(function ($) {
var tableOfContents = $('#table-of-contents');
var tableList = $('<ol />')
$('.slide-header').each(function (index) {
var _self = $(this),
hash = '#/' + _self.closest('section').index();
tableList.append([
'<li>',
'<a href="' + hash + '">',
_self.text(),
'</a>',
'</li>'
].join(''));
});
tableOfContents.html(tableList);
});
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-54268066-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>