$30 off During Our Annual Pro Sale. View Details »

Java Idioms for becoming a more powerful developer

Java Idioms for becoming a more powerful developer

Half day workshop at KCDC: Knowing programming idioms makes us more powerful and faster developers. This session will cover common (and not so common) idioms with regular expressions, file I/O and streams/lambdas. After attending this session, you’ll be able to impress your co-workers with your increased speed in solving programming tasks. You’ll also have the tools to spend less time on the “easy tasks” and more time solving business problems.

Jeanne Boyarsky

June 21, 2023
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Technology

Transcript

  1. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky 1
    Java Idioms
    Jeanne Boyarsky


    Wednesday June 21, 2023


    KCDC


    speakerdeck.com/boyarsky


    View Slide

  2. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    About Me
    • Java Champion


    • Author


    • Developer at NYC
    bank for 21+
    years


    • FIRST Robotics
    Mentor
    2

    View Slide

  3. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Pause for a Commercial
    3
    Java certs: 8/11/17


    Book giveaway at end!

    View Slide

  4. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Disclaimer
    • A bit of the material is from my
    books.


    • Some of the material in this
    presentation may appear in
    our upcoming certification
    books.
    4

    View Slide

  5. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Introductions
    5

    View Slide

  6. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Agenda
    • Strings and Text Blocks


    • Regular Expressions


    • Collections & Streams


    • File I/O + Other Useful APIs
    6

    View Slide

  7. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    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.
    7

    View Slide

  8. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Idiom
    • Recurring construct


    • Has meaning


    • Common approach
    8

    View Slide

  9. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Design Patterns
    • Typically higher level


    • May be language agnostic
    9

    View Slide

  10. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Anti-Pattern
    • Common “solution”


    • Negative effects
    10

    View Slide

  11. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Example #1 - What is it?
    11
    Design Pattern


    (Singleton)

    View Slide

  12. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Example #2 - What is it?
    12
    Idiom


    (Pre-Java 8)

    View Slide

  13. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Example #3 - What is it?
    13
    Anti-pattern


    * catches “Exception”


    * ignores exception

    View Slide

  14. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Example #4 - What is it?
    14

    View Slide

  15. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Agenda
    • Strings and Text Blocks


    • Regular Expressions


    • Collections & Streams


    • File I/O + Other Useful APIs
    15

    View Slide

  16. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Is there a match?
    16
    IntelliJ warning!

    View Slide

  17. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky 17
    Is there a match?

    View Slide

  18. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Only whitespace
    18

    View Slide

  19. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Even Better
    19
    Java 11
    Java 11

    View Slide

  20. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    String Concatentation
    20
    Best choice
    changes over time

    View Slide

  21. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Equal?
    21

    View Slide

  22. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Case Insensitive Equal
    22

    View Slide

  23. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Case Insensitive Compare
    23

    View Slide

  24. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Creating Csv
    24
    Better to use
    Commons
    CSV Library

    View Slide

  25. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Multiples
    25
    Java 11

    View Slide

  26. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Formatting
    • %s - string


    • %d - int


    • %f - float/double (minimum width/precision)


    • %n - new line

    View Slide

  27. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Formatting
    Java 15

    View Slide

  28. @jeanneboyarsky
    Text Blocks
    28
    Java 15


    (adds line breaks)

    View Slide

  29. @jeanneboyarsky
    Text Blocks
    29
    incidental


    whitespace
    essential


    whitespace
    no trailing


    new line
    quote not


    escaped

    View Slide

  30. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Other “new” APIs
    API Introduced
    stripLeading()


    stripTrailing()


    lines()
    11
    indent(number) 12
    transform(function) 12
    stripIndent()


    translateEscapes()
    15
    indexOf(ch, beginIndex, endIndex)


    indexOf(str, beginIndex, endIndex)
    21
    splitWithDelimiters(regex, limit) 21

    View Slide

  31. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Review - What best API?
    • Check if String empty?


    • Duplicate String?


    • Merge Strings?


    • Compare case sensitive
    strings?


    • Compare non-case sensitive
    strings?
    31

    View Slide

  32. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab
    32
    Do NOT use streams or lambdas


    in this lab

    View Slide

  33. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab & Break
    33
    https://github.com/boyarsky/
    2023-kcdc-java-idioms

    View Slide

  34. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Agenda
    • Strings and Text Blocks


    • Regular Expressions


    • Collections & Streams


    • File I/O + Other Useful APIs
    34

    View Slide

  35. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab Review
    • Any questions?


    • How did you solve Tic Tac Toe?
    35

    View Slide

  36. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Remove all Numbers
    36
    Note replaceAll
    What are two ways to remove
    blank spaces too?

    View Slide

  37. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Remove leading pattern
    37
    Three changes:


    replaceFirst()


    ^


    +

    View Slide

  38. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Remove trailing pattern
    38
    Watch escaping

    View Slide

  39. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Remove middle
    39
    Off by one error?

    View Slide

  40. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Ugly regex
    40

    View Slide

  41. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Better regex
    41

    View Slide

  42. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Regex quick reference
    42
    0 or more *
    1 or more +
    0 or 1 ?
    any .
    three x’s x{3}
    range [0-9]
    Not in range [^0-9]
    Digit \d
    White space \s
    Word \w

    View Slide

  43. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Brainstorm
    43
    How many ways can we match


    one or more using at most


    2 x characters?

    View Slide

  44. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Splitting on a Regex
    44

    View Slide

  45. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Contains only digits
    45

    View Slide

  46. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Contains any digits
    46
    More to regex

    View Slide

  47. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Case Insensitive
    47

    View Slide

  48. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Case Insensitive
    48

    View Slide

  49. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Multi Line Replace
    49
    (?m)


    Also $1
    a


    b


    c

    View Slide

  50. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Dot All
    50
    (?s)










    View Slide

  51. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Pattern Idiom
    51

    View Slide

  52. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Pattern Replace
    52

    View Slide

  53. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Review - How do I?
    • Match digit?


    • Remove a single match?


    • Treat dot as any character?


    • Match a character 100 times?


    • Loop through matches?


    • Match a period?
    53

    View Slide

  54. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab & Break
    54
    https://github.com/boyarsky/
    2023-kcdc-java-idioms

    View Slide

  55. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Agenda
    • Strings and Text Blocks


    • Regular Expressions


    • Collections & Streams


    • File I/O + Other Useful APIs
    55

    View Slide

  56. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab Review
    • Any questions?


    • Did you like replaceAll() or
    Pattern better?


    • What was hardest?
    56

    View Slide

  57. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Create a Set
    57
    Java 9

    View Slide

  58. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Create a List
    58
    Java 9

    View Slide

  59. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Mutable List
    59
    Java 9

    View Slide

  60. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Mutable Copy
    60

    View Slide

  61. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Immutable Copy
    61
    Java 10

    View Slide

  62. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Synchronized View
    62

    View Slide

  63. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Unmodifiable View
    63

    View Slide

  64. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Create a Map
    64

    View Slide

  65. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Create a Map
    65
    Java 9

    View Slide

  66. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Sorting
    66

    View Slide

  67. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Sorting
    67

    View Slide

  68. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Removing from a List
    68

    View Slide

  69. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Element in List
    69

    View Slide

  70. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Replacing
    70

    View Slide

  71. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Venn Diagram?
    71

    View Slide

  72. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Defaulting
    72

    View Slide

  73. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Add to map if not present
    73

    View Slide

  74. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Other Useful Map Methods
    • containsKey(), containsValue()


    • entrySet()


    • keySet(), values()


    • remove(key)


    • remove(key, value)


    • replace(key, value)


    • replace(key, oldValue, newValue)
    74

    View Slide

  75. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Printing
    75

    View Slide

  76. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Review - How do I?
    • Create an immutable copy


    • Remove all values less than 5


    • Update the value in a map if the
    current value is 10


    • Return 0 if the value isn’t found


    • Find the intersection of two
    collections
    76

    View Slide

  77. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Find max
    77

    View Slide

  78. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Join a String
    78

    View Slide

  79. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Get First Elements
    79
    Java 9
    and 16

    View Slide

  80. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Get element
    80
    Java 9

    View Slide

  81. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Strings
    81

    View Slide

  82. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Useful Terminal Operations
    • count()


    • collect() - more on this next module


    • allMatch(), anyMatch(), noneMatch()


    • findFirst(), findAny()


    • min()/max()


    • forEach()


    • toArray, toList()
    82

    View Slide

  83. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Useful Intermediate Operations
    • filter()


    • map()


    • distinct()


    • limit(), skip()


    • sorted()


    • peek()
    83

    View Slide

  84. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Review - What method can…
    • Return whether 5/5 values == 7?


    • Return whether >=1 values == 7?


    • Make a stream smaller?


    • Change the order of stream elements?


    • Loop through stream elements?


    • Ignore the first 2 elements?


    • Ignore any elements are the first 2?
    84

    View Slide

  85. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Infinite: Generating
    85

    View Slide

  86. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Infinite: Iterative
    86
    Java 9

    View Slide

  87. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    FlatMap
    87

    View Slide

  88. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Creating Map
    88

    View Slide

  89. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Creating Map With Conflict
    89

    View Slide

  90. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Grouping By Key
    90

    View Slide

  91. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Partitioning By Key
    91

    View Slide

  92. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Grouping By Key
    92

    View Slide

  93. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Reduce
    93

    View Slide

  94. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Review
    • Sources


    • generate()


    • iterate()


    • Intermediate


    • flatMap()


    • Terminal


    • reduce()


    • Collectors


    • toMap()


    • groupingBy()


    • partitioningBy()
    94

    View Slide

  95. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    What method for?
    • Creating an infinite stream of a times
    table?


    • Creating a Map that always has two
    keys?


    • Creating a Map with a custom key/
    value?


    • Returning an Optional representing the
    stream?
    95

    View Slide

  96. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab & Break
    96
    https://github.com/boyarsky/
    2023-kcdc-java-idioms

    View Slide

  97. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Agenda
    • Strings and Text Blocks


    • Regular Expressions


    • Collections & Streams


    • File I/O + Other Useful APIs
    97

    View Slide

  98. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab Review
    • How did you feel about longer
    streams?


    • Did you see how different
    methods are useful for
    different problems?
    98

    View Slide

  99. @jeanneboyarsky
    Records
    99
    Java 16
    Ran out of room
    on the screen

    View Slide

  100. @jeanneboyarsky
    Records
    100
    Automatically get


    * final record


    * private final instance
    variables


    * public accessors


    * constructor taking both
    fields


    * equals


    * hashCode


    Can @Override any
    methods (ex: backward
    compatibility for getters)

    View Slide

  101. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Creating a Path
    101

    View Slide

  102. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Absolute Path
    102

    View Slide

  103. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Separators
    103
    Windows:


    \\ and ;
    Mac/Linux:


    / and :

    View Slide

  104. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Read File into String
    104
    Java 11

    View Slide

  105. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Read File into List
    105

    View Slide

  106. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Read File with Stream
    106

    View Slide

  107. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Write File from String
    107
    Java 11

    View Slide

  108. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Write File from List
    108

    View Slide

  109. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Append To File
    109

    View Slide

  110. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Walk Tree
    110

    View Slide

  111. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Convert to unchecked
    111
    Or
    unchecked
    wrapper

    View Slide

  112. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Delete File
    112

    View Slide

  113. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    What API to?
    • Read file into a single string?


    • Write a List to a file?


    • Get rid of a file?


    • List all files in directory?


    • Add to a file?


    • Process file as a
    Stream?
    113

    View Slide

  114. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Math
    114

    View Slide

  115. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Math
    115

    View Slide

  116. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Random
    116

    View Slide

  117. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Random
    117

    View Slide

  118. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Random
    118

    View Slide

  119. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Random
    119

    View Slide

  120. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    LocalDate
    120

    View Slide

  121. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    LocalDate
    121

    View Slide

  122. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    What API to?
    • Get today?


    • Get July 4th of this year?


    • Create a random number
    between 2 and 10?


    • Get the largest value of two?


    • Get a unique id?
    122

    View Slide

  123. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Win a book
    123

    View Slide

  124. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky

    View Slide

  125. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky
    Lab & Break
    125
    https://github.com/boyarsky/
    2023-kcdc-java-idioms

    View Slide