Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

[Philly ETE] Java Puzzlers NG

350 views

Published on

The more we work with Java 8, the more we go into the rabbit hole. Did they add all those streams, lambdas, monads, Optionals and CompletableFutures only to confuse us? It surely looks so! And Java 9 that heads our way brings even more of what we like the most, more puzzlers, of course! In this season we as usual have a great batch of the best Java WTF, great jokes to present them and great prizes for the winners!

Published in: Software
  • Hi there! Get Your Professional Job-Winning Resume Here - Check our website! http://bit.ly/resumpro
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

[Philly ETE] Java Puzzlers NG

  1. 1. This slide is intentionally left blank
  2. 2. JAVA PUZZLERS NG DOWN THE RABBIT HOLEhttp://www.davidfloresmedia.com/philly-rocky-statue/
  3. 3. @gAmUssA @hazelcast #jbreak #hazelcastjet > whoami
  4. 4. @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect > whoami
  5. 5. @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect Developer Advocate > whoami
  6. 6. @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect Developer Advocate @gamussa in internetz > whoami
  7. 7. @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect Developer Advocate @gamussa in internetz Please, follow me on Twitter > whoami
  8. 8. @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect Developer Advocate @gamussa in internetz Please, follow me on Twitter I’m very interesting © > whoami
  9. 9. Developer Advocate @JFrog on the internetz Developer Advocate @Hazelcast @gAmUssA on the internetz
  10. 10. CLICK AND HACK THE TYPE-IT BROTHERS
  11. 11. @tagir_valeev @gamussa #PhillyETE
  12. 12. @gamussa #PhillyETE
  13. 13. @gamussa #PhillyETE
  14. 14. @gamussa #PhillyETE
  15. 15. 1. Two One entertaining guy on the stage
  16. 16. 1. Two One entertaining guy on the stage 2. Funny puzzling questions
  17. 17. 1. Two One entertaining guy on the stage 2. Funny puzzling questions 3. You think and vote
  18. 18. 1. Two One entertaining guy on the stage 2. Funny puzzling questions 3. You think and vote 4.Official twitter hashtags #javapuzzlersng #PhillyETE
  19. 19. FIRST RULE OF THE PUZZLERS: NO CHEATING!
  20. 20. Which Java version are you on? @gamussa #PhillyETE
  21. 21. Which Java version are you on? A. Java 7 @gamussa #PhillyETE
  22. 22. Which Java version are you on? A. Java 7 B. Java 8 @gamussa #PhillyETE
  23. 23. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 @gamussa #PhillyETE
  24. 24. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 @gamussa #PhillyETE
  25. 25. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 E. Java 5 @gamussa #PhillyETE
  26. 26. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 E. Java 5 F. Java 2 @gamussa #PhillyETE
  27. 27. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 E. Java 5 F. Java 2 @gamussa #PhillyETE
  28. 28. Watching the puzzlers like… #dafaq @gamussa #PhillyETE
  29. 29. Watching the puzzlers like… #dafaq @gamussa #PhillyETE
  30. 30. Everything works (or doesn't) 
 in the latest Java 8 and/or 9 update @gamussa #PhillyETE
  31. 31. RUSSIAN HACKERS HACKING THE BANK
  32. 32. 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());
 }
 }

  33. 33. 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 negative
  34. 34. 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 negative B. UnsupportedOperationException – can’t drain when negative
  35. 35. 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 negative B. UnsupportedOperationException – can’t drain when negative C. 0
  36. 36. 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 negative B. UnsupportedOperationException – can’t drain when negative C. 0 D. -42
  37. 37. 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 negative B. UnsupportedOperationException – can’t drain when negative C. 0 D. -42
  38. 38. @gamussa #PhillyETE
  39. 39. @gamussa #PhillyETE
  40. 40. @gamussa #PhillyETE
  41. 41. A. IllegalArgumentException – can’t create semaphore with negative B. UnsupportedOperationException – can’t drain when negative C. 0 D. -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());
 }
 }

  42. 42. @gamussa #PhillyETE
  43. 43. @gamussa #PhillyETE
  44. 44. @gamussa #PhillyETE
  45. 45. Available -42?! @gamussa #PhillyETE
  46. 46. Available -42?! @gamussa #PhillyETE
  47. 47. @gamussa #PhillyETE
  48. 48. Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  49. 49. A. true/true Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  50. 50. A. true/true B. true/false Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  51. 51. A. true/true B. true/false C. false/true Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  52. 52. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  53. 53. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() == Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  54. 54. @gamussa #PhillyETE
  55. 55. @gamussa #PhillyETE
  56. 56. Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  57. 57. A. true/true Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  58. 58. A. true/true B. true/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  59. 59. A. true/true B. true/false C. false/true Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  60. 60. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  61. 61. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();

  62. 62. Singleton Strikes Back! @gamussa #PhillyETE
  63. 63. Singleton Strikes Back! @gamussa #PhillyETE
  64. 64. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();
 @gamussa #PhillyETE
  65. 65. Even empty Stream has state! @gamussa #PhillyETE
  66. 66. Even empty Stream has state! @gamussa #PhillyETE
  67. 67. Even empty Stream has state! @gamussa #PhillyETE
  68. 68. ”Identical” 1. Has the same state 2. Not related to “equals and hashcode” contract 3. Not related to references to objects in memory @gamussa #PhillyETE
  69. 69. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 @gamussa #PhillyETE
  70. 70. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists @gamussa #PhillyETE
  71. 71. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists B. Absolutely identical non-empty lists @gamussa #PhillyETE
  72. 72. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists @gamussa #PhillyETE
  73. 73. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists D. Non-identical non-empty lists @gamussa #PhillyETE
  74. 74. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists D. Non-identical non-empty lists @gamussa #PhillyETE
  75. 75. @gamussa #PhillyETE
  76. 76. @gamussa #PhillyETE
  77. 77. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists D. Non-identical non-empty lists @gamussa #PhillyETE
  78. 78. @gamussa #PhillyETE
  79. 79. @gamussa #PhillyETE
  80. 80. @gamussa #PhillyETE
  81. 81. How single is a Single Abstract Method Interface? @gamussa #PhillyETE
  82. 82. 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  83. 83. A. WTF?! ’Single’ means one, not three! 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  84. 84. A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard(T), remove it and it will work 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  85. 85. A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard(T), remove it and it will work C. Problem is the drinkIn methods, removing one of them and it will work 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  86. 86. A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard(T), remove it and it will work C. Problem is the drinkIn methods, removing one of them and it will work D. 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  87. 87. A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard(T), remove it and it will work C. Problem is the drinkIn methods, removing one of them and it will work D. 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  88. 88. A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard(T), remove it and it will work C. Problem are the drinkIn methods, removing it will leave one abstract method D. 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);
 
 }
 @FunctionalInterface
 public interface SingleAndHappy extends Single<String> { } @gamussa #PhillyETE
  89. 89. @gamussa #PhillyETE
  90. 90. RUSSIAN HACKERS STILL TRYING TO HACK THE BANK
  91. 91. Hacking the bank ☑Bank software written in Java ☑Hack into it ☑Analyze the accounts @gamussa #PhillyETE
  92. 92. Given the code above, which statement is wrong: Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  93. 93. Given the code above, which statement is wrong: A. The Set is ordered by hashcode Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  94. 94. Given the code above, which statement is wrong: A. The Set is ordered by hashcode B. The order is predictable across multiple runs of the JVM on the same machine Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  95. 95. Given the code above, which statement is wrong: A. The Set is ordered by hashcode B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  96. 96. Given the code above, which statement is wrong: A. The Set is ordered by hashcode B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  97. 97. Given the code above, which statement is wrong: A. The Set is ordered by hashcode B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  98. 98. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg"));
 System.out.println(”accounts= " + accounts);
 @gamussa #PhillyETE
  99. 99. public boolean add(E e) {
 return map.put(e, PRESENT)==null;
 } @gamussa #PhillyETE
  100. 100. Your turn, FBI @gamussa #PhillyETE
  101. 101. Given the code above, which statement is wrong: Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  102. 102. Given the code above, which statement is wrong: A. The Set is ordered Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  103. 103. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  104. 104. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  105. 105. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  106. 106. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  107. 107. @gamussa #PhillyETE
  108. 108. @gamussa #PhillyETE
  109. 109. @gamussa #PhillyETE
  110. 110. Given the code above, which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  111. 111. 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;
 }
 }
 } @gamussa #PhillyETE
  112. 112. Juggling Accident
  113. 113. Juggling Accident
  114. 114. What’s correct? @gamussa #PhillyETE
  115. 115. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly @gamussa #PhillyETE
  116. 116. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module, you have to declare it in module-info in order to use @gamussa #PhillyETE
  117. 117. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module, you have to declare it in module-info in order to use C. Once you added the module-info to your project you have to declare the dependencies twice, in classpath and in module-info @gamussa #PhillyETE
  118. 118. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module, you have to declare it in module-info in order to use C. 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 @gamussa #PhillyETE
  119. 119. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module, you have to declare it in module-info in order to use C. 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 @gamussa #PhillyETE
  120. 120. What’s correct? A. If you convert your application to module, classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module, you have to declare it in module-info in order to use C. 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 @gamussa #PhillyETE
  121. 121. @gamussa #PhillyETE
  122. 122. @gamussa #PhillyETE
  123. 123. 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);
 }

  124. 124. A. You killed them all 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);
 }

  125. 125. A. You killed them all B. You killed only even ones 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);
 }

  126. 126. A. You killed them all B. You killed only even ones C. They all survived 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);
 }

  127. 127. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones 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);
 }

  128. 128. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones E. 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);
 }

  129. 129. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones E. 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);
 }

  130. 130. @gamussa #PhillyETE
  131. 131. @gamussa #PhillyETE
  132. 132. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones E. 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);
 }

  133. 133. Don’t do that. Really, don’t. @gamussa #PhillyETE
  134. 134. Don’t do that. Really, don’t. killThemAll(new ArrayList<String>(Arrays.asList("N","S","W","S","L","S","L","V")));
 [] @gamussa #PhillyETE
  135. 135. 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"))); @gamussa #PhillyETE
  136. 136. 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] @gamussa #PhillyETE
  137. 137. 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] @gamussa #PhillyETE
  138. 138. 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] @gamussa #PhillyETE
  139. 139. 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"))); @gamussa #PhillyETE
  140. 140. 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] @gamussa #PhillyETE
  141. 141. Subtle Difference @gamussa #PhillyETE
  142. 142. @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  143. 143. A. Both work just fine @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  144. 144. A. Both work just fine B. Lambda works, method ref fails @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  145. 145. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  146. 146. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails D. Won’t compile @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  147. 147. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails D. Won’t compile @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object> lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  148. 148. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails D. Not a functional interface, will fail on annotation processing @FunctionalInterface public interface CopyCatPredicate {
 <T> boolean test(T t);
 } CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
 CopyCatPredicate methodRef = "adadas"::equals;

  149. 149. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails D. Not a functional interface, will fail on annotation processing @FunctionalInterface public interface CopyCatPredicate {
 <T> boolean test(T t);
 } CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
 CopyCatPredicate methodRef = "adadas"::equals;

  150. 150. A. Both work just fine B. Lambda works, method ref fails C. Method ref works, lambda fails D. Not a functional interface, will fail on annotation processing @FunctionalInterface public interface CopyCatPredicate {
 <T> boolean test(T t);
 } CopyCatPredicate lambda = (Object obj) -> "adadas".equals(obj);
 CopyCatPredicate methodRef = "adadas"::equals;

  151. 151. @gamussa #PhillyETE
  152. 152. A generic function type for a functional interface may be implemented by a method reference expression (§15.13), but not by a lambda expression (§15.27) as there is no syntax for generic lambda expressions. “ @gamussa #PhillyETE
  153. 153. List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()
 .filter(new TreeSet<>()::add).collect(Collectors.toList());
 System.out.println(list);
 @gamussa #PhillyETE
  154. 154. A.[Data, Kirk, Spock] List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()
 .filter(new TreeSet<>()::add).collect(Collectors.toList());
 System.out.println(list);
 @gamussa #PhillyETE
  155. 155. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()
 .filter(new TreeSet<>()::add).collect(Collectors.toList());
 System.out.println(list);
 @gamussa #PhillyETE
  156. 156. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] C.[Spock, Kirk, Data] List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()
 .filter(new TreeSet<>()::add).collect(Collectors.toList());
 System.out.println(list);
 @gamussa #PhillyETE
  157. 157. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] C.[Spock, Kirk, Data] D.[Data, Data, Kirk, Kirk, Spock, Spock] List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential()
 .filter(new TreeSet<>()::add).collect(Collectors.toList());
 System.out.println(list);
 @gamussa #PhillyETE
  158. 158. 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);
 @gamussa #PhillyETE
  159. 159. 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);
 @gamussa #PhillyETE
  160. 160. 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);
 @gamussa #PhillyETE
  161. 161. 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);
 @gamussa #PhillyETE
  162. 162. @gamussa #PhillyETE
  163. 163. filter(new TreeSet<>()::add) @gamussa #PhillyETE
  164. 164. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i)) @gamussa #PhillyETE
  165. 165. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!= @gamussa #PhillyETE
  166. 166. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!= New instance is created every time! @gamussa #PhillyETE
  167. 167. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!= New instance is created every time! Instance method is created once! @gamussa #PhillyETE
  168. 168. @gamussa #PhillyETE
  169. 169. Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  170. 170. A. obvious / obvious Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  171. 171. A. obvious / obvious B. obvious / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  172. 172. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  173. 173. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  174. 174. @gamussa #PhillyETE
  175. 175. @gamussa #PhillyETE
  176. 176. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  177. 177. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); Will never happen @gamussa #PhillyETE
  178. 178. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); Will never happen Will never happen @gamussa #PhillyETE
  179. 179. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); Will never happen Will never happen @gamussa #PhillyETE
  180. 180. @gamussa #PhillyETE
  181. 181. @gamussa #PhillyETE
  182. 182. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  183. 183. @gamussa #PhillyETE
  184. 184. @gamussa #PhillyETE
  185. 185. @gamussa #PhillyETE
  186. 186. @gamussa #PhillyETE
  187. 187. Identical? @gamussa #PhillyETE
  188. 188. When agentA == agentB?
  189. 189. 1. Consumer<String> agentA = s -> System.out.println(s);
 Consumer<String> agentB = s -> System.out.println(s); When agentA == agentB?
  190. 190. 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; When agentA == agentB?
  191. 191. 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(); When agentA == agentB?
  192. 192. 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?
  193. 193. A.All 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?
  194. 194. A.All B.3 and 4 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?
  195. 195. A.All B.3 and 4 C.Only 3 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?
  196. 196. A.All B.3 and 4 C.Only 3 D.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?
  197. 197. A.All B.3 and 4 C.Only 3 D.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?
  198. 198. @gamussa #PhillyETE
  199. 199. @gamussa #PhillyETE
  200. 200. A.All B.3 and 4 C.Only 3 D.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?
  201. 201. Reuse is only possible for pure functions @gamussa #PhillyETE
  202. 202. Reuse is only possible for pure functions Consumers accept parameters == have state @gamussa #PhillyETE
  203. 203. Reuse is only possible for pure functions Consumers accept parameters == have state Supplier in 4 has state – the resolved method reference @gamussa #PhillyETE
  204. 204. Conclusions @gamussa #PhillyETE
  205. 205. -Write readable code!
  206. 206. -Write readable code! -Comment all the tricks
  207. 207. -Write readable code! -Comment all the tricks -Sometimes it’s just a bug
  208. 208. -Write readable code! -Comment all the tricks -Sometimes it’s just a bug -Static code analysis FTW - IntelliJ IDEA!
  209. 209. -Write readable code! -Comment all the tricks -Sometimes it’s just a bug -Static code analysis FTW - IntelliJ IDEA! -RTFM!
  210. 210. -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!
  211. 211. -Trust us, we have much more where those came from.
  212. 212. -Trust us, we have much more where those came from. -Puzzlers? Gotchas? Fetal position inducing behavior?
  213. 213. -Trust us, we have much more where those came from. -Puzzlers? Gotchas? Fetal position inducing behavior? -puzzlers@jfrog.com
  214. 214. -Did you like it?
  215. 215. -Did you like it? -Praise me on twitter!
  216. 216. -Did you like it? -Praise me on twitter! -#javapuzzlersng #PhillyETE
  217. 217. -Did you like it? -Praise me on twitter! -#javapuzzlersng #PhillyETE -@gamussa
  218. 218. -Did you like it? -Praise me on twitter! -#javapuzzlersng #PhillyETE -@gamussa -Didn’t like it?
  219. 219. -Did you like it? -Praise me on twitter! -#javapuzzlersng #PhillyETE -@gamussa -Didn’t like it? -/dev/null

×