java 8 puzzlers [as presented at oscon 2016]

77
JAVA 8 PUZZLERS THE STRANGE, THE BIZARRE, AND THE WONDERFUL

Upload: baruch-sadogursky

Post on 14-Feb-2017

596 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Java 8 Puzzlers [as presented at  OSCON 2016]

JAVA 8 PUZZLERSTHE STRANGE, THE BIZARRE,

AND THE WONDERFUL

Page 2: Java 8 Puzzlers [as presented at  OSCON 2016]

whoiam

Developer Advocate @JFrog@jbaruch on the internetz

Page 3: Java 8 Puzzlers [as presented at  OSCON 2016]

whoiam

Solutions Architect @Hazelcast@gAmUssA on the internetz

Page 4: Java 8 Puzzlers [as presented at  OSCON 2016]

CLICK AND HACK

THE TYPING BROTHERS

Page 5: Java 8 Puzzlers [as presented at  OSCON 2016]

BTW,

Page 6: Java 8 Puzzlers [as presented at  OSCON 2016]

1. Two entertaining guys on stage2. Funny Puzzling questions3. You think and vote4. Awesome t-shirts fly in the air5. Official twitter handle!

JAVA8puzzlers

Page 7: Java 8 Puzzlers [as presented at  OSCON 2016]

FIRST RULE OF THE PUZZLERS:

NO CHEATING!

Page 8: Java 8 Puzzlers [as presented at  OSCON 2016]

Watching the puzzlers like… #dafaq

Page 9: Java 8 Puzzlers [as presented at  OSCON 2016]

Everything works (or doesn't) in the latest Java 8 update

Page 10: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 11: Java 8 Puzzlers [as presented at  OSCON 2016]

Broken Eggs Tale

Page 12: Java 8 Puzzlers [as presented at  OSCON 2016]

What will be the output?

A. milk/bread/sausageB. milk/bread/sausage/eggs, don’t forget eggs!C. milk/bread/sausage/ConcurrentModificationException

D. ConcurrentModificationException

List<String> list = new ArrayList<>();list.add("milk");list.add("bread");list.add("sausage");Stream<String> stream = list.stream();list.add("eggs, don’t forget eggs!");stream.forEach(System.out::println);

Page 13: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 14: Java 8 Puzzlers [as presented at  OSCON 2016]

Late binding, duh…

List<String> list = new ArrayList<>();list.add("milk");list.add("bread");list.add("sausage");Stream<String> stream = list.stream();list.add("eggs, don’t forget eggs!");stream.forEach(System.out::println);

Page 15: Java 8 Puzzlers [as presented at  OSCON 2016]

Late binding, duh…

List<String> list = new ArrayList<>();list.add("milk");list.add("bread");list.add("sausage");Stream<String> stream = list.stream();list.add("eggs, don’t forget eggs!");stream.forEach(System.out::println);

Page 16: Java 8 Puzzlers [as presented at  OSCON 2016]

Going Vegan

Page 17: Java 8 Puzzlers [as presented at  OSCON 2016]

What will be the output?

A. milk/bread/sausageB. milk/bread/eggs, don’t forget eggs!C. milk/bread/ConcurrentModificationExceptionD. ConcurrentModificationException

List<String> list = new ArrayList<>();list.add("milk");list.add("bread");list.add("sausage");list = list.subList(0, 2); //No sausage, please!Stream<String> stream = list.stream();list.add("eggs, don’t forget eggs!");stream.forEach(System.out::println);

Page 18: Java 8 Puzzlers [as presented at  OSCON 2016]

ONE DOES NOT SIMPLY

GIVE UP ON SAUSAGES

Page 19: Java 8 Puzzlers [as presented at  OSCON 2016]

Sometimes it’s just a bug…

Page 20: Java 8 Puzzlers [as presented at  OSCON 2016]

Execute ’em all

Page 21: Java 8 Puzzlers [as presented at  OSCON 2016]

What’s the difference between 1 and 2?

A. 1 compiles, 2 does notB. 2 compiles, 1 does notC. Same same, both work fineD. Same same, both won’t compile

public void killAll(){ ExecutorService ex = Executors.newSingleThreadExecutor(); List<String> sentence = Arrays.asList("Punish"); ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1 ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2}

Page 22: Java 8 Puzzlers [as presented at  OSCON 2016]

Semicolons are the evil!

Page 23: Java 8 Puzzlers [as presented at  OSCON 2016]

What’s the difference between 1 and 2?

A. 1 compiles, 2 does notB. 2 compiles, 1 does notC. Same same, both work fineD. Same same, both won’t compile

public void killAll(){ ExecutorService ex = Executors.newSingleThreadExecutor(); List<String> sentence = Arrays.asList("Punish"); ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1 ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2}

Page 24: Java 8 Puzzlers [as presented at  OSCON 2016]

public void killAll(){ ExecutorService ex = Executors.newSingleThreadExecutor(); List<String> sentence = Arrays.asList("Punish"); ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1 ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2}

@FunctionalInterfacepublic interface Runnable {

public abstract void run();}

@FunctionalInterfacepublic interface Callable<V> {

V call() throws Exception;}

Implicit return

Explicit return

Page 25: Java 8 Puzzlers [as presented at  OSCON 2016]

Mad Max

Page 26: Java 8 Puzzlers [as presented at  OSCON 2016]

How that will work?

A. Compilation errorB. Runtime ExceptionC. 3D. Something else

System.out.println( Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get());

Page 27: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 28: Java 8 Puzzlers [as presented at  OSCON 2016]

How about now?

A. −3B. −1C. 0D. Something else

System.out.println( Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get());

Page 29: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 30: Java 8 Puzzlers [as presented at  OSCON 2016]

How about now?

A. −3B. −1C. 0D. Something else

System.out.println( Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get());

Page 31: Java 8 Puzzlers [as presented at  OSCON 2016]

• Math.max(−3, −2) = −2 < 0 −3 < −2, selecting−2• Math.max(−2, −1) = −1 < 0 −2 < −1,

selecting−1• Math.max(−1, 0) = 0 −1 == 0,

keeping−1• Math.max(−1, 1) = 1 > 0 −1 > 1,

keeping−1• Math.max(−1, 2) = 2 > 0 −1 > 2,

keeping−1• Math.max(−1, 3) = 3 > 0 −1 > 3,

keeping−1

Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get()

Page 32: Java 8 Puzzlers [as presented at  OSCON 2016]

Let’s upgrade the stack!

Page 33: Java 8 Puzzlers [as presented at  OSCON 2016]

What will happen?

A.Maps will switchB.Both will become oldSchoolC.Both will become hipsterD.Really?! That won’t even compile!

Map<String, String> oldSchool = initOldSchoolStack();// oldSchool = {buildTool=maven, lang=java, db=db2}

Map<String, String> proper = initHipsterStack();// proper = {buildTool=npm, lang=javascript, db=elastic}

oldSchool.replaceAll(proper::put);

Page 34: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 35: Java 8 Puzzlers [as presented at  OSCON 2016]

void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

V put(K key, V value);

Map interface

oldSchool.replaceAll(proper::put);

Page 36: Java 8 Puzzlers [as presented at  OSCON 2016]

void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

V put(K key, V value);

Map interface

final BiFunction<String, String, String> function = (key, value) -> proper.put(key, value);

for (Map.Entry<String, String> entry : oldSchool.entrySet()) entry.setValue(function.apply(entry.getKey(), entry.getValue()));

Page 37: Java 8 Puzzlers [as presented at  OSCON 2016]

void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

V put(K key, V value);

Map interface

final BiFunction<String, String, String> function = (key, value) -> proper.put(key, value);

for (Map.Entry<String, String> entry : oldSchool.entrySet()) entry.setValue(function.apply(entry.getKey(), entry.getValue()));

Page 38: Java 8 Puzzlers [as presented at  OSCON 2016]

ALL THE LISTS!

MAX

Page 39: Java 8 Puzzlers [as presented at  OSCON 2016]

How many lines will be the same?List<String> kitties = Arrays.asList("Soft", "Warm", "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(Collections.max(kitties, kittiesComparator));System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());System.out.println(kitties.stream().max(kittiesComparator).get());

A.All lines the sameB.Two lines the sameC.All differentD.Four different

Page 40: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 41: Java 8 Puzzlers [as presented at  OSCON 2016]

How about now?List<String> kitties = Arrays.asList("Soft", null, "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(Collections.max(kitties, kittiesComparator));System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());System.out.println(kitties.stream().max(kittiesComparator).get());

A.All lines the sameB.Two lines the sameC.All differentD.Four different

Page 42: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 43: Java 8 Puzzlers [as presented at  OSCON 2016]

How about now?List<String> kitties = Arrays.asList("Soft", null, "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(Collections.max(kitties, kittiesComparator));System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());System.out.println(kitties.stream().max(kittiesComparator).get());

A.All lines the sameB.Two lines the sameC.All differentD.Four different

Page 44: Java 8 Puzzlers [as presented at  OSCON 2016]

List<String> kitties = Arrays.asList("Soft", null, "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(Collections.max(kitties, kittiesComparator));

Page 45: Java 8 Puzzlers [as presented at  OSCON 2016]

List<String> kitties = Arrays.asList("Soft", null, "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());

Page 46: Java 8 Puzzlers [as presented at  OSCON 2016]

List<String> kitties = Arrays.asList("Soft", null, "Purr");Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());

System.out.println(kitties.stream().max(kittiesComparator).get());

Page 47: Java 8 Puzzlers [as presented at  OSCON 2016]

nullCaught: java.lang.NoSuchElementExceptionCaught: java.lang.NullPointerException

Consistency, yeah.

Page 48: Java 8 Puzzlers [as presented at  OSCON 2016]

Mutants

Page 49: Java 8 Puzzlers [as presented at  OSCON 2016]

How to cast to a type without declaring it?interface Cat{ default void meow() {System.out.println(”meow ");}}interface Dog{ default void bark() {System.out.println(”woof ");}}

public static void main(String[] args) { class Dogcatimplements Dog, Cat{} test(new Dogcat());}

static void test(Object obj) {def x = (?)obj;x.meow ();x.bark ();

}

Page 50: Java 8 Puzzlers [as presented at  OSCON 2016]

How to cast to a type without declaring it?

static void test(Object obj) { // A. Will that work?Dog& Catx = (Dog& Cat) obj;x.meow ();x.bark ();}

static void test(Object obj) { // B. Will that work? ((Consumer<? extends Dog& Cat>)(x -> { x.meow (); x.bark ();})).accept((Dog& Cat)obj); }

static void test(Object obj) { // C. Will that work? Optional.of((Dog& Cat) obj) .ifPresent(x -> { x.meow (); x.bark (); });}

// D. You’re two sick bastards.

interface Cat{ default void meow() {System.out.println(”meow");}}interface Dog{ default void bark() {System.out.println(”woof");}}

public static void main(String[] args) { class Dogcat implements Dog, Cat{} test(new Dogcat());}

Page 51: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 52: Java 8 Puzzlers [as presented at  OSCON 2016]

How to cast to a type without declaring it?

static void test(Object obj) { // A. Will that work?Dog & Cat x = (Dog & Cat) obj;x.meow();x.bark();}

static void test(Object obj) { // B. Will that work? ((Consumer<? extends Dog & Cat>)(x -> { x.meow(); x.bark();})).accept((Dog & Cat)obj); }

static void test(Object obj) { // C. Will that work? Optional.of((Dog & Cat) obj) .ifPresent(x -> { x.meow(); x.bark(); });}

// D. You’re two sick bastards.

interface Cat{ default void meow() {System.out.println(”meow");}}interface Dog{ default void bark() {System.out.println(”woof");}}

public static void main(String[] args) { static class Dogcat implements Dog, Cat{} test(new Dogcat());}

Page 53: Java 8 Puzzlers [as presented at  OSCON 2016]

Bill Gates explains how that works

Page 54: Java 8 Puzzlers [as presented at  OSCON 2016]

static void test(Object obj) { // C. Will that work? Optional.of((Dog & Cat) obj) .ifPresent(x -> { x.meow(); x.bark(); });}

Page 55: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 56: Java 8 Puzzlers [as presented at  OSCON 2016]

Viktor Gamov and Baruch Sadogursky call customer service:

Page 57: Java 8 Puzzlers [as presented at  OSCON 2016]

What will be the output?

1. HOTEL ECHO LIMA LIMA OSCAR/ HOTEL ECHO LIMA LIMA OSCAR

2. HELLO / HOTEL ECHO LIMA LIMA OSCAR3. HOTEL ECHO LIMA LIMA OSCAR/ HELLO4. HELLO/HELLO

public class Test { String str;

void run() { str = "hello "; Supplier<String> s1 = str::toUpperCase; Supplier<String> s2 = () -> str.toUpperCase(); str = "Hotel Echo Lima Lima Oscar "; System.out.println(s1.get()); System.out.println(s2.get()); }}

Page 58: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 59: Java 8 Puzzlers [as presented at  OSCON 2016]

What will be the output?

1. HOTEL ECHO LIMA LIMA OSCAR/ HOTEL ECHO LIMA LIMA OSCAR

2. HELLO / HOTEL ECHO LIMA LIMA OSCAR3. HOTEL ECHO LIMA LIMA OSCAR/ HELLO4. HELLO/HELLO

public class Test { String str;

void run() { str = ”hello"; Supplier<String> s1 = str::toUpperCase; Supplier<String> s2 = () -> str.toUpperCase(); str = ”Hotel Echo Lima Lima Oscar"; System.out.println(s1.get()); System.out.println(s2.get()); }}

Page 60: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 61: Java 8 Puzzlers [as presented at  OSCON 2016]

What will happen?

1. ConcurrentModificationException2. ArrayIndexOutOfBoundsException3. NullPointerException4. No exceptions, all good

List<String> list = new ArrayList<>(Arrays.asList("Arnie", "Chuck", "Slay"));list.stream().forEach(x -> { if(x.equals("Chuck")) { list.remove(x); }});

Page 62: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 63: Java 8 Puzzlers [as presented at  OSCON 2016]

Java 8 vs Chuck Norris

Page 64: Java 8 Puzzlers [as presented at  OSCON 2016]

What will happen?

A. ConcurrentModificationExceptionB. ArrayIndexOutOfBoundsExceptionC. NullPointerExceptionD. No exceptions, all good

List<String> list = new ArrayList<>(Arrays.asList("Arnie", "Chuck", "Slay"));list.stream().forEach(x -> { if(x.equals("Chuck")) { list.remove(x); }});

Page 65: Java 8 Puzzlers [as presented at  OSCON 2016]

Here’s why:stream().forEach() spliterator().forEachRemaining()

forEachRemaining checks for mod count once, in the end

Removing element adds null to the end of the array:["Arne", "Chuck", "Slay"] ["Arne", "Slay", null]

On the last iteration if(null.equals("Chuck")) fails with NPE (didn’t get to CME)

Use list.removeIf("Chuck"::equals);

Page 66: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 67: Java 8 Puzzlers [as presented at  OSCON 2016]

OR ELSE…

RTFM

Page 68: Java 8 Puzzlers [as presented at  OSCON 2016]

System.out.println(Optional.of("rtfm").orElseGet(null));System.out.println(Optional.empty().map(null).orElse("rtfm"));

What will be the output?

A.rtfm / rtfmB.rtfm / NullPointerExceptionC.NullPointerException /

NullPointerExceptionD.NullPointerException / rtfm

Page 69: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 70: Java 8 Puzzlers [as presented at  OSCON 2016]

System.out.println(Optional.of("rtfm").orElseGet(null));System.out.println(Optional.empty().map(null).orElse("rtfm"));

What will be the output?

A.rtfm /rtfmB.rtfm / NullPointerExceptionC.NullPointerException /

NullPointerExceptionD.NullPointerException / rtfm

Page 71: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 72: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 73: Java 8 Puzzlers [as presented at  OSCON 2016]
Page 74: Java 8 Puzzlers [as presented at  OSCON 2016]

Conclusions

Page 75: Java 8 Puzzlers [as presented at  OSCON 2016]

- Write readable code!- Comment all the tricks- Sometimes it’s a bug- Static code analysis FTW - intellij IDEA!- Rtfm- Don’t abuse lambdas and streams!

Page 76: Java 8 Puzzlers [as presented at  OSCON 2016]

- Trust us, we have much more where those came from. - Puzzlers? Gotchas? Fetal position inducing behavior?

- puzzlers jfrog.com

Page 77: Java 8 Puzzlers [as presented at  OSCON 2016]

Did you like it?Praise us on twitter and in the feedback form!

- java8puzzlers- gamussa - jbaruch

Didn’t like it?/dev/null