Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
Developer Advocate @JFrog
@jbaruch on the internetz
Developer Advocate @Hazelcast
@gAmUssA on the internetz
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
@tagir_valeev
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
1. Two entertaining guys on
the stage
2. Funny puzzling questions
3. You think and vote
4.T-shirts are airborne
5. Official twitter hashtags:
#javapuzzlersng
#DevoxxUS
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
Which Java version are you on?
A. Java 7
B. Java 8
C. Java 9
D. Java 6
E. Java 5
F. Java 2
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Watching the puzzlers like… #dafaq
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Everything works (or doesn't)
in the latest Java 8 and/or 9 update
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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());
}
}
Available
-42?!
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Available
-42?!
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. true/true
B. true/false
C. false/true
D. false/false
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. true/true
B. true/false
C. false/true
D. false/false
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
Singleton Strikes Back!
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Even empty Stream has state!
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
”Identical”
1. Has the same state
2. Not related to “equals and hashcode”contract
3. Not related to references to objects in memory
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
How single is a Single Abstract Method Interface?
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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> { }
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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> { }
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
Hacking the bank
☑Bank software written in Java
☑Hack into it
☑Analyze the accounts
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Your turn,FBI
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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	=	Set.of("Gates",	"Buffett",	"Bezos",	"Zuckerberg");
System.out.println(”accounts=	"	+	accounts);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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	=	Set.of("Gates",	"Buffett",	"Bezos",	"Zuckerberg");
System.out.println(”accounts=	"	+	accounts);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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;
}
}
}
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Juggling Accident
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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);
}
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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);
}
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]
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Subtle Difference
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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;
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;
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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;
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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;
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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.	“
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017
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);
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!=
New	instance	is	
created	every	time!
Instance	method	is	
created	once!
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Identical?
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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?
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
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?
Reuse is only possible for pure functions
Consumers accept parameters == have
state
Supplier in 4 has state –the resolved
method reference
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
Conclusions
@jbaruch										@gamussa #DevoxxUS http://jfrog.com/shownotes
-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!
-Trust us, we have much
more where those came
from.
-Puzzlers? Gotchas? Fetal
position inducing behavior?
-puzzlers@jfrog.com
-Shownotes!
-http://jfrog.com/shownotes
-Slides
-Video in 24 hours
-Links
-Ratings
-Raffle! (come early)
-Did you like it?
-Praise us on twitter!
-#javapuzzlersng #DevoxxUS
-@gamussa
-@jbaruch
-Didn’t like it?
-/dev/null

More Related Content

PDF
Java Puzzlers NG S02: Down the Rabbit Hole as presented at DevNexus 2017
PDF
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsbu...
PDF
Java 8 Puzzlers as it was presented at Codemash 2017
PDF
Java Puzzlers NG as it was presented at Detroit Java User Group
PPTX
Make Your Own Damn SEO Tools (Using Google Docs!)
PDF
Trying to learn C# (NDC Oslo 2019)
PDF
Reading Other Peoples Code (NDC London 2019)
Java Puzzlers NG S02: Down the Rabbit Hole as presented at DevNexus 2017
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsbu...
Java 8 Puzzlers as it was presented at Codemash 2017
Java Puzzlers NG as it was presented at Detroit Java User Group
Make Your Own Damn SEO Tools (Using Google Docs!)
Trying to learn C# (NDC Oslo 2019)
Reading Other Peoples Code (NDC London 2019)

What's hot (7)

PDF
Reading Other Peoples Code (NDC Copenhagen 2019)
PDF
Reading Other Peoples Code (NDC Sydney 2018)
PDF
Geb for browser automation
PDF
Statecharts - Controlling the behavior of complex systems
PDF
Are statecharts the next big UI paradigm?
PDF
3 Reasons that Contribute to the Most Common AC Problems
PDF
C++ The Principles of Most Surprise
Reading Other Peoples Code (NDC Copenhagen 2019)
Reading Other Peoples Code (NDC Sydney 2018)
Geb for browser automation
Statecharts - Controlling the behavior of complex systems
Are statecharts the next big UI paradigm?
3 Reasons that Contribute to the Most Common AC Problems
C++ The Principles of Most Surprise
Ad

Viewers also liked (20)

PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code Au...
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code SF...
KEY
Basic Wicket and Scala
PPTX
Presentation: Everything you wanted to know about writing async, high-concurr...
PDF
Prg java l
PDF
Best Way to Write SQL in Java
KEY
Functional UI testing of Adobe Flex RIA
PDF
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
PDF
JavaOne 2013: «Java and JavaScript - Shaken, Not Stirred»
PPTX
Creating your own private Download Center with Bintray
PPTX
Java 8 Puzzlers [as presented at OSCON 2016]
PPTX
Spring Data: New approach to persistence
KEY
Testing Flex RIAs for NJ Flex user group
PPTX
Confession of an Engineer
PPTX
Morning at Lohika 2nd anniversary
PDF
Couchbase Sydney meetup #1 Couchbase Architecture and Scalability
PDF
Patterns and antipatterns in Docker image lifecycle @ DevOpsDays Charlotte 2017
PPTX
Javaeeconf 2016 how to cook apache kafka with camel and spring boot
PDF
Patterns and antipatterns in Docker image lifecycle as was presented at Oracl...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code Au...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code SF...
Basic Wicket and Scala
Presentation: Everything you wanted to know about writing async, high-concurr...
Prg java l
Best Way to Write SQL in Java
Functional UI testing of Adobe Flex RIA
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
JavaOne 2013: «Java and JavaScript - Shaken, Not Stirred»
Creating your own private Download Center with Bintray
Java 8 Puzzlers [as presented at OSCON 2016]
Spring Data: New approach to persistence
Testing Flex RIAs for NJ Flex user group
Confession of an Engineer
Morning at Lohika 2nd anniversary
Couchbase Sydney meetup #1 Couchbase Architecture and Scalability
Patterns and antipatterns in Docker image lifecycle @ DevOpsDays Charlotte 2017
Javaeeconf 2016 how to cook apache kafka with camel and spring boot
Patterns and antipatterns in Docker image lifecycle as was presented at Oracl...
Ad

Similar to Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017 (20)

DOCX
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
PDF
1z0 851 exam-java standard edition 6 programmer certified professional
TXT
CORE JAVA
PDF
Java Puzzlers NG S03 a DevNexus 2018
PPT
Core java by a introduction sandesh sharma
DOC
MX Server is my friend
DOC
MX Server is my friend
DOC
MX Server is my friend
DOC
MX Server is my friend
PPTX
131 Lab slides (all in one)
DOCX
Java Questions
PDF
Big Java Early Objects 5th Edition Horstmann Solutions Manual
PPTX
Language fundamentals ocjp
PPTX
AP Computer Science AP Review 2011 FRQ Questions
PPTX
More topics on Java
PDF
The Sincerest Form of Flattery
DOCX
Simulado java se 7 programmer
DOCX
Jist of Java
DOCX
JAVA CONCEPTS AND PRACTICES
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
1z0 851 exam-java standard edition 6 programmer certified professional
CORE JAVA
Java Puzzlers NG S03 a DevNexus 2018
Core java by a introduction sandesh sharma
MX Server is my friend
MX Server is my friend
MX Server is my friend
MX Server is my friend
131 Lab slides (all in one)
Java Questions
Big Java Early Objects 5th Edition Horstmann Solutions Manual
Language fundamentals ocjp
AP Computer Science AP Review 2011 FRQ Questions
More topics on Java
The Sincerest Form of Flattery
Simulado java se 7 programmer
Jist of Java
JAVA CONCEPTS AND PRACTICES

More from Baruch Sadogursky (20)

PDF
DevOps Patterns & Antipatterns for Continuous Software Updates @ NADOG April ...
PDF
DevOps Patterns & Antipatterns for Continuous Software Updates @ DevOps.com A...
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code NY...
PDF
Data driven devops as presented at QCon London 2018
PDF
A Research Study Into DevOps Bottlenecks as presented at Oracle Code LA 2018
PDF
Where the Helm are your binaries? as presented at Canada Kubernetes Meetups
PDF
Data driven devops as presented at Codemash 2018
PDF
A Research Study into DevOps Bottlenecks as presented at Codemash 2018
PPTX
Best Practices for Managing Docker Versions as presented at JavaOne 2017
PDF
Troubleshooting & Debugging Production Microservices in Kubernetes as present...
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Devoxx 2017
PPTX
Amazon Alexa Skills vs Google Home Actions, the Big Java VUI Faceoff as prese...
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at DevOps Days Be...
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at The Pittsburgh...
PDF
Let’s Wing It: A Study in DevRel Strategy
PDF
Log Driven First Class Customer Support at Scale
PPTX
[Webinar] The Frog And The Butler: CI Pipelines For Modern DevOps
PDF
Patterns and antipatterns in Docker image lifecycle as was presented at DC Do...
PDF
Groovy Puzzlers S04: The Bytecode Bites Back at Gr8Conf US 2017
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Gr8Conf 2017
DevOps Patterns & Antipatterns for Continuous Software Updates @ NADOG April ...
DevOps Patterns & Antipatterns for Continuous Software Updates @ DevOps.com A...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code NY...
Data driven devops as presented at QCon London 2018
A Research Study Into DevOps Bottlenecks as presented at Oracle Code LA 2018
Where the Helm are your binaries? as presented at Canada Kubernetes Meetups
Data driven devops as presented at Codemash 2018
A Research Study into DevOps Bottlenecks as presented at Codemash 2018
Best Practices for Managing Docker Versions as presented at JavaOne 2017
Troubleshooting & Debugging Production Microservices in Kubernetes as present...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Devoxx 2017
Amazon Alexa Skills vs Google Home Actions, the Big Java VUI Faceoff as prese...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at DevOps Days Be...
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at The Pittsburgh...
Let’s Wing It: A Study in DevRel Strategy
Log Driven First Class Customer Support at Scale
[Webinar] The Frog And The Butler: CI Pipelines For Modern DevOps
Patterns and antipatterns in Docker image lifecycle as was presented at DC Do...
Groovy Puzzlers S04: The Bytecode Bites Back at Gr8Conf US 2017
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Gr8Conf 2017

Recently uploaded (20)

PDF
Hybrid model detection and classification of lung cancer
PDF
CloudStack 4.21: First Look Webinar slides
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
August Patch Tuesday
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
The various Industrial Revolutions .pptx
PDF
Architecture types and enterprise applications.pdf
DOCX
search engine optimization ppt fir known well about this
PPT
What is a Computer? Input Devices /output devices
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Enhancing emotion recognition model for a student engagement use case through...
Hybrid model detection and classification of lung cancer
CloudStack 4.21: First Look Webinar slides
NewMind AI Weekly Chronicles – August ’25 Week III
Getting started with AI Agents and Multi-Agent Systems
August Patch Tuesday
A contest of sentiment analysis: k-nearest neighbor versus neural network
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
A comparative study of natural language inference in Swahili using monolingua...
Hindi spoken digit analysis for native and non-native speakers
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
sustainability-14-14877-v2.pddhzftheheeeee
The various Industrial Revolutions .pptx
Architecture types and enterprise applications.pdf
search engine optimization ppt fir known well about this
What is a Computer? Input Devices /output devices
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
WOOl fibre morphology and structure.pdf for textiles
A review of recent deep learning applications in wood surface defect identifi...
Tartificialntelligence_presentation.pptx
Enhancing emotion recognition model for a student engagement use case through...

Java Puzzlers NG S02: Down the Rabbit Hole as presented at Devoxx US 2017

  • 3. Developer Advocate @JFrog @jbaruch on the internetz Developer Advocate @Hazelcast @gAmUssA on the internetz
  • 9. 1. Two entertaining guys on the stage 2. Funny puzzling questions 3. You think and vote 4.T-shirts are airborne 5. Official twitter hashtags: #javapuzzlersng #DevoxxUS
  • 11. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 E. Java 5 F. Java 2 @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 12. Watching the puzzlers like… #dafaq @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 13. Everything works (or doesn't) in the latest Java 8 and/or 9 update @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 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
  • 18. 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. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() == Collections.emptyList(); Collections.emptyIterator() == Collections.emptyIterator(); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 23. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() == Collections.emptyList(); Collections.emptyIterator() == Collections.emptyIterator(); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 25. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator(); Stream.empty() == Stream.empty();
  • 26. Singleton Strikes Back! @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 27. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator(); Stream.empty() == Stream.empty(); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 28. Even empty Stream has state! @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 30. ”Identical” 1. Has the same state 2. Not related to “equals and hashcode”contract 3. Not related to references to objects in memory @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 31. 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 @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 33. 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 @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 35. How single is a Single Abstract Method Interface? @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 36. 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> { } @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 38. 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> { } @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 41. Hacking the bank ☑Bank software written in Java ☑Hack into it ☑Analyze the accounts @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 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); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 44. 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); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 45. public boolean add(E e) { return map.put(e, PRESENT)==null; } @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 46. Your turn,FBI @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 48. 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 = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg"); System.out.println(”accounts= " + accounts); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 51. 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 = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg"); System.out.println(”accounts= " + accounts); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 52. 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; } } } @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 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 D. None of the above @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 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 @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 59. 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); }
  • 61. 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); }
  • 62. 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] @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 63. Subtle Difference @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 64. 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;
  • 65. 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;
  • 67. 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;
  • 69. 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;
  • 73. 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); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 75. 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); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 77. 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); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 78. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!= New instance is created every time! Instance method is created once! @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 80. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null); Optional.empty().map(null).orElse("obvious"); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 82. 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 @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 84. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null); Optional.empty().map(null).orElse("obvious"); @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 87. 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?
  • 89. 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?
  • 90. Reuse is only possible for pure functions Consumers accept parameters == have state Supplier in 4 has state –the resolved method reference @jbaruch @gamussa #DevoxxUS http://jfrog.com/shownotes
  • 92. -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!
  • 93. -Trust us, we have much more where those came from. -Puzzlers? Gotchas? Fetal position inducing behavior? -puzzlers@jfrog.com
  • 95. -Did you like it? -Praise us on twitter! -#javapuzzlersng #DevoxxUS -@gamussa -@jbaruch -Didn’t like it? -/dev/null