Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[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!

Viktor Gamov

April 18, 2017
Tweet

More Decks by Viktor Gamov

Other Decks in Programming

Transcript

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

    internetz Please, follow me on Twitter I’m very interesting © > whoami
  2. 1. Two One entertaining guy on the stage 2. Funny

    puzzling questions 3. You think and vote
  3. 1. Two One entertaining guy on the stage 2. Funny

    puzzling questions 3. You think and vote 4.Official twitter hashtags #javapuzzlersng #PhillyETE
  4. Which Java version are you on? A. Java 7 B.

    Java 8 @gamussa #PhillyETE
  5. Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 @gamussa #PhillyETE
  6. Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 D. Java 6 @gamussa #PhillyETE
  7. Which Java version are you on? A. Java 7 B.

    Java 8 C. Java 9 D. Java 6 E. Java 5 @gamussa #PhillyETE
  8. 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
  9. 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
  10. Everything works (or doesn't) 
 in the latest Java 8

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

  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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());
 }
 }

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

    Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  19. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() ==

    Collections.emptyList();
 Collections.emptyIterator() == Collections.emptyIterator();
 @gamussa #PhillyETE
  20. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() ==

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

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

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

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

    Spliterators.emptySpliterator();
 Stream.empty() == Stream.empty();
 @gamussa #PhillyETE
  23. ”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
  24. List[] twins = new List[2];
 Arrays.setAll(twins, ArrayList::new);
 A. Absolutely identical

    empty lists B. Absolutely identical non-empty lists @gamussa #PhillyETE
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. Hacking the bank ☑Bank software written in Java ☑Hack into

    it ☑Analyze the accounts @gamussa #PhillyETE
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. Given the code above, which statement is wrong: Set<String> accounts

    = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg");
 System.out.println(”accounts= " + accounts); @gamussa #PhillyETE
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. What’s correct? A. If you convert your application to module,

    classpath dependencies will still be resolved correctly @gamussa #PhillyETE
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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);
 }

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

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

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

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

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

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

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

  66. 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
  67. 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
  68. @FunctionalInterface
 public interface OriginalPredicate<T> {
 boolean test(T t);
 } OriginalPredicate<Object>

    lambda = (Object obj) -> "adidas".equals(obj);
 OriginalPredicate<Object> methodRef = "adidas"::equals;
  69. 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;
  70. 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;
  71. 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;
  72. 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;
  73. 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;
  74. 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;

  75. 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;

  76. 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;

  77. 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
  78. 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
  79. 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
  80. 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
  81. 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
  82. 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
  83. 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
  84. 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
  85. 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
  86. 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
  87. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i)) != New instance is

    created every time! Instance method is created once! @gamussa #PhillyETE
  88. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException

    / obvious Optional.of("obvious").orElseGet(null);
 Optional.empty().map(null).orElse("obvious"); @gamussa #PhillyETE
  89. 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
  90. 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
  91. 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
  92. 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
  93. 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
  94. 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
  95. 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?
  96. 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?
  97. 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?
  98. 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?
  99. 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?
  100. 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?
  101. 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?
  102. 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?
  103. 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?
  104. Reuse is only possible for pure functions Consumers accept parameters

    == have state Supplier in 4 has state – the resolved method reference @gamussa #PhillyETE
  105. -Write readable code! -Comment all the tricks -Sometimes it’s just

    a bug -Static code analysis FTW - IntelliJ IDEA!
  106. -Write readable code! -Comment all the tricks -Sometimes it’s just

    a bug -Static code analysis FTW - IntelliJ IDEA! -RTFM!
  107. -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!
  108. -Trust us, we have much more where those came from.

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

    -Puzzlers? Gotchas? Fetal position inducing behavior? [email protected]