We aren't sure about you, but working with Java 8 made one of the speakers lose all of his hair and the other lose his sleep (or was it the jetlag?). If you still haven't reached the level of Brian Goetz in mastering lambdas and strings, this talk is for you. And if you think you have, we have some bad news for you, you should attend as well.
6. 1. Two entertaining guys on stage
2. Funny Puzzling questions
3. You think and vote
4. Awesome t-shirts fly in the air
5. Official twitter handle!
JAVA8puzzlers
12. What will be the output?
A. milk/bread/sausage
B. milk/bread/sausage/eggs, don’t forget
eggs!
C.
List<String> list = new ArrayList<>();
list.add("milk");
list.add("bread");
list.add("sausage");
Stream<String> stream = list.stream();
list.add("eggs, don’t forget eggs!");
stream.forEach(System.out::println);
13.
14. Late binding, duh…
List<String> list = new ArrayList<>();
list.add("milk");
list.add("bread");
list.add("sausage");
Stream<String> stream = list.stream();
list.add("eggs, don’t forget eggs!");
stream.forEach(System.out::println);
15. Late binding, duh…
List<String> list = new ArrayList<>();
list.add("milk");
list.add("bread");
list.add("sausage");
Stream<String> stream = list.stream();
list.add("eggs, don’t forget eggs!");
stream.forEach(System.out::println);
17. What will be the output?
A. milk/bread/sausage
B. milk/bread/eggs, don’t forget eggs!
C. milk/bread/ConcurrentModificationException
D. ConcurrentModificationException
List<String> list = new ArrayList<>();
list.add("milk");
list.add("bread");
list.add("sausage");
list = list.subList(0, 2); //No sausage, please!
Stream<String> stream = list.stream();
list.add("eggs, don’t forget eggs!");
stream.forEach(System.out::println);
21. What’s the difference between 1 and 2?
A. 1 compiles, 2 does not
B. 2 compiles, 1 does not
C. Same same, both work fine
D. Same same, both won’t compile
public void killAll(){
ExecutorService ex = Executors.newSingleThreadExecutor();
List<String> sentence = Arrays.asList("Punish");
ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1
ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2
}
23. What’s the difference between 1 and 2?
A. 1 compiles, 2 does not
B. 2 compiles, 1 does not
C. Same same, both work fine
D. Same same, both won’t compile
public void killAll(){
ExecutorService ex = Executors.newSingleThreadExecutor();
List<String> sentence = Arrays.asList("Punish");
ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1
ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2
}
24. public void killAll(){
ExecutorService ex = Executors.newSingleThreadExecutor();
List<String> sentence = Arrays.asList("Punish");
ex.submit(() -> Files.write(Paths.get("Sentence.txt"), sentence) ); // 1
ex.submit(() -> { Files.write(Paths.get("Sentence.txt"), sentence); }); // 2
}
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
@FunctionalInterface
public interface Callable<V> {
V call() throws Exception;
}
26. How that will work?
A. Compilation error
B. Runtime Exception
C. 3
D. Something else
System.out.println(
Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get()
);
27.
28. How about now?
A. −3
B. −1
C. 0
D. Something else
System.out.println(
Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get()
);
29.
30. How about now?
A. −3
B. −1
C. 0
D. Something else
System.out.println(
Stream.of(-3, -2, -1, 0, 1, 2, 3).max(Math::max).get()
);
33. What will happen?
A.Maps will switch
B.Both will become oldSchool
C.Both will become hipster
D.Really?! That won’t even compile!
Map<String, String> oldSchool = initOldSchoolStack();
// oldSchool = {buildTool=maven, lang=java, db=db2}
Map<String, String> proper = initHipsterStack();
// proper = {buildTool=npm, lang=javascript, db=elastic}
oldSchool.replaceAll(proper::put);
34.
35. void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
V put(K key, V value);
Map interface
oldSchool.replaceAll(proper::put);
36. void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
V put(K key, V value);
Map interface
final BiFunction<String, String, String> function =
(key, value) -> proper.put(key, value);
for (Map.Entry<String, String> entry : oldSchool.entrySet())
entry.setValue(function.apply(entry.getKey(), entry.getValue()));
37. void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
V put(K key, V value);
Map interface
final BiFunction<String, String, String> function =
(key, value) -> proper.put(key, value);
for (Map.Entry<String, String> entry : oldSchool.entrySet())
entry.setValue(function.apply(entry.getKey(), entry.getValue()));
38.
39. How many lines will be the same?
List<String> kitties = Arrays.asList("Soft", "Warm", "Purr");
Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());
System.out.println(Collections.max(kitties, kittiesComparator));
System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());
System.out.println(kitties.stream().max(kittiesComparator).get());
A.All lines the same
B.Two lines the same
C.All different
D.Four different
40.
41. How about now?
List<String> kitties = Arrays.asList("Soft", null, "Purr");
Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());
System.out.println(Collections.max(kitties, kittiesComparator));
System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());
System.out.println(kitties.stream().max(kittiesComparator).get());
A.All lines the same
B.Two lines the same
C.All different
D.Four different
42.
43. How about now?
List<String> kitties = Arrays.asList("Soft", null, "Purr");
Comparator<String> kittiesComparator= Comparator.nullsLast(Comparator.naturalOrder());
System.out.println(Collections.max(kitties, kittiesComparator));
System.out.println(kitties.stream().collect(Collectors.maxBy(kittiesComparator)).get());
System.out.println(kitties.stream().max(kittiesComparator).get());
A.All lines the same
B.Two lines the same
C.All different
D.Four different
49. How to cast to a type without declaring it?
interface Cat{ default void meow() {System.out.println(”meow ");}}
interface Dog{ default void bark() {System.out.println(”woof ");}}
public static void main(String[] args) {
class Dogcatimplements Dog, Cat{}
test(new Dogcat());
}
static void test(Object obj) {
def x = (?)obj;
x.meow ();
x.bark ();
}
50. How to cast to a type without declaring it?
static void test(Object obj) {
// A. Will that work?
Dog& Catx = (Dog& Cat) obj;
x.meow ();
x.bark ();
}
static void test(Object obj) {
// B. Will that work?
((Consumer<? extends Dog& Cat>)(x -> {
x.meow ();
x.bark ();
})).accept((Dog& Cat)obj); }
static void test(Object obj) {
// C. Will that work?
Optional.of((Dog& Cat) obj)
.ifPresent(x -> {
x.meow ();
x.bark ();
});}
// D. You’re two sick bastards.
interface Cat{ default void meow() {System.out.println(”meow");}}
interface Dog{ default void bark() {System.out.println(”woof");}}
public static void main(String[] args) {
class Dogcat implements Dog, Cat{}
test(new Dogcat());
}
51.
52. How to cast to a type without declaring it?
static void test(Object obj) {
// A. Will that work?
Dog & Cat x = (Dog & Cat) obj;
x.meow();
x.bark();
}
static void test(Object obj) {
// B. Will that work?
((Consumer<? extends Dog & Cat>)(x -> {
x.meow();
x.bark();
})).accept((Dog & Cat)obj); }
static void test(Object obj) {
// C. Will that work?
Optional.of((Dog & Cat) obj)
.ifPresent(x -> {
x.meow();
x.bark();
});}
// D. You’re two sick bastards.
interface Cat{ default void meow() {System.out.println(”meow");}}
interface Dog{ default void bark() {System.out.println(”woof");}}
public static void main(String[] args) {
static class Dogcat implements Dog, Cat{}
test(new Dogcat());
}
57. What will be the output?
1. HOTEL ECHO LIMA LIMA OSCAR/ HOTEL ECHO LIMA LIMA
OSCAR
2. HELLO / HOTEL ECHO LIMA LIMA OSCAR
3. HOTEL ECHO LIMA LIMA OSCAR/ HELLO
4. HELLO/HELLO
public class Test {
String str;
void run() {
str = "hello ";
Supplier<String> s1 = str::toUpperCase;
Supplier<String> s2 = () -> str.toUpperCase();
str = "Hotel Echo Lima Lima Oscar ";
System.out.println(s1.get());
System.out.println(s2.get());
}
}
58.
59. What will be the output?
1. HOTEL ECHO LIMA LIMA OSCAR/ HOTEL ECHO LIMA LIMA
OSCAR
2. HELLO / HOTEL ECHO LIMA LIMA OSCAR
3. HOTEL ECHO LIMA LIMA OSCAR/ HELLO
4. HELLO/HELLO
public class Test {
String str;
void run() {
str = ”hello";
Supplier<String> s1 = str::toUpperCase;
Supplier<String> s2 = () -> str.toUpperCase();
str = ”Hotel Echo Lima Lima Oscar";
System.out.println(s1.get());
System.out.println(s2.get());
}
}
60.
61. What will happen?
1. ConcurrentModificationException
2. ArrayIndexOutOfBoundsException
3. NullPointerException
4. No exceptions, all good
List<String> list = new ArrayList<>(Arrays.asList("Arnie", "Chuck", "Slay"));
list.stream().forEach(x -> {
if(x.equals("Chuck")) {
list.remove(x);
}
});
64. What will happen?
A. ConcurrentModificationException
B. ArrayIndexOutOfBoundsException
C. NullPointerException
D. No exceptions, all good
List<String> list = new ArrayList<>(Arrays.asList("Arnie", "Chuck", "Slay"));
list.stream().forEach(x -> {
if(x.equals("Chuck")) {
list.remove(x);
}
});
65. Here’s why:
stream().forEach() spliterator().forEachRemaining()
forEachRemaining checks for mod count once, in the end
Removing element adds null to the end of the array:
["Arne", "Chuck", "Slay"] ["Arne", "Slay", null]
On the last iteration if(null.equals("Chuck")) fails with NPE
(didn’t get to CME)
Use list.removeIf("Chuck"::equals);