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

Java Idioms

Java Idioms

This is the first draft of what I'll be presenting at DevNexus in February.

Jeanne Boyarsky

February 19, 2020
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Programming

Transcript

  1. @jeanneboyarsky 1
    Java Idioms
    Jeanne Boyarsky
    Wednesday February 19, 2020
    DevNexus
    speakerdeck.com/boyarsky

    View Slide

  2. @jeanneboyarsky
    About Me
    • Java Champion
    • Author
    • Developer at
    NYC bank for
    17+ years
    • FIRST Robotics
    Mentor
    2

    View Slide

  3. @jeanneboyarsky
    Pause for a Commercial
    3
    Java 11 certs
    •1Z0-815 - Out now
    •1Z0-816 - April ETA

    View Slide

  4. @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. @jeanneboyarsky
    Introductions
    5

    View Slide

  6. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    6

    View Slide

  7. @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. @jeanneboyarsky
    Idiom
    • Recurring construct
    • Has meaning
    • Common approach
    8

    View Slide

  9. @jeanneboyarsky
    Design Patterns
    • Typically higher level
    • May be language agnostic
    9

    View Slide

  10. @jeanneboyarsky
    Anti-Pattern
    • Common “solution”
    • Negative effects
    10

    View Slide

  11. @jeanneboyarsky
    Example #1 - What is it?
    11
    Design Pattern
    (Singleton)

    View Slide

  12. @jeanneboyarsky
    Example #2 - What is it?
    12
    Idiom
    (Pre-Java 8)

    View Slide

  13. @jeanneboyarsky
    Example #3 - What is it?
    13
    Anti-pattern
    * catches “Exception”
    * ignores exception

    View Slide

  14. @jeanneboyarsky
    Example #4 - What is it?
    14

    View Slide

  15. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    15

    View Slide

  16. @jeanneboyarsky
    Is there a match?
    16
    IntelliJ warning!

    View Slide

  17. @jeanneboyarsky 17
    Is there a match?

    View Slide

  18. @jeanneboyarsky
    Only whitespace
    18

    View Slide

  19. @jeanneboyarsky
    Even Better
    19
    Java 11
    Java 11

    View Slide

  20. @jeanneboyarsky
    String Concatentation
    20
    Best choice
    changes over time

    View Slide

  21. @jeanneboyarsky
    Equal?
    21

    View Slide

  22. @jeanneboyarsky
    Case Insensitive Equal
    22

    View Slide

  23. @jeanneboyarsky
    Case Insensitive Compare
    23

    View Slide

  24. @jeanneboyarsky
    Creating Csv
    24
    Better to use
    Commons
    CSV Library

    View Slide

  25. @jeanneboyarsky
    Multiples
    25

    View Slide

  26. @jeanneboyarsky
    How do I unit test this?
    26
    can
    edit?
    pass in a
    mock
    stub out real
    System.out
    Yes
    No

    View Slide

  27. @jeanneboyarsky
    Pass Mock object
    27

    View Slide

  28. @jeanneboyarsky
    And Test
    28

    View Slide

  29. @jeanneboyarsky
    If Cannot Change
    29

    View Slide

  30. @jeanneboyarsky
    Formatting
    • %s - string
    • %d - int
    • %f - float/double (minimum width/precision)
    • %n - new line

    View Slide

  31. @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. @jeanneboyarsky
    Lab
    32
    Do NOT use streams or lambdas
    in this lab

    View Slide

  33. @jeanneboyarsky
    Lab & Break
    33
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  34. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    34

    View Slide

  35. @jeanneboyarsky
    Lab Review
    • Any questions?
    • How did you solve Tic Tac Toe?
    35

    View Slide

  36. @jeanneboyarsky
    Remove all Numbers
    36
    Note replaceAll
    What are two ways to remove
    blanks spaces too?

    View Slide

  37. @jeanneboyarsky
    Remove leading pattern
    37
    Three changes:
    replaceFirst()
    ^
    +

    View Slide

  38. @jeanneboyarsky
    Remove trailing pattern
    38
    Watch escaping

    View Slide

  39. @jeanneboyarsky
    Remove middle
    39
    Off by one error?

    View Slide

  40. @jeanneboyarsky
    Ugly regex
    40

    View Slide

  41. @jeanneboyarsky
    Better regex
    41

    View Slide

  42. @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. @jeanneboyarsky
    Splitting on a Regex
    43

    View Slide

  44. @jeanneboyarsky
    Contains only digits
    44

    View Slide

  45. @jeanneboyarsky
    Contains any digits
    45
    More to regex

    View Slide

  46. @jeanneboyarsky
    Case Insensitive
    46

    View Slide

  47. @jeanneboyarsky
    Case Insensitive
    47

    View Slide

  48. @jeanneboyarsky
    Multi Line Replace
    48
    (?m)
    Also $1
    a
    b
    c

    View Slide

  49. @jeanneboyarsky
    Dot All
    49
    (?s)




    View Slide

  50. @jeanneboyarsky
    Pattern Idiom
    50

    View Slide

  51. @jeanneboyarsky
    Pattern Replace
    51

    View Slide

  52. @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?
    52

    View Slide

  53. @jeanneboyarsky
    Lab & Break
    53
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  54. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    54

    View Slide

  55. @jeanneboyarsky
    Lab Review
    • Any questions?
    • Did you like replaceAll() or
    Pattern better?
    • What was hardest?
    55

    View Slide

  56. @jeanneboyarsky
    Create a Set
    56
    Java 9

    View Slide

  57. @jeanneboyarsky
    Create a List
    57
    Java 9

    View Slide

  58. @jeanneboyarsky
    Mutable List
    58
    Java 9

    View Slide

  59. @jeanneboyarsky
    Mutable Copy
    59

    View Slide

  60. @jeanneboyarsky
    Immutable Copy
    60
    Java 10

    View Slide

  61. @jeanneboyarsky
    Synchronized View
    61

    View Slide

  62. @jeanneboyarsky
    Unmodifiable View
    62

    View Slide

  63. @jeanneboyarsky
    Create a Map
    63

    View Slide

  64. @jeanneboyarsky
    Create a Map
    64
    Java 9

    View Slide

  65. @jeanneboyarsky
    Sorting
    65

    View Slide

  66. @jeanneboyarsky
    Sorting
    66

    View Slide

  67. @jeanneboyarsky
    Removing from a List
    67

    View Slide

  68. @jeanneboyarsky
    Element in List
    68

    View Slide

  69. @jeanneboyarsky
    Replacing
    69

    View Slide

  70. @jeanneboyarsky
    Venn Diagram?
    70

    View Slide

  71. @jeanneboyarsky
    Defaulting
    71

    View Slide

  72. @jeanneboyarsky
    Add to map if not present
    72

    View Slide

  73. @jeanneboyarsky
    Other Useful Map Methods
    • containsKey(), containsValue()
    • entrySet()
    • keySet(), values()
    • remove(key)
    • remove(key, value)
    • replace(key, value)
    • replace(key, oldValue, newValue)
    73

    View Slide

  74. @jeanneboyarsky
    Printing
    74

    View Slide

  75. @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
    75

    View Slide

  76. @jeanneboyarsky
    Lab & Break
    76
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  77. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    77

    View Slide

  78. @jeanneboyarsky
    Lab Review
    • Did you get them all?
    • Which was your longest
    solution?
    • How many ways did you get the
    challenge question?
    78

    View Slide

  79. @jeanneboyarsky
    Optional
    79
    Optional.empty() Optional.of(95)
    95

    View Slide

  80. @jeanneboyarsky
    Stream Flow
    80
    Intermediate
    Operations
    Source Terminal Operation

    View Slide

  81. @jeanneboyarsky
    Painting Example
    81
    1 2 3
    4 5
    6
    Take sign
    out of box
    Put sign
    in pile
    Intermediate
    Operations
    Paint sign

    View Slide

  82. @jeanneboyarsky
    Painting Example
    82
    Take sign
    out of box
    Put sign
    in pile
    Intermediate
    Operations
    Only do 2 signs
    Paint sign

    View Slide

  83. @jeanneboyarsky
    Painting Example
    83
    stream() forEach()
    Intermediate
    Operations
    filter() limit()
    sorted()

    View Slide

  84. @jeanneboyarsky
    Basic Stream
    84

    View Slide

  85. @jeanneboyarsky
    Find max
    85

    View Slide

  86. @jeanneboyarsky
    Join a String
    86

    View Slide

  87. @jeanneboyarsky
    Get First Elements
    87
    Java 9

    View Slide

  88. @jeanneboyarsky
    Get element
    88
    Java 9

    View Slide

  89. @jeanneboyarsky
    Strings
    89

    View Slide

  90. @jeanneboyarsky
    Useful Terminal Operations
    • count()
    • collect() - more on this next module
    • allMatch(), anyMatch(), noneMatch()
    • findFirst(), findAny()
    • min()/max()
    • forEach()
    90

    View Slide

  91. @jeanneboyarsky
    Useful Intermediate Operations
    • filter()
    • map()
    • distinct()
    • limit(), skip()
    • sorted()
    • peek()
    91

    View Slide

  92. @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?
    92

    View Slide

  93. @jeanneboyarsky
    Lab & Break
    93
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  94. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    94

    View Slide

  95. @jeanneboyarsky
    Lab Review
    • Did you get them all?
    • Which was your longest
    solution?
    • What did you come up with for
    takeWhile() or dropWhile()?
    95

    View Slide

  96. @jeanneboyarsky
    Infinite: Generating
    96

    View Slide

  97. @jeanneboyarsky
    Infinite: Iterative
    97
    Java 9

    View Slide

  98. @jeanneboyarsky
    FlatMap
    98

    View Slide

  99. @jeanneboyarsky
    Creating Map
    99

    View Slide

  100. @jeanneboyarsky
    Creating Map With Conflict
    100

    View Slide

  101. @jeanneboyarsky
    Grouping By Key
    101

    View Slide

  102. @jeanneboyarsky
    Partitioning By Key
    102

    View Slide

  103. @jeanneboyarsky
    Grouping By Key
    103

    View Slide

  104. @jeanneboyarsky
    Reduce
    104

    View Slide

  105. @jeanneboyarsky
    Review
    • Sources
    • generate()
    • iterate()
    • Intermediate
    • flatMap()
    • Terminal
    • reduce()
    • Collectors
    • toMap()
    • groupingBy()
    • partitioningBy()
    105

    View Slide

  106. @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?
    106

    View Slide

  107. @jeanneboyarsky
    Lab & Break
    107
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  108. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    108

    View Slide

  109. @jeanneboyarsky
    Lab Review
    • Did you get them all?
    • How did you solve the challenge
    problem?
    • Did you see how different
    methods are useful for
    different problems?
    109

    View Slide

  110. @jeanneboyarsky
    Creating a Path
    110

    View Slide

  111. @jeanneboyarsky
    Absolute Path
    111

    View Slide

  112. @jeanneboyarsky
    Separators
    112
    Windows:
    \\ and ;
    Mac/Linux:
    / and :

    View Slide

  113. @jeanneboyarsky
    Read File into String
    113
    Java 11

    View Slide

  114. @jeanneboyarsky
    Read File into List
    114

    View Slide

  115. @jeanneboyarsky
    Read File with Stream
    115

    View Slide

  116. @jeanneboyarsky
    Write File from String
    116
    Java 11

    View Slide

  117. @jeanneboyarsky
    Write File from List
    117

    View Slide

  118. @jeanneboyarsky
    Append To File
    118

    View Slide

  119. @jeanneboyarsky
    Walk Tree
    119

    View Slide

  120. @jeanneboyarsky
    Convert to unchecked
    120
    Or
    unchecked
    wrapper

    View Slide

  121. @jeanneboyarsky
    Delete File
    121

    View Slide

  122. @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?
    122

    View Slide

  123. @jeanneboyarsky
    Lab & Break
    123
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide

  124. @jeanneboyarsky
    Agenda
    • Strings
    • Regular Expressions
    • Collections
    • Streams
    • Advanced Streams
    • File I/O
    • Other Useful APIs
    124

    View Slide

  125. @jeanneboyarsky
    Lab Review
    • Did you get them all?
    • How did you solve the challenge
    problem?
    • Which seemed easier - reading
    all lines or lines() for the odd/
    even problem?
    125

    View Slide

  126. @jeanneboyarsky
    Math
    126

    View Slide

  127. @jeanneboyarsky
    Math
    127

    View Slide

  128. @jeanneboyarsky
    Random
    128

    View Slide

  129. @jeanneboyarsky
    Random
    129

    View Slide

  130. @jeanneboyarsky
    Random
    130

    View Slide

  131. @jeanneboyarsky
    Random
    131

    View Slide

  132. @jeanneboyarsky
    LocalDate
    132

    View Slide

  133. @jeanneboyarsky
    LocalDate
    133

    View Slide

  134. @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?
    134

    View Slide

  135. @jeanneboyarsky
    Before the last lab:
    Tomorrow 10:20am
    Java: Did you know that?
    + Win a book
    135

    View Slide

  136. @jeanneboyarsky
    Lab & Break
    136
    https://github.com/boyarsky/2020-
    devnexus-java-idioms

    View Slide