[Philly ETE] Java Puzzlers NG

[Philly ETE] Java Puzzlers NG

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!

0680be1c881abcf19219f09f1e8cf140?s=128

Viktor Gamov

April 18, 2017
Tweet

Transcript

  1. 8.

    @gAmUssA @hazelcast #jbreak #hazelcastjet Solutions Architect Developer Advocate @gamussa in

    internetz Please, follow me on Twitter I’m very interesting © > whoami
  2. 9.
  3. 10.
  4. 17.
  5. 20.

    1. Two One entertaining guy on the stage 2. Funny

    puzzling questions 3. You think and vote
  6. 21.

    1. Two One entertaining guy on the stage 2. Funny

    puzzling questions 3. You think and vote 4.Official twitter hashtags #javapuzzlersng #PhillyETE
  7. 25.

    Which Java version are you on? A. Java 7 B.

    Java 8 @gamussa #PhillyETE
  8. 26.

    Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 @gamussa #PhillyETE
  9. 27.

    Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 D. Java 6 @gamussa #PhillyETE
  10. 28.

    Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 D. Java 6 E. Java 5 @gamussa #PhillyETE
  11. 29.

    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
  12. 30.

    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
  13. 33.

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

    and/or 9 update @gamussa #PhillyETE
  14. 34.
  15. 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());
 }
 }

  16. 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
  17. 38.

    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
  18. 39.

    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
  19. 40.

    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
  20. 41.

    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
  21. 45.

    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());
 }
 }

  22. 56.

    A. true/true B. true/false C. false/true D. false/false Collections.emptyList() ==

    Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  23. 57.

    A. true/true B. true/false C. false/true D. false/false Collections.emptyList() ==

    Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  24. 64.

    A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() ==

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

  25. 65.

    A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() ==

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

  26. 68.

    A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() ==

    Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();
 @gamussa #PhillyETE
  27. 72.
  28. 73.

    ”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
  29. 76.

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

    empty lists B. Absolutely identical non-empty lists @gamussa #PhillyETE
  30. 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 @gamussa #PhillyETE
  31. 78.

    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
  32. 79.

    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
  33. 82.

    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
  34. 87.

    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
  35. 88.

    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
  36. 89.

    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
  37. 90.

    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
  38. 91.

    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
  39. 92.

    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
  40. 93.
  41. 94.
  42. 95.

    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
  43. 98.

    Hacking the bank ☑Bank software written in Java ☑Hack into

    it ☑Analyze the accounts @gamussa #PhillyETE
  44. 99.

    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
  45. 100.

    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
  46. 101.

    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
  47. 102.

    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
  48. 103.

    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
  49. 104.

    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
  50. 105.
  51. 106.
  52. 107.

    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
  53. 110.
  54. 111.

    Given the code above, which statement is wrong: Set<String> accounts

    = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  55. 112.

    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
  56. 113.

    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
  57. 114.

    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
  58. 115.

    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
  59. 116.

    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
  60. 120.

    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
  61. 121.

    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
  62. 125.

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

    classpath dependencies will still be resolved correctly @gamussa #PhillyETE
  63. 126.

    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
  64. 127.

    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
  65. 128.

    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
  66. 129.

    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
  67. 130.
  68. 131.
  69. 132.

    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
  70. 135.
  71. 136.

    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);
 }

  72. 137.

    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);
 }

  73. 138.

    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);
 }

  74. 139.

    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);
 }

  75. 140.

    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);
 }

  76. 141.

    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);
 }

  77. 142.

    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);
 }

  78. 145.

    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);
 }

  79. 150.
  80. 151.
  81. 152.

    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
  82. 153.

    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
  83. 155.

    @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object>

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

    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;
  85. 157.

    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;
  86. 158.

    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;
  87. 159.

    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;
  88. 160.

    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;
  89. 161.
  90. 162.
  91. 163.

    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;

  92. 164.

    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;

  93. 165.
  94. 166.
  95. 167.
  96. 168.

    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;

  97. 170.

    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
  98. 171.
  99. 172.

    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
  100. 173.

    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
  101. 174.

    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
  102. 175.

    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
  103. 176.

    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
  104. 177.

    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
  105. 178.
  106. 179.
  107. 180.

    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
  108. 181.

    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
  109. 182.
  110. 183.
  111. 184.

    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
  112. 190.

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

    created every time! Instance method is created once! @gamussa #PhillyETE
  113. 195.

    A. obvious / obvious B. obvious / NullPointerException C. NullPointerException

    / obvious Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  114. 196.

    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
  115. 199.

    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
  116. 200.

    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
  117. 201.

    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
  118. 202.

    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
  119. 205.

    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
  120. 212.
  121. 213.

    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?
  122. 214.

    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?
  123. 215.

    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?
  124. 216.

    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?
  125. 217.

    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?
  126. 218.

    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?
  127. 219.

    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?
  128. 220.

    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?
  129. 223.

    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?
  130. 226.

    Reuse is only possible for pure functions Consumers accept parameters

    == have state Supplier in 4 has state – the resolved method reference @gamussa #PhillyETE
  131. 228.
  132. 232.

    -Write readable code! -Comment all the tricks -Sometimes it’s just

    a bug -Static code analysis FTW - IntelliJ IDEA!
  133. 233.

    -Write readable code! -Comment all the tricks -Sometimes it’s just

    a bug -Static code analysis FTW - IntelliJ IDEA! -RTFM!
  134. 234.

    -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!
  135. 235.
  136. 237.

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

    -Puzzlers? Gotchas? Fetal position inducing behavior?
  137. 238.

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

    -Puzzlers? Gotchas? Fetal position inducing behavior? -puzzlers@jfrog.com
  138. 239.
  139. 245.