java puzzlers ng s02: down the rabbit hole as it was presented at the pittsburgh java meetup group
TRANSCRIPT
![Page 1: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/1.jpg)
![Page 2: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/2.jpg)
![Page 3: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/4.jpg)
![Page 5: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/5.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/6.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/7.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/8.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/10.jpg)
![Page 11: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/14.jpg)
![Page 15: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/15.jpg)
![Page 16: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/17.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/21.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/23.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/28.jpg)
![Page 29: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/29.jpg)
”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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/31.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/33.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/36.jpg)
![Page 37: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/38.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/39.jpg)
![Page 40: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/42.jpg)
![Page 43: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/46.jpg)
![Page 47: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/48.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/49.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/50.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/51.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/52.jpg)
Juggling Accident
![Page 53: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/53.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/54.jpg)
![Page 55: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/55.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/56.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/57.jpg)
![Page 58: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/58.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/59.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/60.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/61.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/62.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/63.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/64.jpg)
![Page 65: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/65.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/66.jpg)
![Page 67: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/67.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/68.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/69.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/70.jpg)
![Page 71: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/71.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/72.jpg)
![Page 73: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/73.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/74.jpg)
![Page 75: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/75.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/76.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/77.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/78.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/79.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/80.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/81.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/82.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/83.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/84.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/85.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/86.jpg)
@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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/87.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/88.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/89.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/90.jpg)
-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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/91.jpg)
-Trust us, we have much more where those came from.
-Puzzlers? Gotchas? Fetal position inducing behavior?
![Page 92: Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/92.jpg)
-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](https://reader035.vdocuments.site/reader035/viewer/2022081605/5a6d15547f8b9ab3418b5629/html5/thumbnails/93.jpg)
-Did you like it?-Praise us on twitter!
-#javapuzzlersng-@gamussa-@jbaruch
-Didn’t like it?-/dev/null