Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

93

Transcript of Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Page 1: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 2: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 3: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Developer Advocate @JFrog@jbaruch on the internetz

Solution Architect @Confluent@gAmUssA on the internetz

Page 4: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 5: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@tagir_valeev@jbaruch http://jfrog.com/shownotes @gamussa

Page 6: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 7: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 8: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 9: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

1. Two entertaining guys on the stage

2. Funny puzzling questions3. You think and vote4.T-shirts are airborne5. Official twitter hashtag:

#javapuzzlersng

Page 10: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 11: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Which Java version are you on?

A. Java 7B. Java 8C. Java 9D. Java 6E. Java 5F. Java 2

@jbaruch http://jfrog.com/shownotes @gamussa

Page 12: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Watching the puzzlers like… #dafaq

@jbaruch http://jfrog.com/shownotes @gamussa

Page 13: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

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

@jbaruch http://jfrog.com/shownotes @gamussa

Page 14: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 15: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 16: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

public class PerfectRobbery {private Semaphore bankAccount = new Semaphore(-42);public static void main(String[] args) {

PerfectRobbery perfectRobbery = new PerfectRobbery();perfectRobbery.takeAllMoney();perfectRobbery.checkBalance();

}public void takeAllMoney(){

bankAccount.drainPermits();}public void checkBalance(){

System.out.println(bankAccount.availablePermits());}

}

A. IllegalArgumentException – can’t create semaphore with negativeB. UnsupportedOperationException – can’t drain when negativeC. 0D. -42

Page 17: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 18: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. IllegalArgumentException – can’t create semaphore with negativeB. UnsupportedOperationException – can’t drain when negativeC. 0D. -42

public class PerfectRobbery {private Semaphore bankAccount = new Semaphore(-42);public static void main(String[] args) {

PerfectRobbery perfectRobbery = new PerfectRobbery();perfectRobbery.takeAllMoney();perfectRobbery.checkBalance();

}public void takeAllMoney(){

bankAccount.drainPermits();}public void checkBalance(){

System.out.println(bankAccount.availablePermits());}

}

Page 19: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Available -42?!

@jbaruch http://jfrog.com/shownotes @gamussa

Page 20: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Available -42?!

@jbaruch http://jfrog.com/shownotes @gamussa

Page 21: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 22: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. true/trueB. true/falseC. false/trueD. false/false

Collections.emptyList() == Collections.emptyList();Collections.emptyIterator() == Collections.emptyIterator();

@jbaruch http://jfrog.com/shownotes @gamussa

Page 23: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 24: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. true/trueB. true/falseC. false/trueD. false/false

Spliterators.emptySpliterator() == Spliterators.emptySpliterator();Stream.empty() == Stream.empty();

Page 25: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Singleton Strikes Back!

@jbaruch http://jfrog.com/shownotes @gamussa

Page 26: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. true/trueB. true/falseC. false/trueD. false/false

Spliterators.emptySpliterator() == Spliterators.emptySpliterator();Stream.empty() == Stream.empty();

@jbaruch http://jfrog.com/shownotes @gamussa

Page 27: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Even empty Stream has state!

@jbaruch http://jfrog.com/shownotes @gamussa

Page 28: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 29: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

”Identical”

1. Has the same state

2. Not related to “equals and hashcode” contract

3. Not related to references to objects in memory

@jbaruch http://jfrog.com/shownotes @gamussa

Page 30: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

List[] twins = new List[2];Arrays.setAll(twins, ArrayList::new);

A. Absolutely identical empty listsB. Absolutely identical non-empty listsC. Non-identical empty listsD. Non-identical non-empty lists

@jbaruch http://jfrog.com/shownotes @gamussa

Page 31: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 32: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

List[] twins = new List[2];Arrays.setAll(twins, ArrayList::new);

A. Absolutely identical empty listsB. Absolutely identical non-empty listsC. Non-identical empty listsD. Non-identical non-empty lists

@jbaruch http://jfrog.com/shownotes @gamussa

Page 33: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 34: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

How single is a Single Abstract Method Interface?

@jbaruch http://jfrog.com/shownotes @gamussa

Page 35: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. WTF?! ’Single’ means one, not three!B. Problem is with partyHard(T), remove it and it will workC. Problem is the drinkIn methods, removing one of them and it will

workD. It will work fine! Both partyHard() and drinkIn() are merged in

SingleAndHappy, leaving one abstract method

public interface Single<T> {default void partyHard(String songName) { System.out.println(songName); }void partyHard(T songName);void drinkIn(T drinkName);void drinkIn(String dringName);

}@FunctionalInterfacepublic interface SingleAndHappy extends Single<String> { }

@jbaruch http://jfrog.com/shownotes @gamussa

Page 36: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 37: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. WTF?! ’Single’ means one, not three!B. Problem is with partyHard(T), remove it and it will workC. Problem are the drinkIn methods, removing it will leave one abstract

methodD. Yes! Both partyHard() and drinkIn() are merged in SingleAndHappy,

leaving one abstract method

public interface Single<T> {default void partyHard(String songName) { System.out.println(songName); }void partyHard(T songName);void drinkIn(T drinkName);void drinkIn(String dringName);

}@FunctionalInterfacepublic interface SingleAndHappy extends Single<String> { }

@jbaruch http://jfrog.com/shownotes @gamussa

Page 38: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 39: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 40: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Hacking the bank

☑Bank software written in Java☑Hack into it☑Analyze the accounts

@jbaruch http://jfrog.com/shownotes @gamussa

Page 41: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Given the code above, which statement is wrong:A. The Set is ordered by hashcodeB. The order is predictable across multiple runs of the JVM on the same machineC. The order of elements in Set is not predictableD. Statements A & B are correct

Set<String>accounts=newHashSet<>(Arrays.asList("Gates","Buffett","Bezos","Zuckerberg"));System.out.println(”accounts="+accounts);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 42: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 43: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Given the code above, which statement is wrong:A. The Set is orderedB. The order is predictable across multiple runs of the JVM on the same machineC. The order of elements in Set is not predictableD. Statements A & B are correct

Set<String>accounts=newHashSet<>(Arrays.asList("Gates","Buffett","Bezos","Zuckerberg"));System.out.println(”accounts="+accounts);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 44: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

public boolean add(E e) {return map.put(e, PRESENT)==null;

}

@jbaruch http://jfrog.com/shownotes @gamussa

Page 45: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Your turn, FBI

@jbaruch http://jfrog.com/shownotes @gamussa

Page 46: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 47: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Given the code above, which statement is wrong:A. The Set is orderedB. The order is predictable across multiple runs of the JVM on the same machineC. The order of elements in Set is not predictableD. Statements A & B are correct

Set<String>accounts=Set.of("Gates","Buffett","Bezos","Zuckerberg");System.out.println(”accounts="+accounts);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 48: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 49: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 50: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Given the code above, which statement is wrong:A. The Set is orderedB. The order is predictable across multiple runs of the JVM on the same machineC. The order of elements in Set is not predictableD. Statements A & B are correct

Set<String>accounts=Set.of("Gates","Buffett","Bezos","Zuckerberg");System.out.println(”accounts="+accounts);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 51: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

private int probe(Object pe) {int idx = Math.floorMod(pe.hashCode() ^ SALT, elements.length);while (true) {

E ee = elements[idx];if (ee == null) {

return -idx - 1;} else if (pe.equals(ee)) {

return idx;} else if (++idx == elements.length) {

idx = 0;}

}}

@jbaruch http://jfrog.com/shownotes @gamussa

Page 52: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Juggling Accident

Page 53: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

What’s correct?A. If you convert your application to module, classpath

dependencies will still be resolved correctlyB. If one of the dependencies was converted to a module, you

have to declare it in module-info in order to useC. Once you added the module-info to your project you have to

declare the dependencies twice, in classpath and in module-info

D. None of the above@jbaruch http://jfrog.com/shownotes @gamussa

Page 54: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 55: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

What’s correct?A. If you convert your application to module, classpath

dependencies will still be resolved correctlyB. If one of the dependencies was converted to a module, you

have to declare it in module-info in order to useC. Once you added the module-info to your project you have to

declare the dependencies twice, in classpath and in module-info

D. None of the above@jbaruch http://jfrog.com/shownotes @gamussa

Page 56: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 57: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 58: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. You killed them allB. You killed only even onesC. They all survivedD. You killed only odd onesE. All answers are correct

static void killThemAll(Collection<Hero> expendables) {Iterator<Hero> heroes = expendables.iterator();heroes.forEachRemaining(e -> {

if (heroes.hasNext()) {heroes.next();heroes.remove();

}});System.out.println(expendables);

}

Page 59: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 60: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. You killed them allB. You killed only even onesC. They all survivedD. You killed only odd onesE. All answers are correct

static void killThemAll(Collection<Hero> expendables) {Iterator<Hero> heroes = expendables.iterator();heroes.forEachRemaining(e -> {

if (heroes.hasNext()) {heroes.next();heroes.remove();

}});System.out.println(expendables);

}

Page 61: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Don’t do that. Really, don’t.

killThemAll(new ArrayList<String>(Arrays.asList("N","S","W","S","L","S","L","V")));[]

killThemAll(new LinkedList<String>(Arrays.asList("N","S","W","S","L","S","L","V")));[S,S,S,V]

killThemAll(new ArrayDeque<String>(Arrays.asList("N","S","W","S","L","S","L","V")));[N,S,W,S,L,S,L,V]

killThemAll(new TreeSet<String>(Arrays.asList("N","S","W","S","L","S","L","V")));[N,W,L,L]

@jbaruch http://jfrog.com/shownotes @gamussa

Page 62: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Subtle Difference

@jbaruch http://jfrog.com/shownotes @gamussa

Page 63: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. Both work just fineB. Lambda works, method ref failsC. Method ref works, lambda failsD. Won’t compile

@FunctionalInterfacepublic interface OriginalPredicate<T> {

boolean test(T t);}

OriginalPredicate<Object> lambda = (Object obj) -> ”adidas".equals(obj);OriginalPredicate<Object> methodRef = ”adidas"::equals;

Page 64: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 65: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. Both work just fineB. Lambda works, method ref failsC. Method ref works, lambda failsD. Not a functional interface, will fail on annotation processing

@FunctionalInterfacePublic interface CopyCatPredicate {

<T> boolean test(T t);}

CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj);CopyCatPredicate methodRef = " adadas"::equals;

Page 66: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 67: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. Both work just fineB. Lambda works, method ref failsC. Method ref works, lambda failsD. Not a functional interface, will fail on annotation processing

@FunctionalInterfacePublic interface CopyCatPredicate {

<T> boolean test(T t);}

CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj);CopyCatPredicate methodRef = " adadas"::equals;

Page 68: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 69: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Agenericfunctiontypeforafunctionalinterfacemaybeimplementedbyamethodreferenceexpression(§15.13),butnotbyalambdaexpression(§15.27)asthereisnosyntaxforgenericlambdaexpressions.“

@jbaruch http://jfrog.com/shownotes @gamussa

Page 70: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 71: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A.[Data, Kirk, Spock]B.[Spock, Kirk, Data, Data, Kirk, Spock]C.[Spock, Kirk, Data]D.[Data, Data, Kirk, Kirk, Spock, Spock]E.Are you nuts? Won’t compile! Data with

Kirk?!

List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()

.filter(new TreeSet<>()::add).collect(Collectors.toList());System.out.println(list);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 72: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 73: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A.[Data, Kirk, Spock]B.[Spock, Kirk, Data, Data, Kirk, Spock]C.[Spock, Kirk, Data]D.[Data, Data, Kirk, Kirk, Spock, Spock]E.Are you nuts? Won’t compile! Data with

Kirk?!

List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()

.filter(new TreeSet<>()::add).collect(Collectors.toList());System.out.println(list);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 74: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Page 75: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A.[Data, Kirk, Spock]B.[Spock, Kirk, Data, Data, Kirk, Spock]C.[Spock, Kirk, Data]D.[Data, Data, Kirk, Kirk, Spock, Spock]E.Are you nuts? Won’t compile! Data with

Kirk?!

List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()

.filter(new TreeSet<>()::add).collect(Collectors.toList());System.out.println(list);

@jbaruch http://jfrog.com/shownotes @gamussa

Page 76: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!=

Newinstanceiscreatedeverytime!

Instancemethodiscreatedonce!

@jbaruch http://jfrog.com/shownotes @gamussa

Page 77: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 78: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. obvious / obviousB. obvious / NullPointerExceptionC. NullPointerException / obviousD. NullPointerException / NullPointerException

Optional.of("obvious").orElseGet(null);Optional.empty().map(null).orElse("obvious");

@jbaruch http://jfrog.com/shownotes @gamussa

Page 79: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 80: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. obvious / obviousB. obvious / NullPointerExceptionC. NullPointerException / obviousD. NullPointerException / NullPointerException

Optional.of("obvious").orElseGet(null);Optional.empty().map(null).orElse("obvious");

WillneverhappenWillneverhappen

@jbaruch http://jfrog.com/shownotes @gamussa

Page 81: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 82: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. obvious / obviousB. obvious / NullPointerExceptionC. NullPointerException / obviousD. NullPointerException / NullPointerException

Optional.of("obvious").orElseGet(null);Optional.empty().map(null).orElse("obvious");

@jbaruch http://jfrog.com/shownotes @gamussa

Page 83: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 84: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Identical?

@jbaruch http://jfrog.com/shownotes @gamussa

Page 85: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. AllB. 3 and 4C. Only 3D. Other

1. Consumer<String> agentA = s -> System.out.println(s);Consumer<String> agentB = s -> System.out.println(s);

2. Consumer<String> agentA = System.out::println;Consumer<String> agentB = System.out::println;

3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s);Consumer<String> agentA = supplier.get();Consumer<String> agentB = supplier.get();

4. Supplier<Consumer<String>> supplier = () -> System.out::println;Consumer<String> agentA = supplier.get();Consumer<String> agentB = supplier.get();

When agentA == agentB?

Page 86: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

@jbaruch http://jfrog.com/shownotes @gamussa

Page 87: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

A. AllB. 3 and 4C. Only 3D. Other

1. Consumer<String> agentA = s -> System.out.println(s);Consumer<String> agentB = s -> System.out.println(s);

2. Consumer<String> agentA = System.out::println;Consumer<String> agentB = System.out::println;

3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s);Consumer<String> agentA = supplier.get();Consumer<String> agentB = supplier.get();

4. Supplier<Consumer<String>> supplier = () -> System.out::println;Consumer<String> agentA = supplier.get();Consumer<String> agentB = supplier.get();

When agentA == agentB?

Page 88: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Reuse is only possible for pure functions

Consumers accept parameters == have state

Supplier in 4 has state – the resolved method reference

@jbaruch http://jfrog.com/shownotes @gamussa

Page 89: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

Conclusions

@jbaruch http://jfrog.com/shownotes @gamussa

Page 90: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

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

Page 91: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

-Trust us, we have much more where those came from.

-Puzzlers? Gotchas? Fetal position inducing behavior?

[email protected]

Page 92: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

-Shownotes!-http://jfrog.com/shownotes-Slides-Video in 24 hours-Links-Ratings-Raffle! (come early)

Page 93: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

-Did you like it?-Praise us on twitter!

-#javapuzzlersng-@gamussa-@jbaruch

-Didn’t like it?-/dev/null