Share
Embed
Transcript
159 slides
-
Preparing for the Java 25 cert + Learning New Features
Workshop Jeanne Boyarsky 9/9/26 KCDC Lab: https://github.com/boyarsky/2026-kcdc https://linktr.ee/jeanneboyarsky 1 -
At end of session 1. https://speakerdeck.com/boyarsky 2. https://github.com/boyarsky/2026-kcdc-java25 solutions will
be committed https://linktr.ee/jeanneboyarsky 2 -
Pause for a Commercial Java certs: 8/11/17/21 (on sale 25)
Real World Java https://linktr.ee/jeanneboyarsky 3 -
Disclaimers I do not work for Oracle. I’m still not
authorized to tell you any non public information of if I know any :) Some of the material in this presentation appears in our certification books. https://linktr.ee/jeanneboyarsky 4 -
Agenda Module 1 2 3 4 Topics Intro, compact source
files, instance main, IO class, multi file source code, module imports Flexible constructor bodies, unnamed variables, pattern matching changes Stream Gatherers, sequenced collections Virtual threads, scoped values, Bonus: JavaDoc, structured concurrency https://linktr.ee/jeanneboyarsky 6 -
Module Flow •Review lab from previous module •Lecture/review •Hands on
exercises •10 minute break This means if a colleague needs to call you, the last 15-20 minutes of each hour is best. https://linktr.ee/jeanneboyarsky 7 -
Required Software for Lab Option 1 •Java 25+ - https://jdk.java.net/26/
•IDE of your choice or… https://linktr.ee/jeanneboyarsky 8 -
Required Software for Lab Option 2 •https://onecompiler.com/java for module 1
•https://dev.java/playground/ (for modules 2 and 3) https://linktr.ee/jeanneboyarsky 9 -
Features Feature Preview Final Compact Source FIles 21, 22, 23,
24 25 Instance Main 21, 22, 23, 24 25 IO class 25 Multi file source code 22 Module imports 23, 24 https://linktr.ee/jeanneboyarsky 25 10 -
Hello JavaOne public class Hello { public static void main(
String[] args) { } } 25 void main() { IO.println( "Hello JavaOne!"); } System.out.println( "Hello JavaOne!"); • Compact class files • Instance main • IO class @jeanneboyarsky 11 -
Compact Source Files 25 • Default package • (package declaration
not permitted) • Default module • Must declare main method Can have naming conflicts in different folders @jeanneboyarsky 12 -
Compact Source Files • Instance methods allowed? Yes • Static
methods allowed? Yes • Constructors allowed? No @jeanneboyarsky 25 13 -
Compact Source Files 25 • Automatic: • import module java.base;
• More than java.lang! More on module import @jeanneboyarsky 14 -
Instance main 25 void main() {} void main(String[] args) {}
void main(String... args) {} static void main() {} public void main() {} public void main(String[] args) {} + more If two, calls one with args @jeanneboyarsky 15 -
What’s wrong? 25 public class Foo { private Foo() {
} void main() { IO.println("Hello KCDC"); } No public no-args } constructor @jeanneboyarsky 16 -
IO Class IO 25 Wraps IO.print(obj); System.out.print(obj); IO.println(); System.out.println(); IO.println(obj);
System.out.println(obj); IO.readln(); System.in.readlin(); IO.readln(prompt); System.in.readlin(prompt); @jeanneboyarsky 17 -
IO Class 25 Still need originals for • System.err •
All other System.out methods such as printf, format • All other System.in methods such as read() • Ability to wrap in System.in in Scanner • Setting streams to different values @jeanneboyarsky 18 -
Multi-File Source-Code 22 java Conference.java public class Conference { public
Conference(List<Speaker> speakers) { } } Speaker also autocompiled in memory @jeanneboyarsky 19 -
Multi-File Source-Code 22 javac Conference.java public class Conference { public
Conference(List<Speaker> speakers) { } } Speaker also compiled on disk @jeanneboyarsky 20 -
Module Imports 25 // imports over 50 packages import module
java.base; import java.util.List; vs import java.util.*; vs import module java.base; @jeanneboyarsky 21 -
Module Imports Shadowing Code import module.java.sql; Type 25 Imports Module
imports import java.sql.*; 2 direct packages 3 transitive packages (via requires transitive) 1 service (Driver) Wildcard/package import 50+ classes import java.sql.Date; Import single class @jeanneboyarsky 1 class 22 -
Module 1 - Question 1 Which of these are valid
main methods? A. public static void main(String[] args) {} B. void main(String args) {} C. final void main() { } D. private void main(String[] args) {} @jeanneboyarsky 23 -
Module 1 - Question 1 Which of these are valid
main methods? A. public static void main(String[] args) {} A, C B. void main(String args) {} (B wrong type, D private) C. final void main() { } D. private void main(String[] args) {} @jeanneboyarsky 24 -
Module 1 - Question 2 Which of these are valid
main methods? (yes more) A. main() {} B. public void main() {} C. void main(String... args) { } D. void final main(String[] args) {} @jeanneboyarsky 25 -
Module 1 - Question 2 Which of these are valid
main methods? (yes more) A. main() {} B, C B. public void main() (A no{} void, D final order) C. void main(String... args) { } D. void final main(String[] args) {} @jeanneboyarsky 26 -
Module 1 - Question 3 What does this output? 1:
private void print() { 2: IO.println("Ran!"); 3: } 4: void main() { 5: print(); 6: } A. Ran! B. Does not compile @jeanneboyarsky 27 -
Module 1 - Question 3 What does this output? 1:
private void print() { 2: IO.println("Ran!"); 3: } 4: void main() { 5: print(); 6: } A A. Ran! B. Does not compile @jeanneboyarsky 28 -
Module 1 - Question 4 What does this output in
a class named Foo.java? 1: private static void print() { 2: IO.println("Ran!"); 3: } 4: void main() { 5: Foo.print(); 6: } A. Ran! B. Does not compile @jeanneboyarsky 29 -
Module 1 - Question 4 What does this output in
a class named Foo.java? 1: private static void print() { 2: IO.println("Ran!"); 3: } 4: void main() { (static) 5: Foo.print(); 6: } B A. Ran! B. Does not compile @jeanneboyarsky 30 -
Module 1 - Question 5 What does this output? record
Foo() { void main() { IO.println("Ran!"); } } A. Ran! B. Does not compile @jeanneboyarsky 31 -
Module 1 - Question 5 What does this output? record
Foo() { void main() { IO.println("Ran!"); (no public } constructor) } B A. Ran! B. Does not compile @jeanneboyarsky 32 -
Module 1 - Question 6 What does this output? record
Foo() { public Foo() {} void main() { IO.println("Ran!"); } } A. Ran! B. Does not compile @jeanneboyarsky 33 -
Module 1 - Question 6 What does this output? record
Foo() { public Foo() {} void main() { IO.println("Ran!"); } } A A. Ran! B. Does not compile @jeanneboyarsky 34 -
Module 1 - Question 7 Which of these are valid
IO methods? A. IO.println("text"); B. IO.format("Weather: %s", "sunny"); C. IO.readln("Ok?"); D. IO.readPassword(); @jeanneboyarsky 35 -
Module 1 - Question 7 Which of these are valid
IO methods? A. IO.println("text"); A, C B. IO.format("Weather: %s", "sunny"); C. IO.readln("Ok?"); D. IO.readPassword(); @jeanneboyarsky 36 -
Module 1 - Question 8 How many classes are created
when running javac Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac @jeanneboyarsky 37 -
Module 1 - Question 8 How many classes are created
when running javac Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac C @jeanneboyarsky 38 -
Module 1 - Question 9 How many classes are created
when running java Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac @jeanneboyarsky 39 -
Module 1 - Question 9 How many classes are created
when running java Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac A @jeanneboyarsky 40 -
Module 1 - Question 10 What are true of line
5? 1: import java.util.*; 2: import jeanne.List; 3: import module java.base; 4: 5: void main(List list) {} A. The import on line 1 is used for line 5 B. The import on line 2 is used for line 5 C. The import on line 3 is used for line 5 D. This program will run @jeanneboyarsky 41 -
Module 1 - Question 10 What are true of line
5? 1: import java.util.*; 2: import jeanne.List; 3: import module java.base; 4: 5: void main(List list) {} B A. The import on line 1 is used for line 5 B. The import on line 2 is used for line 5 C. The import on line 3 is used for line 5 D. This program will run @jeanneboyarsky 42 -
Agenda Module 1 2 3 4 Topics Intro, compact source
files, instance main, IO class, multi file source code, module imports Flexible constructor bodies, unnamed variables, pattern matching changes Stream Gatherers, sequenced collections Virtual threads, scoped values, Bonus: JavaDoc, structured concurrency https://linktr.ee/jeanneboyarsky 44 -
Features Feature Preview Final Flexible Constructor Bodies 22, 23, 24
25 Unnamed Variables 21 22 Pattern Matching 17, 18,19, 20 21 https://linktr.ee/jeanneboyarsky 45 -
Prologue Rules 25 • Can reference constructor params • Cannot
reference instance methods • Cannot reference instance variables • Except for setting initial value of unitialized one @jeanneboyarsky 47 -
Constructor Flow - What prints? public Bridge(int num) { IO.print("n");
} public class Bridge { { IO.print("i"); } public Bridge() { IO.print("p"); this(1); IO.print("e"); } 25 } public void main() { new Bridge(); } pinepine @jeanneboyarsky 48 -
Unnamed Variables 22 • _ (single underscore) is valid variable
name • Restrictions • Cannot read • Cannot write • Can define multiple in same scope • Useful for: catch, lambda, pattern matching, etc @jeanneboyarsky 49 -
Unnamed Variables 22 // good String _ = "init"; String
_ = "again"; // no good _ = "write"; IO.println(_); String _; @jeanneboyarsky 50 -
Using Pattern Matching 21 null allowed guard clause return switch
(obj) { case null -> ""; case Integer i when i >= 21 -> "can gamble"; case Integer i -> "too young to gamble"; case String s when "poker".equals(s) -> "table game"; case String s when "craps".equals(s) -> "table game"; case String s -> "other game"; default -> throw new IllegalArgumentException("unexpected type"); }; pattern variable Still Null Pointer if don’t specify case null @jeanneboyarsky 51 -
Order matters 21 return switch (obj) { case null ->
""; case Integer i -> "too young to gamble"; case Integer i when i >= 21 -> "can gamble”; // DOES NOT COMPILE case String s -> "other game"; default -> throw new IllegalArgumentException("unexpected type"); }; Order like catching exceptions! @jeanneboyarsky 52 -
Enums 21 enum Suit { HEART, DIAMOND, CLUB, SPADE; }
String symbol(Suit suit) { } return switch (suit) { case HEART -> "♥"; case Suit.DIAMOND -> "♦"; case CLUB -> "♣"; case Suit.SPADE -> "♠"; }; Fails compilation if miss one since no default @jeanneboyarsky 53 -
Record Patterns 21 enum Suit { HEART, DIAMOND, CLUB, SPADE;
} enum Rank { NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7 NUM_8, NUM_9, NUM_10, JACK, QUEEN, KING, ACE; } record Card(Suit suit, Rank rank) { } if (card instanceof Card c) { IO.println(c.suit()); Deconstructed IO.println(c.rank()); } if (card instanceof Card(Suit suit, Rank rank)) { IO.println(suit); IO.println(rank); } https://linktr.ee/jeanneboyarsky 54 -
Nested Record Patterns record MultiDeck(int deckNum, Card card) { 21
} if (multi instanceof MultiDeck(int deckNum, Card(Suit suit, Rank rank))) { System.out.println(deckNum); System.out.println(suit); System.out.println(rank); } https://linktr.ee/jeanneboyarsky 55 -
Switch pattern matching 21 switch(card) { case Card(Suit suit, Rank
rank) when suit == Suit.HEART -> System.out.println("Heart"); case Card c -> System.out.println("other"); } https://linktr.ee/jeanneboyarsky 56 -
Switch pattern matching 21 String str = ""; switch (str)
{ case "heart" -> System.out.println("heart"); } Card card = new Card(Suit.HEART, Rank.NUM_2); switch (card) { case Card(Suit suit, Rank rank) when suit == Suit.HEART -> System.out.println("Heart"); case Card c -> System.out.println("other"); } Must be exhaustive if using “when” https://linktr.ee/jeanneboyarsky 57 -
What can’t you do? 21 • In switch(x), x still
can’t be • boolean • float • double • long • Expanding non-records (coming ?) https://linktr.ee/jeanneboyarsky 58 -
Module 2 - Question 1 What is the output? Blue(String
s) { IO.print(s); super(); } private class Blue { { IO.print("i"); } private Blue() { IO.print("c"); this("l"); } void main() { new Blue(); } } A. cli B. clicli C. Does not compile D. Does not run @jeanneboyarsky 59 -
Module 2 - Question 1 What is the output? private
class Blue { { IO.print("i"); } private Blue() { IO.print("c"); this("l"); } B Blue(String s) { IO.print(s); super(); } void main() { new Blue(); } } A. cli B. clicli C. Does not compile D. Does not run @jeanneboyarsky 60 -
Module 2 - Question 2 Which lines could independently be
replaced by IO.println(shade); 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } 9: A. 3} C. 6 B. 5 D. 7 @jeanneboyarsky 61 -
Module 2 - Question 2 Which lines could independently be
replaced by IO.println(shade); 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } 9: A. 3} C. 6 C, D B. 5 D. 7 @jeanneboyarsky 62 -
Module 2 - Question 3 Which lines could be independently
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 63 -
Module 2 - Question 3 Which lines could be independently
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } B, C A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 64 -
Module 2 - Question 4 Which lines could be independently
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 65 -
Module 2 - Question 4 Which lines could be independently
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } C, D A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 66 -
Module 2 - Question 5 Which lines could independently be
replaced by shade = "sky blue"; 1: public class Blue { 2: private final String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } D. line 5 can have A. 5 B. 6 the replacement applied twice @jeanneboyarsky 67 -
Module 2 - Question 5 Which lines could independently be
replaced by shade = "sky blue"; 1: public class Blue { 2: private final String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } D. line 5 can have A, B, C A. 5 B. 6 the replacement applied twice @jeanneboyarsky 68 -
Module 2 - Question 6 Which lines have compiler errors?
1: void main(String[] _) { 2: double _ = 0.00; 3: String _ = null; 4: IO.println(_); 5: boolean isNull = _ == null; 6: } A. Line 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 @jeanneboyarsky 69 -
Module 2 - Question 6 Which lines have compiler errors?
1: void main(String[] _) { 2: double _ = 0.00; 3: String _ = null; 4: IO.println(_); 5: boolean isNull = _ == null; 6: } A, D, E A. Line 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 @jeanneboyarsky 70 -
Module 2 - Question 7 Which variables can be replaced
by _? void main(String[] args) { enum Suit { HEART, DIAMOND, SPADE, CLUB }; enum Symbol { ACE, JACK, QUEENS, KING}; record Card(Suit suit, Symbol symbol) {} var card = new Card(Suit.HEART, Symbol.ACE); try { if (card instanceof Card(Suit suit, Symbol symbol)) IO.println(suit); } catch (NullPointerException e) { IO.print("party!"); } } A. args B. card C. symbol D. e @jeanneboyarsky 71 -
Module 2 - Question 7 Which variables can be replaced
by _? void main(String[] args) { enum Suit { HEART, DIAMOND, SPADE, CLUB }; enum Symbol { ACE, JACK, QUEENS, KING}; record Card(Suit suit, Symbol symbol) {} var card = new Card(Suit.HEART, Symbol.ACE); try { if (card instanceof Card(Suit suit, Symbol symbol)) IO.println(suit); } catch (NullPointerException e) { IO.print("party!"); } } C, D A. args B. card C. symbol D. e @jeanneboyarsky 72 -
Module 2 - Question 8 How many lines do you
need to remove to make this code compile? double odds(Object obj) { return switch (obj) { case String s -> throw new IllegalArgumentException("unknown game"); case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case Object o -> throw new IllegalArgumentException("unknown game"); default -> throw new IllegalArgumentException("known game"); }; } A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 73 -
Module 2 - Question 8 How many lines do you
need to remove to make this code compile? double odds(Object obj) { return switch (obj) { case String s -> throw new IllegalArgumentException("unknown game"); case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case Object o -> throw new IllegalArgumentException("unknown game"); String s & Object default -> throw new IllegalArgumentException("known game"); o or default }; } C A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 74 -
Module 2 - Question 9 How many variables can you
replace with _? double odds(Object obj) { return switch (obj) { case String s when "blackjack".equals(s) -> .05; case String s -> throw new IllegalArgumentException("unknown game"); case Object o -> throw new IllegalArgumentException("unknown game"); }; } A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 75 -
Module 2 - Question 9 How many variables can you
replace with _? double odds(Object obj) { return switch (obj) { case String s when "blackjack".equals(s) -> .05; case String s -> throw new IllegalArgumentException("unknown game"); case Object o -> throw new IllegalArgumentException("unknown game"); }; } C A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 76 -
Module 2 - Question 10 Which of these variables can
be replaced by _? private int unused; void transform(List<String> list) { if (list instanceof ArrayList arr) try { list.removeIf(x -> true); } catch (NullPointerException e) { } } A. unused B. arr C. x D. e https://linktr.ee/jeanneboyarsky 77 -
Module 2 - Question 10 Which of these variables can
be replaced by _? private int unused; void transform(List<String> list) { if (list instanceof ArrayList arr) try { list.removeIf(x -> true); } catch (NullPointerException e) { } } B, C, D A. unused B. arr C. x D. e https://linktr.ee/jeanneboyarsky 78 -
Agenda Module 1 2 3 4 Topics Intro, compact source
files, instance main, IO class, multi file source code, module imports Flexible constructor bodies, unnamed variables, pattern matching changes Stream Gatherers, sequenced collections Virtual threads, scoped values, Bonus: JavaDoc, structured concurrency https://linktr.ee/jeanneboyarsky 80 -
Features Feature Preview Final Stream Gatherers 22, 23 24 Sequenced
Collections https://linktr.ee/jeanneboyarsky 21 81 -
New Intermediate Operation 24 public Stream<R> gather( Gatherer<? super T,
? , R> gatherer) Predefined or custom https://linktr.ee/jeanneboyarsky 82 -
Predefined Method 24 Description Gatherers.windowFixed() Groups of size x with
possibly smaller last one Gatherers.windowSliding() Groups of size x differing by 1 Gatherers.fold() Combine elements into value Gatherers.scan() Accumulates storing intermediate Gatherers.mapConcurrent() Map with parallelism https://linktr.ee/jeanneboyarsky 83 -
Window Fixed 24 Stream.of(1, 2, 3, 4, 5) .gather(Gatherers.windowFixed(3)) .forEach(IO::println);
[1, 2, 3] [4, 5] https://linktr.ee/jeanneboyarsky 84 -
Window Sliding 24 Stream.of(1, 2, 3, 4, 5) .gather(Gatherers.windowSliding(3)) .forEach(IO::println);
[1, 2, 3] [2, 3, 4] [3, 4, 5] https://linktr.ee/jeanneboyarsky 85 -
Fold 24 Stream.of("w", "o", "l", "f") .gather(Gatherers.fold(() -> “", (s,
c) -> s + c)) .forEach(IO::println); wolf https://linktr.ee/jeanneboyarsky 86 -
Scan 24 Stream.of(10, 15, 13) .gather(Gatherers.scan( () -> 0, (t,
a) -> t + a)) .forEach(IO::println); 10 25 38 https://linktr.ee/jeanneboyarsky 87 -
Map Concurrent 24 Stream.of("a", "b", "c") .gather(Gatherers.mapConcurrent( 2, s ->
s.toUpperCase())) .forEach(IO::println); A B C https://linktr.ee/jeanneboyarsky 88 -
Custom Gatherer parts 24 Function Description Initializer New state. Can
be mutable Integrator Integrates new element Combiner Combines two states Finisher One final action https://linktr.ee/jeanneboyarsky 89 -
Parallel Builders 24 • Gatherer.of(integrator) • Gatherer.of(integrator, finisher) • Gatherer.of(intitializer,
integrator, combiner, finisher) @jeanneboyarsky 90 -
Sequential Builders 24 • Gatherer.ofSequential(integrator) • Gatherer.ofSequential(initializer, integrator) • Gatherer.ofSequential(integrator,
finisher) • Gatherer.ofSequential(initializer, integrator, finisher) No combiner as don’t need to merge intermediate states when sequential @jeanneboyarsky 91 -
Customer Gatherer: Initializer 24 // create empty set Supplier<Set<Character>> initializer
= TreeSet::new; @jeanneboyarsky 92 -
Customer Gatherer: Integrator 24 // add to intermediate set if
letter // stop stream processing when not a letter Gatherer.Integrator<Set<Character>, Character, String> integrator = (set, ch, downstream) -> { if (Character.isLetter(ch)) { set.add(ch); return true; } return false; }; @jeanneboyarsky 93 -
Customer Gatherer: Finisher 24 // convert into a string BiConsumer<Set<Character>,
Gatherer.Downstream<? super String>> finisher = (set, downstream) -> downstream.push( set.stream() .map(String::valueOf) .collect(Collectors.joining(""))); } @jeanneboyarsky 94 -
Customer Gatherer: Using Sequential 24 var gatherer = Gatherer.ofSequential( initializer,
integrator, finisher); Stream.of('J', 'a', 'v', 'a', ' ‘, '2', '5', 'r', 'o', 'c', 'k', 's') .gather(gatherer) .forEach(IO::println); Jav @jeanneboyarsky 95 -
Customer Gatherer: Combiner 24 // combine the two sets BinaryOperator<Set<Character>>
combiner = (set1, set2) -> { set1.addAll(set2); return set1; }; @jeanneboyarsky 96 -
Customer Gatherer: Using Parallel 24 var gatherer = Gatherer.of( initializer,
integrator, combiner, finisher); Stream.of('J', 'a', 'v', 'a', ' ‘, '2', '5', 'r', 'o', 'c', 'k', 's') .gather(gatherer) .forEach(IO::println); Jav @jeanneboyarsky 97 -
Java 17 ArrayList, LinkedList, Deque, TreeSet, TreeMap became sequenced collections
LinkedHashSet and LinkedHashMap too https://linktr.ee/jeanneboyarsky 98 -
What does this output? 21 HashSet<String> set = new HashSet<>();
set.add("Donald"); set.add("Mickey"); set.add("Minnie"); System.out.println(set.iterator().next()); Encounter order undefined(but Mickey on my https://linktr.ee/jeanneboyarsky 99 -
New APIs SequencedCollection SequencedSet SequencedCollection <E> reversed(); void addFirst(E); void
addLast(E); E getFirst(); E getLast(); E removeFirst(); E removeLast(); SequencedSet<E> reversed(); SequencedMap SequencedMap<K,V> reversed(); SequencedSet<K> sequencedKeySet(); SequencedCollection<V> sequencedValues(); SequencedSet<Entry<K,V>> sequencedEntrySet(); V putFirst(K, V); V putLast(K, V); Entry<K, V> rstEntry(); Entry<K, V> lastEntry(); Entry<K, V> pollFirstEntry(); Entry<K, V> pollLastEntry(); https://linktr.ee/jeanneboyarsky fi 21 101 -
Now defined! 21 SequencedSet<String> set = new LinkedHashSet<>(); set.add("Donald"); set.add("Mickey");
set.add("Minnie"); System.out.println(set.getFirst()); Donald Note: LInkedHashSet not new https://linktr.ee/jeanneboyarsky 102 -
Module 3 - Question 1 Which part of a stream
gatherer sets the value if the stream is empty? A. Constructor C. Initializer B. Integrator D. Instantiator https://linktr.ee/jeanneboyarsky 103 -
Module 3 - Question 1 Which part of a stream
gatherer sets the value if the stream is empty? C A. Constructor C. Initializer B. Integrator D. Instantiator https://linktr.ee/jeanneboyarsky 104 -
Module 3 - Question 2 Which part of a stream
gatherer processes a single value from the stream? A. Combiner C. Integrator B. Consolidator D. Initializer https://linktr.ee/jeanneboyarsky 105 -
Module 3 - Question 2 Which part of a stream
gatherer processes a single value from the stream? C A. Combiner C. Integrator B. Consolidator D. Initializer https://linktr.ee/jeanneboyarsky 106 -
Module 3 - Question 3 Which part of a stream
gatherer is only used for a parallel stream? A. Combiner C. Integrator B. Consolidator D. Merger https://linktr.ee/jeanneboyarsky 107 -
Module 3 - Question 3 Which part of a stream
gatherer is only used for a parallel stream? A A. Combiner C. Integrator B. Consolidator D. Merger https://linktr.ee/jeanneboyarsky 108 -
Module 3 - Question 4 Which part of a stream
gatherer returns the value for the next step in the pipeline at the end of the gatherer A. Ender C. StreamOperation B. Finisher D. Terminator https://linktr.ee/jeanneboyarsky 109 -
Module 3 - Question 4 Which part of a stream
gatherer returns the value for the next step in the pipeline at the end of the gatherer B A. Ender C. StreamOperation B. Finisher D. Terminator https://linktr.ee/jeanneboyarsky 110 -
Module 3 - Question 5 If you want to return
multiple elements to the stream what parts can push to the downstream? A. Initializer C. Combiner B. Integrator D. Finisher https://linktr.ee/jeanneboyarsky 111 -
Module 3 - Question 5 If you want to return
multiple elements to the stream what parts can push to the downstream? B, D A. Initializer C. Combiner B. Integrator D. Finisher https://linktr.ee/jeanneboyarsky 112 -
Module 3 - Question 6 What is the output of
the following? var list = Stream.of("kansas", "missouri") .gather(Gatherers.scan( () -> "", (s1, s2) -> s1 + " " + s2)) .toList(); IO.println(list); A. [kansas missouri] B. [ kansas missouri] C. [kansas, kansas missouri] D. [ kansas, kansas missouri] https://linktr.ee/jeanneboyarsky 113 -
Module 3 - Question 6 What is the output of
the following? var list = Stream.of("kansas", "missouri") .gather(Gatherers.scan( () -> "", (s1, s2) -> s1 + " " + s2)) .toList(); IO.println(list); D A. [kansas, missouri] B. [ kansas, missouri] C. [kansas, kansas missouri] D. [ kansas, kansas missouri] https://linktr.ee/jeanneboyarsky 114 -
Module 3 - Question 7 What is the output of
the following? var list = Stream.of("kansas", "missouri") .gather(Gatherers.fold( () -> "", (s1, s2) -> s1 + " " + s2)) .toList(); IO.println(list); A. [kansas missouri] B. [ kansas missouri] C. [kansas, kansas missouri] D. [ kansas, kansas missouri] https://linktr.ee/jeanneboyarsky 115 -
Module 3 - Question 7 What is the output of
the following? var list = Stream.of("kansas", "missouri") .gather(Gatherers.fold( () -> "", (s1, s2) -> s1 + " " + s2)) .toList(); IO.println(list); B A. [kansas missouri] B. [ kansas missouri] C. [kansas, kansas missouri] D. [ kansas, kansas missouri] https://linktr.ee/jeanneboyarsky 116 -
Module 3 - Question 8 What is the output of
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowFixed(4)) .toList(); IO.println(list); } A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 117 -
Module 3 - Question 8 What is the output of
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowFixed(4)) .toList(); IO.println(list); } B A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 118 -
Module 3 - Question 9 What is the output of
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowSliding(4)) .toList(); IO.println(list); } A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 119 -
Module 3 - Question 9 What is the output of
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowSliding(4)) .toList(); IO.println(list); } C A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 120 -
Module 4 - Question 10 How many of these lines
compile? SequencedCollection<Integer> seq1 = new HashSet<>(); SequencedSet<Integer> seq2 = new HashSet<>(); SequencedMap<Integer> seq3 = new HashSet<>(); SequencedCollection<Integer> seq4 = new TreeSet<>(); SequencedSet<Integer> seq5 = new TreeSet<>(); SequencedMap<Integer> seq6 = new TreeSet<>(); A. One C. Three B. Two D. Four https://linktr.ee/jeanneboyarsky 121 -
Module 4 - Question 10 How many of these lines
compile? SequencedCollection<Integer> seq1 = new HashSet<>(); SequencedSet<Integer> seq2 = new HashSet<>(); SequencedMap<Integer> seq3 = new HashSet<>(); SequencedCollection<Integer> seq4 = new TreeSet<>(); (seq4=and 5) SequencedSet<Integer> seq5 newseq TreeSet<>(); SequencedMap<Integer> seq6 = new TreeSet<>(); B A. One C. Three B. Two D. Four https://linktr.ee/jeanneboyarsky 122 -
Agenda Module 1 2 3 4 Topics Intro, compact source
files, instance main, IO class, multi file source code, module imports Flexible constructor bodies, unnamed variables, pattern matching changes Stream Gatherers, sequenced collections Virtual threads, scoped values, Bonus: JavaDoc, structured concurrency https://linktr.ee/jeanneboyarsky 124 -
Features Feature Preview Final Virtual Threads 19, 20 21 Scoped
Values 21, 22, 23, 24 25 JavaDoc 18, 23 Structured Concurrency 21, 22, 23, 24, 25, 26, 27 https://linktr.ee/jeanneboyarsky TBD 125 -
Leaps in Concurrency + Executors Threads 21 + Virtual Threads
Plus many APIs… https://linktr.ee/jeanneboyarsky 126 -
Why we need threads Program I’m bored… Do calculation and
call REST API CPU Do calculation and call REST API Network https://linktr.ee/jeanneboyarsky I’m waiting for a reply 127 -
Why we need virtual threads Program Platform Thread 1 Platform
Thread n Still bored… Send to threads Do calculation and call REST API Do calculation and call REST API Proceed Network https://linktr.ee/jeanneboyarsky CPU Still waiting for a reply 128 -
With virtual threads Program Send to threads Proceed Platform Thread
1 Platform Thread n Virtual Thread 1 Virtual Thread t+1 Do calculation and call REST API Do calculation and call REST API Virtual Thread t Virtual Thread x Do calculation and call REST API Do calculation and call REST API Thanks for the work! CPU Me too! Network https://linktr.ee/jeanneboyarsky 129 -
When most helpful? 21 • Thousands of threads • Not
CPU bound https://linktr.ee/jeanneboyarsky 130 -
Comparing 21 Platform (Traditional) Thread Heavyweight Virtual Thread Tied to
OS threads Runs on OS thread but doesn’t monopolize Often shortlived Often pooled Lightweight Instance of java.lang.Thread Instance of java.lang.Thread https://linktr.ee/jeanneboyarsky 131 -
Fill in the blank 21 try (ExecutorService service = Executors._________)
{ service.submit(() -> doStuff()); } Examples: newSingleThreadExecutor() newFixedThreadPool(5) newCachedThreadPool() newVirtualThreadPerTaskExecutor() https://linktr.ee/jeanneboyarsky 132 -
Autocloseable 19 Method Java 21 shutdown() shutdownNow() No more tasks,
but finish ones have Stop tasks in progress close() Calls shutdown by default https://linktr.ee/jeanneboyarsky 133 -
If need directly Platform Thread Virtual Thread Thread.ofPlatform() Thread.ofVirtual() new
Thread(…) Not via constructor https://linktr.ee/jeanneboyarsky 21 134 -
Scoped Values 25 static final ScopedValue<List<String>> SESSIONS = ScopedValue.newInstance(); void
main() { var sessions = List.of("Java 25", "AI"); ScopedValue.where(SESSIONS, sessions).run( () -> { attend(); }); } void attend() { immutable IO.println(SESSIONS.get()); scoped data } @jeanneboyarsky 135 -
Scoped Values 25 static final ScopedValue<List<String>> SESSIONS = ScopedValue.newInstance(); void
main() { IO.print(SESSIONS.isBound()); IO.print(SESSIONS.orElse("Not found"); SESSIONS.get(); } false Not found NoSuchElementException @jeanneboyarsky 136 -
Structured Concurrency ? Incubated Java 19 Sixth preview in Java
27 @jeanneboyarsky 137 -
Structured Concurrency Preview 25 static final ScopedValue<List<String>> SESSIONS = ScopedValue.newInstance();
void main() { ScopedValue.where(SESSIONS, List.of()) .run(() -> { scope.fork(() -> attend()); scope.fork(() -> attend()); }); } @jeanneboyarsky 138 -
Bonus: JavaDoc 23 /// While /** */ uses HTML, ///
triple slashes use **markdown** /// /// commonmark.org format /// - on odd days, … /// - on even days, … /// /// @return an important number @jeanneboyarsky 139 -
Module 4 - Question 1 Which is used to create/with
a virtual thread? (more than 1 is correct) A. Executors.newVirtualThread() B. Executors.newVirtualThreadExecutor() C. Executors.newVirtualThreadPerTaskExecutor() D. new VirtualThread() E. Thread.ofVirtual() F. Thread.ofVirtualThread() https://linktr.ee/jeanneboyarsky 140 -
Module 4 - Question 1 Which is used to create/with
a virtual thread? (more than 1 is correct) A. Executors.newVirtualThread() B. Executors.newVirtualThreadExecutor() C, E C. Executors.newVirtualThreadPerTaskExecutor() D. new VirtualThread() E. Thread.ofVirtual() F. Thread.ofVirtualThread() https://linktr.ee/jeanneboyarsky 141 -
Module 4 - Question 2 Which is used to create/with
a platform thread? (more than 1 is correct) A. Executors.newCachedThreadPool() B. Executors.newPlatformThreadPool() C. Executors.newPlatformThreadPerTaskExecutor() D. new Thread() E. new PlatformThread() F. Thread.ofPlatform() https://linktr.ee/jeanneboyarsky 142 -
Module 4 - Question 2 Which is used to create/with
a platform thread? (more than 1 is correct) A. Executors.newCachedThreadPool() B. Executors.newPlatformThreadPool() A, D, F C. Executors.newPlatformThreadPerTaskExecutor() D. new Thread() E. new PlatformThread() F. Thread.ofPlatform() https://linktr.ee/jeanneboyarsky 143 -
Module 4 - Question 3 What is true doStuff() at
line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } // line v1 A. doStuff() may be running C. doStuff() completed B. doStuff() was killed D. None of the above https://linktr.ee/jeanneboyarsky 144 -
Module 4 - Question 3 What is true doStuff() at
line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } C // line v1 A. doStuff() may be running C. doStuff() completed B. doStuff() was killed D. None of the above https://linktr.ee/jeanneboyarsky 145 -
Module 4 - Question 4 Which types are are immutable
in SV? static final ScopedValue<XXX>> SV = ScopedValue.newInstance(); A. List B. List<String> C. LocalDateTime D. String @jeanneboyarsky 146 -
Module 4 - Question 4 Which types are are immutable
in SV? static final ScopedValue<XXX>> SV = ScopedValue.newInstance(); C, D A. List B. List<String> C. LocalDateTime D. String @jeanneboyarsky 147 -
Module 4 - Question 5 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 148 -
Module 4 - Question 5 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } D A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 149 -
Module 4 - Question 6 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { ScopedValue.where(ID, 10) .run(this::timing); } A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 150 -
Module 4 - Question 6 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { ScopedValue.where(ID, 10) .run(this::timing); } B A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 151 -
Module 4 - Question 7 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.orElse(2)); } void main() { timing(); } A. 0 B. 2 C. Code does not compile D. Throws an exception @jeanneboyarsky 152 -
Module 4 - Question 7 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); B void timing() { IO.print(LENGTH.orElse(2)); } void main() { timing(); } A. 0 B. 2 C. Code does not compile D. Throws an exception @jeanneboyarsky 153 -
Module 4 - Question 8 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.isBound()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } A. falsefalse B. falsetrue C. truefalse D. truetrue @jeanneboyarsky 154 -
Module 4 - Question 8 What is the output of
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.isBound()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } B A. falsefalse B. falsetrue C. truefalse D. truetrue @jeanneboyarsky 155 -
Module 4 - Question 9 True or false: you can
fork a ScopedValue? A. true B. false @jeanneboyarsky 156 -
Module 4 - Question 9 True or false: you can
fork a ScopedValue? A. true B. false Depends on Java version @jeanneboyarsky 157 -
Module 4 - Question 10 I learned a lot today
and I’m ready to code A. True B. False @jeanneboyarsky 158