SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Java 8 Hands on Workshop
FIRST Robotics Training
July 12, 2017
Jeanne Boyarsky
Twitter @jeanneboyarsky
Blog: http://www.selikoff.net
Disclaimer: Some of this material is copyright of Jeanne
Boyarsky, Scott Selikoff and/or Wiley Publishing
Twitter: @jeanneboyarsky
This PowerPoint is part of a hands
on workshop for teaching fluency
with Java 8 stream basics.
Twitter: @jeanneboyarsky
Pre-req:
Basic Java knowledge is required. If you can
implement this method using a loop, you
are good:
public int countStringsThatAreFourChars(
List<String> list) {
}
Groovy vs Java
Twitter: @jeanneboyarsky
print(
list
.grep{ j -> j.disabled }
.collect { j-> j.name }
)
list.stream()
.filter(j -> j.disabled)
.map(j -> j.name)
.forEach(
System.out::println);
Twitter: @jeanneboyarsky
TryYourIde.java
(5 mins)
• Local IDE
or
• https://www.compilejava.net
https://github.com/boyarsky/java-8-
streams-by-puzzles
Run src/TryYourIde class
Prints Do stuff and Do more stuff
Twitter: @jeanneboyarsky
Lambda vs Boilerplate
filter(j -> ! j.disabled)
filter(new Predicate<Job>() {
public boolean test(Job j) {
return ! j.disabled;
}
})
Twitter: @jeanneboyarsky
Basic Lambdas
Twitter: @jeanneboyarsky
Full Lambda Syntax
Java 8 Lambda Syntax Rules
Must have parens
unless exactly one
parameter
If listing type, must
be consistent
If multiple
statements: requires
braces, return and
semicolon
Twitter: @jeanneboyarsky
• () -> true
• a -> a.startsWith("test")
• (String a) ->
a.startsWith("test")
• (a, b) ->
a.startsWith("test")
• (String a, String b) ->
{ return
a.startsWith("test"); }
What’s wrong?
Twitter: @jeanneboyarsky
• a, b -> a.startsWith("test")
• a -> { a.startsWith("test"); }
• a -> { return a.startsWith("test") }
• (String a, b) -> a.startsWith("test")
• -> a.startsWith("test”)
Using variables
interface Gorilla { String move(); }
class GorillaFamily {
String walk = "walk";
void everyonePlay(boolean baby) {
String approach = "amble";
//approach = "run";
play(() -> walk);
play(() -> baby ? "hitch a ride" : "run");
play(() -> approach);
}
void play(Gorilla g) {
g.move();
}
}
1) Instance var
2) Class var
3) Effectively final
method param
or local var
Twitter: @jeanneboyarsky
LambdaFillInTheBlanks.java
(10-15 mins)
Easy - Fill in the blank so prints
out the numbers less than 3:
Stream.of(1, 2, 3, 5, 8)
.filter(__________)
.forEach(System.out::println);
Easy - Fill in the blank so prints out the
animals that are 4 characters in length:
Stream.of("cat", "dot", "fish", "rabbit")
.filter((p) -> { ___________ })
.forEach(System.out::println);
Medium - Fill in the blank so
prints the number 1:
long count = Stream.of(
"IBM", "Oracle", "Pivotal")
.filter(___________________)
.count();
System.out.println(count);
Twitter: @jeanneboyarsky
Challenge - How many
ways can you write a
lambda that takes one
String parameter named
“s” and returns the result of
s.isEmpty()?
Predicate?
Twitter: @jeanneboyarsky
Find the Functional Interface(s)
interface Slower {
double getCrawlSpeed();
String toString();
default int getSlowestSpeed() { return 0; }
static int getFastestSpeed() { return 10; }
}
interface Swimmer {
double getSwimSpeed();
}
interface Faster extends Slower {
double getFasterSpeed();
}
Twitter: @jeanneboyarsky
Common Functional Interface(s)
+ existing
interfaces
like
Comparable
Twitter: @jeanneboyarsky
FunctionalInterfacesFillInTheBlanks.java
(10-15 mins)
Twitter: @jeanneboyarsky
Easy: Predicate<String> p = s -> s.isEmpty();
Easy: Function<String, Integer> f = ________________;
Medium: Consumer<Double> c = ________________;
Hard: Supplier<Integer> s = _______________;
Challenge: How many ways can you fill in this blank?
_____________ ex = x -> "".equals(x.get(0));
Parts of a Stream Pipeline
Twitter: @jeanneboyarsky
Example of a Stream Pipeline
Twitter: @jeanneboyarsky
List<String> filtered = new ArrayList<>();
for (String name : list) {
if (name.length() == 4)
filtered.add(name);
}
Collections.sort(filtered);
List<String> result = filtered.subList(0, 2);
for (String name : result)
System.out.println(name);
Comparing code
list.stream()
.filter(n -> n.length() == 4)
.sorted().limit(2)
.forEach(System.out::println);
Twitter: @jeanneboyarsky
Example of a Chain
Twitter: @jeanneboyarsky
What happens if missing?
• If no source?
• If no intermediate operations?
• If no terminal operation?
Twitter: @jeanneboyarsky
Creating a Finite Stream
Stream<String> empty = Stream.empty();
Stream<Integer> singleElement =
Stream.of(1);
Stream<Integer> fromArray =
Stream.of(1, 2, 3);
List<String> list =
Arrays.asList("a", "b", "c");
Stream<String> fromList = list.stream();
Twitter: @jeanneboyarsky
Creating an Infinite Stream
Stream<Double> randoms =
Stream.generate(Math::random);
Stream<Integer> oddNumbers =
Stream.iterate(1, n -> n + 2);
Twitter: @jeanneboyarsky
Terminal Operations
Twitter: @jeanneboyarsky
Puzzle – No Intermediate Operations
(10 mins)
Twitter: @jeanneboyarsky
Easy:
Using the puzzle 1 handouts, arrange so the code
prints
true true 3
boolean b1 = ______.___________;
boolean b2 = ________.__________;
long l = ________.________;
System.out.println(b1 + " " + b2 + " " + l);
TranslateSearch.java
(5-10 mins)
Twitter: @jeanneboyarsky
Easy:
Translate this method to a one liner using
streams and lambdas:
private static boolean isZeroInList(
List<BigDecimal> list) {
for (BigDecimal decimal : list) {
if (decimal.equals(BigDecimal.ZERO))
return true;
}
return false;
}
Medium: How might you format the code to make it
easier to read?
Intermediate Operations
filter(p -> p.isEmpty())
limit(3)
skip(2)
map(x -> x.size())
distinct()
sorted()
sorted((a,b) -> a.compareTo(b))
peek(x -> System.out.println(x))
Twitter: @jeanneboyarsky
Puzzle – Intermediate Operations
(10 mins)
Twitter: @jeanneboyarsky
Medium:
Using the puzzle 2 handouts, arrange so the
code prints: true false 3
UnaryOperator<Integer> op = x -> x + 1;
Predicate<Integer> pred = x -> x > 5;
boolean b1 = Stream.iterate(1, op). ______. ______;
boolean b2 = Stream.iterate(1, op)
. ______. ______. ______;
long l = Stream.generate(() -> 1). ______. ______;
System.out.println(b1 + " " + b2 + " " + l);
Challenge: Why are there two correct answers?
CountMatches.java
(10-15 mins)
Twitter: @jeanneboyarsky
Easy:
Translate this method to a one liner using
streams and lambdas: (expected output is 1 and 0)
private static long numberMatches(List<String> list) {
long count = 0;
for (String string : list) {
if (string.contains("g"))
count++;
}
return count;
}
Challenge: Refactor the code so you can pass the
lambda as a method parameter.
MoreLogic.java
(10 mins)
Twitter: @jeanneboyarsky
Medium:
Implement the method to print “walrus” using the logic
described:
public static void main(String[] args) {
List<String> matchingList = Arrays.asList(
"whale", "dolphin", "whale”, "manatee",
"orca", "walrus", "calf");
printMe(matchingList);
}
private static void printMe(List<String> list) {
// Print the third distinct element alphabetically
// that is over 4 characters long
}
Hard: Write “the old way”
Optional
Twitter: @jeanneboyarsky
Optional APIs
Twitter: @jeanneboyarsky
Optional Terminal Operations
Twitter: @jeanneboyarsky
Puzzle – Optional
Twitter: @jeanneboyarsky
Hard:
Using the puzzle 3 handouts, arrange so the
code prints: 1 2 -1
Optional<Integer> o1 = Stream.generate(() -> 1)
. ______;
Optional<Integer> o2 = Stream.iterate(1, x -> x + 1)
. ______. ______. ______;
Optional<Integer> o3 = Stream.iterate(1, x -> x + 1)
. ______. ______. ______;
System.out.println(o1.orElse(-1) + " "
+ o2.orElse(-1) + " " + o3.orElse(-1));
Challenge: Why are there four correct answers?
Collectors
• collect(Collectors.toList())
• collect(Collectors.toSet())
• collect(Collectors.toMap(
keyFunction, valueFunction))
• collect(Collectors.groupingBy(func))
• collect(Collectors.partitioningBy(func))
• collect(Collectors.joining())
• collect(Collectors.joining(”, "))
Twitter: @jeanneboyarsky
Printing a Stream
Twitter: @jeanneboyarsky
Squares.java
(5-10 mins)
Twitter: @jeanneboyarsky
Easy:
Translate this method to a one liner using
streams and lambdas:
private static List<String> convert(List<Integer> list) {
List<String> result = new ArrayList<>();
for (Integer number : list) {
if (number % 2 == 0)
result.add(number + "*" + number + "=" + (number *
number));
}
return result;
}
Challenge: Can you figure out how to make the return
type a LinkedList?
OddsAndEvens.java
(10 mins)
Twitter: @jeanneboyarsky
Medium:
Implement this method using streams and lambdas.
(Remember % is the modulus operator).
public static void main(String[] args) {
System.out.println(split(Arrays.asList(1, 4, 8,
13)));
System.out.println(split(Arrays.asList(1, 3)));
}
private static
Map<Boolean,List<Integer>> split(List<Integer> list) {
// partition the list into odds and evens
}
Hard: Change partioningBy to groupingBy. What’s the
difference?
Challenge: Implement this method without streams.
Method References
Infer lambdas by passing parameters for:
• Static methods
• Constructors
• Instance methods on the lambda param
• Instance methods on a particular instance
Twitter: @jeanneboyarsky
Static References
Twitter: @jeanneboyarsky
Java 7 new Consumer<List<Integer>>() {
public void accept(List<Integer> l) {
Collections.sort(l);
}
}
Lambda
l -> Collections.sort(l)
Method Reference
Collections::sort;
Constructor References
Twitter: @jeanneboyarsky
Java 7 new Supplier<ArrayList>() {
public ArrayList get() {
return new ArrayList();
}
}
Lambda
() -> new ArrayList()
Method Reference
ArrayList::new
Note: passing “zero length param list
Instance References
Twitter: @jeanneboyarsky
Java 7 new Predicate<String>() {
public boolean test(String s) {
return s.isEmpty();
}
}
Lambda s -> s.isEmpty()
Method Reference String::isEmpty
Instance References 2
Twitter: @jeanneboyarsky
Java 7 new Predicate<String>() {
public boolean test(String s) {
return str.startsWith(s);
}
}
Lambda
s -> str.startsWith(s)
Method Reference
str::startsWith
Assume: String str = "abc";
MethodReferencesFillInTheBlanks.java
(10-15 mins)
Easy: Fill in the blank to print with a lambda and method
reference.
Stream.of(“", "b", ”bc”).forEach(_________);
Hard: How many of these can you use a method
reference to fill in the blank? (the string is empty, the
string is not empty, the string ends with ‘c’)
Stream.of(“", "b", ”bc").filter(_________)
.forEach(System.out::println);
Twitter: @jeanneboyarsky
Next Steps to Learn
• Primitive streams
• Reduce()
• Summary Statistics
• Advanced collect()
Twitter: @jeanneboyarsky
Questions
?
Twitter: @jeanneboyarsky

Weitere ähnliche Inhalte

Was ist angesagt?

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced pythonCharles-Axel Dein
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionNandan Sawant
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Paige Bailey
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1Giovanni Della Lunga
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basicsLuigi De Russis
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 englishssuser442080
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2Giovanni Della Lunga
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorFedor Lavrentyev
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queuesIntro C# Book
 
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5PyNSK
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select TopicsJay Coskey
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaMite Mitreski
 

Was ist angesagt? (20)

Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Introduction to advanced python
Introduction to advanced pythonIntroduction to advanced python
Introduction to advanced python
 
Python Puzzlers - 2016 Edition
Python Puzzlers - 2016 EditionPython Puzzlers - 2016 Edition
Python Puzzlers - 2016 Edition
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Java Basics - Part1
Java Basics - Part1Java Basics - Part1
Java Basics - Part1
 
Introduction to python programming 1
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basics
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Introduction to python programming 2
Introduction to python programming   2Introduction to python programming   2
Introduction to python programming 2
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
 
SQL Server Select Topics
SQL Server Select TopicsSQL Server Select Topics
SQL Server Select Topics
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google Guava
 

Ähnlich wie java 8 Hands on Workshop

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Java 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useJava 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useSharon Rozinsky
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리용 최
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)Pavlo Baron
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Simplilearn
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...Functional Thursday
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningBig_Data_Ukraine
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogrammingRichie Cotton
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic SyntaxAdil Jafri
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 

Ähnlich wie java 8 Hands on Workshop (20)

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Nantes Jug - Java 7
Nantes Jug - Java 7Nantes Jug - Java 7
Nantes Jug - Java 7
 
Java 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually useJava 8 new features or the ones you might actually use
Java 8 new features or the ones you might actually use
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python 표준 라이브러리
Python 표준 라이브러리Python 표준 라이브러리
Python 표준 라이브러리
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 

Mehr von Jeanne Boyarsky

Pathways intro january 2018
Pathways intro   january 2018Pathways intro   january 2018
Pathways intro january 2018Jeanne Boyarsky
 
Pathways path-comparison
Pathways path-comparisonPathways path-comparison
Pathways path-comparisonJeanne Boyarsky
 
2017 stuysplash-build-tools
2017 stuysplash-build-tools2017 stuysplash-build-tools
2017 stuysplash-build-toolsJeanne Boyarsky
 
2017 JavaOne Mutation Testing Session
2017 JavaOne Mutation Testing Session2017 JavaOne Mutation Testing Session
2017 JavaOne Mutation Testing SessionJeanne Boyarsky
 
2016 java-sig-mutation-testing
2016 java-sig-mutation-testing2016 java-sig-mutation-testing
2016 java-sig-mutation-testingJeanne Boyarsky
 
2016 first-champs-java-cert
2016 first-champs-java-cert2016 first-champs-java-cert
2016 first-champs-java-certJeanne Boyarsky
 
2015 nyc-spin-collective-ownership
2015 nyc-spin-collective-ownership2015 nyc-spin-collective-ownership
2015 nyc-spin-collective-ownershipJeanne Boyarsky
 
Throw Away all the Rules: Now What Process do you Follow?
Throw Away all the Rules: Now What Process do you Follow?Throw Away all the Rules: Now What Process do you Follow?
Throw Away all the Rules: Now What Process do you Follow?Jeanne Boyarsky
 

Mehr von Jeanne Boyarsky (20)

Pathways intro january 2018
Pathways intro   january 2018Pathways intro   january 2018
Pathways intro january 2018
 
Pathways path-comparison
Pathways path-comparisonPathways path-comparison
Pathways path-comparison
 
2017 stuysplash-build-tools
2017 stuysplash-build-tools2017 stuysplash-build-tools
2017 stuysplash-build-tools
 
Virtual scrum
Virtual scrumVirtual scrum
Virtual scrum
 
Ignite java-robots
Ignite java-robotsIgnite java-robots
Ignite java-robots
 
2017 JavaOne Mutation Testing Session
2017 JavaOne Mutation Testing Session2017 JavaOne Mutation Testing Session
2017 JavaOne Mutation Testing Session
 
2017 java9-spring-days
2017 java9-spring-days2017 java9-spring-days
2017 java9-spring-days
 
Pathways overview
Pathways overviewPathways overview
Pathways overview
 
2016 java-sig-mutation-testing
2016 java-sig-mutation-testing2016 java-sig-mutation-testing
2016 java-sig-mutation-testing
 
Ftc judging
Ftc judgingFtc judging
Ftc judging
 
2016 qcon-virtual-scrum
2016 qcon-virtual-scrum2016 qcon-virtual-scrum
2016 qcon-virtual-scrum
 
2016 java9-how-make-qus
2016 java9-how-make-qus2016 java9-how-make-qus
2016 java9-how-make-qus
 
2016 java9-how-make-qus
2016 java9-how-make-qus2016 java9-how-make-qus
2016 java9-how-make-qus
 
2016 first-champs-java-cert
2016 first-champs-java-cert2016 first-champs-java-cert
2016 first-champs-java-cert
 
2016 java8-cert-intro
2016 java8-cert-intro2016 java8-cert-intro
2016 java8-cert-intro
 
FTC 2015-2016 Judging
FTC 2015-2016 JudgingFTC 2015-2016 Judging
FTC 2015-2016 Judging
 
2015 nyc-spin-collective-ownership
2015 nyc-spin-collective-ownership2015 nyc-spin-collective-ownership
2015 nyc-spin-collective-ownership
 
FTC Robot C to Java
FTC Robot C to JavaFTC Robot C to Java
FTC Robot C to Java
 
Frc java5-8andeclipse
Frc java5-8andeclipseFrc java5-8andeclipse
Frc java5-8andeclipse
 
Throw Away all the Rules: Now What Process do you Follow?
Throw Away all the Rules: Now What Process do you Follow?Throw Away all the Rules: Now What Process do you Follow?
Throw Away all the Rules: Now What Process do you Follow?
 

Kürzlich hochgeladen

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Kürzlich hochgeladen (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

java 8 Hands on Workshop

  • 1. Java 8 Hands on Workshop FIRST Robotics Training July 12, 2017 Jeanne Boyarsky Twitter @jeanneboyarsky Blog: http://www.selikoff.net
  • 2. Disclaimer: Some of this material is copyright of Jeanne Boyarsky, Scott Selikoff and/or Wiley Publishing Twitter: @jeanneboyarsky This PowerPoint is part of a hands on workshop for teaching fluency with Java 8 stream basics.
  • 3. Twitter: @jeanneboyarsky Pre-req: Basic Java knowledge is required. If you can implement this method using a loop, you are good: public int countStringsThatAreFourChars( List<String> list) { }
  • 4. Groovy vs Java Twitter: @jeanneboyarsky print( list .grep{ j -> j.disabled } .collect { j-> j.name } ) list.stream() .filter(j -> j.disabled) .map(j -> j.name) .forEach( System.out::println);
  • 5. Twitter: @jeanneboyarsky TryYourIde.java (5 mins) • Local IDE or • https://www.compilejava.net https://github.com/boyarsky/java-8- streams-by-puzzles Run src/TryYourIde class Prints Do stuff and Do more stuff
  • 6. Twitter: @jeanneboyarsky Lambda vs Boilerplate filter(j -> ! j.disabled) filter(new Predicate<Job>() { public boolean test(Job j) { return ! j.disabled; } })
  • 9. Java 8 Lambda Syntax Rules Must have parens unless exactly one parameter If listing type, must be consistent If multiple statements: requires braces, return and semicolon Twitter: @jeanneboyarsky • () -> true • a -> a.startsWith("test") • (String a) -> a.startsWith("test") • (a, b) -> a.startsWith("test") • (String a, String b) -> { return a.startsWith("test"); }
  • 10. What’s wrong? Twitter: @jeanneboyarsky • a, b -> a.startsWith("test") • a -> { a.startsWith("test"); } • a -> { return a.startsWith("test") } • (String a, b) -> a.startsWith("test") • -> a.startsWith("test”)
  • 11. Using variables interface Gorilla { String move(); } class GorillaFamily { String walk = "walk"; void everyonePlay(boolean baby) { String approach = "amble"; //approach = "run"; play(() -> walk); play(() -> baby ? "hitch a ride" : "run"); play(() -> approach); } void play(Gorilla g) { g.move(); } } 1) Instance var 2) Class var 3) Effectively final method param or local var Twitter: @jeanneboyarsky
  • 12. LambdaFillInTheBlanks.java (10-15 mins) Easy - Fill in the blank so prints out the numbers less than 3: Stream.of(1, 2, 3, 5, 8) .filter(__________) .forEach(System.out::println); Easy - Fill in the blank so prints out the animals that are 4 characters in length: Stream.of("cat", "dot", "fish", "rabbit") .filter((p) -> { ___________ }) .forEach(System.out::println); Medium - Fill in the blank so prints the number 1: long count = Stream.of( "IBM", "Oracle", "Pivotal") .filter(___________________) .count(); System.out.println(count); Twitter: @jeanneboyarsky Challenge - How many ways can you write a lambda that takes one String parameter named “s” and returns the result of s.isEmpty()?
  • 14. Find the Functional Interface(s) interface Slower { double getCrawlSpeed(); String toString(); default int getSlowestSpeed() { return 0; } static int getFastestSpeed() { return 10; } } interface Swimmer { double getSwimSpeed(); } interface Faster extends Slower { double getFasterSpeed(); } Twitter: @jeanneboyarsky
  • 15. Common Functional Interface(s) + existing interfaces like Comparable Twitter: @jeanneboyarsky
  • 16. FunctionalInterfacesFillInTheBlanks.java (10-15 mins) Twitter: @jeanneboyarsky Easy: Predicate<String> p = s -> s.isEmpty(); Easy: Function<String, Integer> f = ________________; Medium: Consumer<Double> c = ________________; Hard: Supplier<Integer> s = _______________; Challenge: How many ways can you fill in this blank? _____________ ex = x -> "".equals(x.get(0));
  • 17. Parts of a Stream Pipeline Twitter: @jeanneboyarsky
  • 18. Example of a Stream Pipeline Twitter: @jeanneboyarsky
  • 19. List<String> filtered = new ArrayList<>(); for (String name : list) { if (name.length() == 4) filtered.add(name); } Collections.sort(filtered); List<String> result = filtered.subList(0, 2); for (String name : result) System.out.println(name); Comparing code list.stream() .filter(n -> n.length() == 4) .sorted().limit(2) .forEach(System.out::println); Twitter: @jeanneboyarsky
  • 20. Example of a Chain Twitter: @jeanneboyarsky
  • 21. What happens if missing? • If no source? • If no intermediate operations? • If no terminal operation? Twitter: @jeanneboyarsky
  • 22. Creating a Finite Stream Stream<String> empty = Stream.empty(); Stream<Integer> singleElement = Stream.of(1); Stream<Integer> fromArray = Stream.of(1, 2, 3); List<String> list = Arrays.asList("a", "b", "c"); Stream<String> fromList = list.stream(); Twitter: @jeanneboyarsky
  • 23. Creating an Infinite Stream Stream<Double> randoms = Stream.generate(Math::random); Stream<Integer> oddNumbers = Stream.iterate(1, n -> n + 2); Twitter: @jeanneboyarsky
  • 25. Puzzle – No Intermediate Operations (10 mins) Twitter: @jeanneboyarsky Easy: Using the puzzle 1 handouts, arrange so the code prints true true 3 boolean b1 = ______.___________; boolean b2 = ________.__________; long l = ________.________; System.out.println(b1 + " " + b2 + " " + l);
  • 26. TranslateSearch.java (5-10 mins) Twitter: @jeanneboyarsky Easy: Translate this method to a one liner using streams and lambdas: private static boolean isZeroInList( List<BigDecimal> list) { for (BigDecimal decimal : list) { if (decimal.equals(BigDecimal.ZERO)) return true; } return false; } Medium: How might you format the code to make it easier to read?
  • 27. Intermediate Operations filter(p -> p.isEmpty()) limit(3) skip(2) map(x -> x.size()) distinct() sorted() sorted((a,b) -> a.compareTo(b)) peek(x -> System.out.println(x)) Twitter: @jeanneboyarsky
  • 28. Puzzle – Intermediate Operations (10 mins) Twitter: @jeanneboyarsky Medium: Using the puzzle 2 handouts, arrange so the code prints: true false 3 UnaryOperator<Integer> op = x -> x + 1; Predicate<Integer> pred = x -> x > 5; boolean b1 = Stream.iterate(1, op). ______. ______; boolean b2 = Stream.iterate(1, op) . ______. ______. ______; long l = Stream.generate(() -> 1). ______. ______; System.out.println(b1 + " " + b2 + " " + l); Challenge: Why are there two correct answers?
  • 29. CountMatches.java (10-15 mins) Twitter: @jeanneboyarsky Easy: Translate this method to a one liner using streams and lambdas: (expected output is 1 and 0) private static long numberMatches(List<String> list) { long count = 0; for (String string : list) { if (string.contains("g")) count++; } return count; } Challenge: Refactor the code so you can pass the lambda as a method parameter.
  • 30. MoreLogic.java (10 mins) Twitter: @jeanneboyarsky Medium: Implement the method to print “walrus” using the logic described: public static void main(String[] args) { List<String> matchingList = Arrays.asList( "whale", "dolphin", "whale”, "manatee", "orca", "walrus", "calf"); printMe(matchingList); } private static void printMe(List<String> list) { // Print the third distinct element alphabetically // that is over 4 characters long } Hard: Write “the old way”
  • 34. Puzzle – Optional Twitter: @jeanneboyarsky Hard: Using the puzzle 3 handouts, arrange so the code prints: 1 2 -1 Optional<Integer> o1 = Stream.generate(() -> 1) . ______; Optional<Integer> o2 = Stream.iterate(1, x -> x + 1) . ______. ______. ______; Optional<Integer> o3 = Stream.iterate(1, x -> x + 1) . ______. ______. ______; System.out.println(o1.orElse(-1) + " " + o2.orElse(-1) + " " + o3.orElse(-1)); Challenge: Why are there four correct answers?
  • 35. Collectors • collect(Collectors.toList()) • collect(Collectors.toSet()) • collect(Collectors.toMap( keyFunction, valueFunction)) • collect(Collectors.groupingBy(func)) • collect(Collectors.partitioningBy(func)) • collect(Collectors.joining()) • collect(Collectors.joining(”, ")) Twitter: @jeanneboyarsky
  • 36. Printing a Stream Twitter: @jeanneboyarsky
  • 37. Squares.java (5-10 mins) Twitter: @jeanneboyarsky Easy: Translate this method to a one liner using streams and lambdas: private static List<String> convert(List<Integer> list) { List<String> result = new ArrayList<>(); for (Integer number : list) { if (number % 2 == 0) result.add(number + "*" + number + "=" + (number * number)); } return result; } Challenge: Can you figure out how to make the return type a LinkedList?
  • 38. OddsAndEvens.java (10 mins) Twitter: @jeanneboyarsky Medium: Implement this method using streams and lambdas. (Remember % is the modulus operator). public static void main(String[] args) { System.out.println(split(Arrays.asList(1, 4, 8, 13))); System.out.println(split(Arrays.asList(1, 3))); } private static Map<Boolean,List<Integer>> split(List<Integer> list) { // partition the list into odds and evens } Hard: Change partioningBy to groupingBy. What’s the difference? Challenge: Implement this method without streams.
  • 39. Method References Infer lambdas by passing parameters for: • Static methods • Constructors • Instance methods on the lambda param • Instance methods on a particular instance Twitter: @jeanneboyarsky
  • 40. Static References Twitter: @jeanneboyarsky Java 7 new Consumer<List<Integer>>() { public void accept(List<Integer> l) { Collections.sort(l); } } Lambda l -> Collections.sort(l) Method Reference Collections::sort;
  • 41. Constructor References Twitter: @jeanneboyarsky Java 7 new Supplier<ArrayList>() { public ArrayList get() { return new ArrayList(); } } Lambda () -> new ArrayList() Method Reference ArrayList::new Note: passing “zero length param list
  • 42. Instance References Twitter: @jeanneboyarsky Java 7 new Predicate<String>() { public boolean test(String s) { return s.isEmpty(); } } Lambda s -> s.isEmpty() Method Reference String::isEmpty
  • 43. Instance References 2 Twitter: @jeanneboyarsky Java 7 new Predicate<String>() { public boolean test(String s) { return str.startsWith(s); } } Lambda s -> str.startsWith(s) Method Reference str::startsWith Assume: String str = "abc";
  • 44. MethodReferencesFillInTheBlanks.java (10-15 mins) Easy: Fill in the blank to print with a lambda and method reference. Stream.of(“", "b", ”bc”).forEach(_________); Hard: How many of these can you use a method reference to fill in the blank? (the string is empty, the string is not empty, the string ends with ‘c’) Stream.of(“", "b", ”bc").filter(_________) .forEach(System.out::println); Twitter: @jeanneboyarsky
  • 45. Next Steps to Learn • Primitive streams • Reduce() • Summary Statistics • Advanced collect() Twitter: @jeanneboyarsky

Hinweis der Redaktion

  1. Key points Quick preview of streams Syntax and flow will come naturally by the end of the session Note that Java and Groovy use similar but different syntax/method names (aka be careful if primarily a Groovy developer) Not expected to understand the details/syntax Appreciate that there isn’t a loop or if statement. Just the declarative statements.
  2. Goal of this “slide” is to ensure all attendees are able to run a Java 8 program – either in their local IDE or online. Online IDEs: compilejava.net – easiest to use – make sure not to have package names ideone.com – has ads, remove public keyword from class to work https://commons.wikimedia.org/wiki/File:Interactive_icon.svg Image from https://commons.wikimedia.org/wiki/File:Gnome-edit-paste.svg
  3. Answers: Need parens since two parameters Need return statement since braces Need semi-colon since braces Need type for neither or both params Need parameter https://goo.gl/images/5X5VL2
  4. Answers: Easy: n -&amp;gt; n &amp;lt; 3 Easy: return p.length() == 4; Medium: There are many correct answers such as n -&amp;gt; n.startsWith(&amp;quot;I&amp;quot;) Challenge: I can think of 6 ways: Predicate&amp;lt;String&amp;gt; p1 = s -&amp;gt; s.isEmpty(); Predicate&amp;lt;String&amp;gt; p2 = (s) -&amp;gt; s.isEmpty(); Predicate&amp;lt;String&amp;gt; p3 = (String s) -&amp;gt; s.isEmpty(); Predicate&amp;lt;String&amp;gt; p4 = s -&amp;gt; { return s.isEmpty(); }; Predicate&amp;lt;String&amp;gt; p5 = (s) -&amp;gt; { return s.isEmpty(); }; Predicate&amp;lt;String&amp;gt; p6 = (String s) -&amp;gt; { return s.isEmpty(); };
  5. Key Points Swimmer is a functional interface Only one method and it is abstract Slower is a functional interface getCrawlSpeed() is abstract toString() is not abstract because inherited from Object getSlowestSpeed() and getFastestSpeed() are not abstract Faster is not a functional interface It has one abstract method declared but inherits another
  6. Key points Don’t need to memorize this. Note that the common combinations exist. If writing code that uses streams, you don’t actually need to know the functional interface name. Compiler infers it. Do need to know these methods if writing libraries
  7. Many answers such as: Easy: s -&amp;gt; s.isEmpty() Easy: s -&amp;gt; s.length() Medium: d -&amp;gt; System.out.println(d) Hard: () -&amp;gt; new Random().nextInt() or () -&amp;gt; 1 Challenge: I can find six Predicate&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; Function&amp;lt;List&amp;lt;String&amp;gt;, Boolean&amp;gt; Consumer&amp;lt;List&amp;lt;String&amp;gt;&amp;gt; Predicate&amp;lt;Map&amp;lt;Integer, String&amp;gt;&amp;gt; Function&amp;lt;Map&amp;lt;Integer, String&amp;gt;, Boolean&amp;gt; Consumer&amp;lt;Map&amp;lt;Integer, String&amp;gt;&amp;gt;
  8. Go over the three parts of a stream pipeline
  9. Walk through the factory example
  10. Compare loop to lambda code
  11. Map the previous example to the three parts of a pipeline
  12. Answers: If no source, can’t start If not intermediate operations ok. If no terminal operations, the stream isn’t run.
  13. Answer: Using the puzzle 1 handouts, arrange so the code prints true true 3 boolean b1 = Stream.empty().allMatch(p -&amp;gt; true); boolean b2 = Stream.generate(String::new).anyMatch(p -&amp;gt; true); long l = Stream.of(1,2,3).count(); System.out.println(b1 + &amp;quot; &amp;quot; + b2 + &amp;quot; &amp;quot; + l); Or boolean b1 = Stream.generate(String::new).anyMatch(p -&amp;gt; true); boolean b2 = Stream.empty().allMatch(p -&amp;gt; true); long l = Stream.of(1,2,3).count(); System.out.println(b1 + &amp;quot; &amp;quot; + b2 + &amp;quot; &amp;quot; + l);
  14. Answer: return list.stream().anyMatch(b -&amp;gt; BigDecimal.ZERO.equals(b)); return list.stream() .anyMatch(b -&amp;gt; BigDecimal.ZERO.equals(b));
  15. Answer: UnaryOperator&amp;lt;Integer&amp;gt; op = x -&amp;gt; x + 1; Predicate&amp;lt;Integer&amp;gt; pred = x -&amp;gt; x &amp;gt; 5; boolean b1 = Stream.iterate(1, op).skip(2).anyMatch(pred); boolean b2 = Stream.iterate(1, op).limit(2).skip(1).allMatch(pred); long l = Stream.generate(() -&amp;gt; 1).limit(3).count(); System.out.println(b1 + &amp;quot; &amp;quot; + b2 + &amp;quot; &amp;quot; + l); Or UnaryOperator&amp;lt;Integer&amp;gt; op = x -&amp;gt; x + 1; Predicate&amp;lt;Integer&amp;gt; pred = x -&amp;gt; x &amp;gt; 5; boolean b1 = Stream.iterate(1, op).skip(1).anyMatch(pred); boolean b2 = Stream.iterate(1, op).skip(2).limit(2).allMatch(pred); long l = Stream.generate(() -&amp;gt; 1).limit(3).count(); System.out.println(b1 + &amp;quot; &amp;quot; + b2 + &amp;quot; &amp;quot; + l);
  16. Answer: return list.stream().filter(s -&amp;gt; s.contains(&amp;quot;g&amp;quot;)).count(); private static long numberMatches(List&amp;lt;String&amp;gt; list, Predicate&amp;lt;String&amp;gt; pred) { return list.stream().filter(pred).count(); }
  17. Answer Medium: list.stream().filter(s -&amp;gt; s.length() &amp;gt; 4) .sorted() .distinct() .skip(2) .limit(1) .forEach(n -&amp;gt; System.out.println(n)); Hard: TreeSet&amp;lt;String&amp;gt; set = new TreeSet&amp;lt;&amp;gt;(list); List&amp;lt;String&amp;gt; sortedList = new ArrayList&amp;lt;&amp;gt;(); for (String string : set) { if (string.length() &amp;gt; 4) { sortedList.add(string); } } System.out.println(sortedList.get(2));
  18. Answer: Optional&amp;lt;Integer&amp;gt; o1 = Stream.generate(() -&amp;gt; 1).findAny(); Optional&amp;lt;Integer&amp;gt; o2 = Stream.iterate(1, x -&amp;gt; x + 1).map(x -&amp;gt; x * 2).limit(3).max(); Optional&amp;lt;Integer&amp;gt; o3 = Stream.iterate(1, x -&amp;gt; x + 1).limit(5).filter(x -&amp;gt; x &amp;gt; 8).findFirst(); Or Optional&amp;lt;Integer&amp;gt; o1 = Stream.generate(() -&amp;gt; 1).findFirst(); Optional&amp;lt;Integer&amp;gt; o2 = Stream.iterate(1, x -&amp;gt; x + 1).map(x -&amp;gt; x * 2).limit(3).max(); Optional&amp;lt;Integer&amp;gt; o3 = Stream.iterate(1, x -&amp;gt; x + 1).limit(5).filter(x -&amp;gt; x &amp;gt; 8).findAny(); Or Optional&amp;lt;Integer&amp;gt; o1 = Stream.generate(() -&amp;gt; 1).findAny(); Optional&amp;lt;Integer&amp;gt; o2 = Stream.iterate(1, x -&amp;gt; x + 1).limit(3).map(x -&amp;gt; x * 2).max(); Optional&amp;lt;Integer&amp;gt; o3 = Stream.iterate(1, x -&amp;gt; x + 1).limit(5).filter(x -&amp;gt; x &amp;gt; 8).findFirst(); Or Optional&amp;lt;Integer&amp;gt; o1 = Stream.generate(() -&amp;gt; 1).findFirst(); Optional&amp;lt;Integer&amp;gt; o2 = Stream.iterate(1, x -&amp;gt; x + 1).limit(3). map(x -&amp;gt; x * 2).max(); Optional&amp;lt;Integer&amp;gt; o3 = Stream.iterate(1, x -&amp;gt; x + 1).limit(5).filter(x -&amp;gt; x &amp;gt; 8).findAny(); For the first line, findAny() or findFirst() give the same result because all the entries are 1. For the last line, either gives the same result because the stream is empty at that point. Also, the order of map and limit doesn’t matter since they don’t interrelate. System.out.println(o1.orElse(-1) + &amp;quot; &amp;quot; + o2.orElse(-1) + &amp;quot; &amp;quot; + o3.orElse(-1));
  19. Answer: return list.stream() .filter(n -&amp;gt; n%2 ==0) .map(n -&amp;gt; n + &amp;quot;*&amp;quot; + n + &amp;quot;=&amp;quot; + (n*n)) .collect(Collectors.toList()); Challenge answer: return list.stream() .filter(n -&amp;gt; n%2 ==0) .map(n -&amp;gt; n + &amp;quot;*&amp;quot; + n + &amp;quot;=&amp;quot; + (n*n)) .collect(Collectors.toCollection(LinkedList::new));
  20. Answer Easy: return list.stream().collect(Collectors.partitioningBy(x -&amp;gt; x % 2 == 0)); Hard: Grouping by doesn’t include the key if there are no matches Challenge: Map&amp;lt;Boolean,List&amp;lt;Integer&amp;gt;&amp;gt; result = new HashMap&amp;lt;&amp;gt;(); result.put(Boolean.FALSE, new ArrayList&amp;lt;&amp;gt;()); result.put(Boolean.TRUE, new ArrayList&amp;lt;&amp;gt;()); for (Integer integer : list) { boolean key = integer %2 == 0; result.get(key).add(integer); } return result;
  21. Answers: Easy: System.out::println and x -&amp;gt; System.out.println(x) Hard: One Stream.of(&amp;quot;&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;bc&amp;quot;).filter(String::isEmpty).forEach(System.out::println); String is not empty requires a lambda (or you to write a static method) because it uses an operator String ends with ‘c’ requires a lambda (or you to write a static method) because it uses a parameter