Here are a few ways to cast to a type without declaring it:1. Use a generic type parameter:<T> T cast(Object o) { return (T) o;}2. Use a raw type: Object cast(Object o) { return (Object) o; }3. Reflection:Object cast(Object o, Class<?> type) throws Exception { return type.cast(o);}4. Dynamic proxy: interface Castable { Object cast(Object o);}class CastProxy implements Castable { private Class<?> type; public CastProxy(Class
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. Baruch and Viktor will show you some surprising, bizarre, and inconceivable parts of Java 8 and make sure that you won’t be (too) surprised when it hits you in production. As with any other puzzlers talk, there will be two speakers, lots of puzzling questions, answers by the audience, T-shirts flying around, and a lot of fun, all for one purpose—to help you better understand Java 8.
Couchbase Sydney meetup #1 Couchbase Architecture and Scalability
Similar a Here are a few ways to cast to a type without declaring it:1. Use a generic type parameter:<T> T cast(Object o) { return (T) o;}2. Use a raw type: Object cast(Object o) { return (Object) o; }3. Reflection:Object cast(Object o, Class<?> type) throws Exception { return type.cast(o);}4. Dynamic proxy: interface Castable { Object cast(Object o);}class CastProxy implements Castable { private Class<?> type; public CastProxy(Class
Seven Peaks Speaks - Compose Screenshot Testing Made EasySeven Peaks Speaks
Similar a Here are a few ways to cast to a type without declaring it:1. Use a generic type parameter:<T> T cast(Object o) { return (T) o;}2. Use a raw type: Object cast(Object o) { return (Object) o; }3. Reflection:Object cast(Object o, Class<?> type) throws Exception { return type.cast(o);}4. Dynamic proxy: interface Castable { Object cast(Object o);}class CastProxy implements Castable { private Class<?> type; public CastProxy(Class (20)
Here are a few ways to cast to a type without declaring it:1. Use a generic type parameter:<T> T cast(Object o) { return (T) o;}2. Use a raw type: Object cast(Object o) { return (Object) o; }3. Reflection:Object cast(Object o, Class<?> type) throws Exception { return type.cast(o);}4. Dynamic proxy: interface Castable { Object cast(Object o);}class CastProxy implements Castable { private Class<?> type; public CastProxy(Class
7. 1. Two entertaining guys on
stage
2. Funny Puzzling questions
3. You think and vote
4. Official twitter handles:
JAVApuzzlersng
codemash
5. Jfrog.com/shownotes
13. What will be the output?
A.milk/bread/sausage
B.milk/bread/sausage/eggs,don’t forget eggs!
C.milk/bread/sausage/ConcurrentModificationException
D.ConcurrentModificationException
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);
22. 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. 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
}
25. 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;
}
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
27. 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()
);
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
33. Let’s upgrade the stack!
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
34. 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);
40. 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
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
42. 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
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
44. 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
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
57. Viktor Gamov and Baruch Sadogursky call customer
service:
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
58. 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. 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());
}
}
65. 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);
}
});
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
66. 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);
@jbaruch @gamussa #javapuzzlersng #codemash http://jfrog.com/shownotes
76. - Write readable code!
- Comment all the tricks
- Sometimes it’s a bug
- Static code analysis FTW -
intellij IDEA!
- Rtfm
- Don’t abuse lambdas and
streams!
77. - Trust us, we have much
more where those came
from.
- Puzzlers? Gotchas? Fetal
position inducing behavior?
- puzzlers jfrog.com
78. Did you like it?
Praise us on twitter and in the
feedback form!
- javapuzzlersng
- gamussa
- Jbaruch
-http://jfrog.com/shownotes
Didn’t like it?
/dev/null