Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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
  2. @jeanneboyarsky About Me • Java Champion • Author • Developer

    at NYC bank for 17+ years • FIRST Robotics Mentor 2
  3. @jeanneboyarsky Pause for a Commercial 3 Java 11 certs •1Z0-815

    - Out now •1Z0-816 - April ETA
  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
  5. @jeanneboyarsky Introductions 5

  6. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 6
  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
  8. @jeanneboyarsky Idiom • Recurring construct • Has meaning • Common

    approach 8
  9. @jeanneboyarsky Design Patterns • Typically higher level • May be

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

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

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

    8)
  13. @jeanneboyarsky Example #3 - What is it? 13 Anti-pattern *

    catches “Exception” * ignores exception
  14. @jeanneboyarsky Example #4 - What is it? 14

  15. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 15
  16. @jeanneboyarsky Is there a match? 16 IntelliJ warning!

  17. @jeanneboyarsky 17 Is there a match?

  18. @jeanneboyarsky Only whitespace 18

  19. @jeanneboyarsky Even Better 19 Java 11 Java 11

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

  21. @jeanneboyarsky Equal? 21

  22. @jeanneboyarsky Case Insensitive Equal 22

  23. @jeanneboyarsky Case Insensitive Compare 23

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

  25. @jeanneboyarsky Multiples 25

  26. @jeanneboyarsky How do I unit test this? 26 can edit?

    pass in a mock stub out real System.out Yes No
  27. @jeanneboyarsky Pass Mock object 27

  28. @jeanneboyarsky And Test 28

  29. @jeanneboyarsky If Cannot Change 29

  30. @jeanneboyarsky Formatting • %s - string • %d - int

    • %f - float/double (minimum width/precision) • %n - new line
  31. @jeanneboyarsky Review - What best API? • Check if String

    empty? • Duplicate String? • Merge Strings? • Compare case sensitive strings? • Compare non-case sensitive strings? 31
  32. @jeanneboyarsky Lab 32 Do NOT use streams or lambdas in

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

  34. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 34
  35. @jeanneboyarsky Lab Review • Any questions? • How did you

    solve Tic Tac Toe? 35
  36. @jeanneboyarsky Remove all Numbers 36 Note replaceAll What are two

    ways to remove blanks spaces too?
  37. @jeanneboyarsky Remove leading pattern 37 Three changes: replaceFirst() ^ +

  38. @jeanneboyarsky Remove trailing pattern 38 Watch escaping

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

  40. @jeanneboyarsky Ugly regex 40

  41. @jeanneboyarsky Better regex 41

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

  44. @jeanneboyarsky Contains only digits 44

  45. @jeanneboyarsky Contains any digits 45 More to regex

  46. @jeanneboyarsky Case Insensitive 46

  47. @jeanneboyarsky Case Insensitive 47

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

    c
  49. @jeanneboyarsky Dot All 49 (?s) <form> <input /> <textarea />

    </form>
  50. @jeanneboyarsky Pattern Idiom 50

  51. @jeanneboyarsky Pattern Replace 51

  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
  53. @jeanneboyarsky Lab & Break 53 https://github.com/boyarsky/2020- devnexus-java-idioms

  54. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 54
  55. @jeanneboyarsky Lab Review • Any questions? • Did you like

    replaceAll() or Pattern better? • What was hardest? 55
  56. @jeanneboyarsky Create a Set 56 Java 9

  57. @jeanneboyarsky Create a List 57 Java 9

  58. @jeanneboyarsky Mutable List 58 Java 9

  59. @jeanneboyarsky Mutable Copy 59

  60. @jeanneboyarsky Immutable Copy 60 Java 10

  61. @jeanneboyarsky Synchronized View 61

  62. @jeanneboyarsky Unmodifiable View 62

  63. @jeanneboyarsky Create a Map 63

  64. @jeanneboyarsky Create a Map 64 Java 9

  65. @jeanneboyarsky Sorting 65

  66. @jeanneboyarsky Sorting 66

  67. @jeanneboyarsky Removing from a List 67

  68. @jeanneboyarsky Element in List 68

  69. @jeanneboyarsky Replacing 69

  70. @jeanneboyarsky Venn Diagram? 70

  71. @jeanneboyarsky Defaulting 71

  72. @jeanneboyarsky Add to map if not present 72

  73. @jeanneboyarsky Other Useful Map Methods • containsKey(), containsValue() • entrySet()

    • keySet(), values() • remove(key) • remove(key, value) • replace(key, value) • replace(key, oldValue, newValue) 73
  74. @jeanneboyarsky Printing 74

  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
  76. @jeanneboyarsky Lab & Break 76 https://github.com/boyarsky/2020- devnexus-java-idioms

  77. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 77
  78. @jeanneboyarsky Lab Review • Did you get them all? •

    Which was your longest solution? • How many ways did you get the challenge question? 78
  79. @jeanneboyarsky Optional 79 Optional.empty() Optional.of(95) 95

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

  81. @jeanneboyarsky Painting Example 81 1 2 3 4 5 6

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

    sign in pile Intermediate Operations Only do 2 signs Paint sign
  83. @jeanneboyarsky Painting Example 83 stream() forEach() Intermediate Operations filter() limit()

    sorted()
  84. @jeanneboyarsky Basic Stream 84

  85. @jeanneboyarsky Find max 85

  86. @jeanneboyarsky Join a String 86

  87. @jeanneboyarsky Get First Elements 87 Java 9

  88. @jeanneboyarsky Get element 88 Java 9

  89. @jeanneboyarsky Strings 89

  90. @jeanneboyarsky Useful Terminal Operations • count() • collect() - more

    on this next module • allMatch(), anyMatch(), noneMatch() • findFirst(), findAny() • min()/max() • forEach() 90
  91. @jeanneboyarsky Useful Intermediate Operations • filter() • map() • distinct()

    • limit(), skip() • sorted() • peek() 91
  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
  93. @jeanneboyarsky Lab & Break 93 https://github.com/boyarsky/2020- devnexus-java-idioms

  94. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 94
  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
  96. @jeanneboyarsky Infinite: Generating 96

  97. @jeanneboyarsky Infinite: Iterative 97 Java 9

  98. @jeanneboyarsky FlatMap 98

  99. @jeanneboyarsky Creating Map 99

  100. @jeanneboyarsky Creating Map With Conflict 100

  101. @jeanneboyarsky Grouping By Key 101

  102. @jeanneboyarsky Partitioning By Key 102

  103. @jeanneboyarsky Grouping By Key 103

  104. @jeanneboyarsky Reduce 104

  105. @jeanneboyarsky Review • Sources • generate() • iterate() • Intermediate

    • flatMap() • Terminal • reduce() • Collectors • toMap() • groupingBy() • partitioningBy() 105
  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
  107. @jeanneboyarsky Lab & Break 107 https://github.com/boyarsky/2020- devnexus-java-idioms

  108. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 108
  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
  110. @jeanneboyarsky Creating a Path 110

  111. @jeanneboyarsky Absolute Path 111

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

    :
  113. @jeanneboyarsky Read File into String 113 Java 11

  114. @jeanneboyarsky Read File into List 114

  115. @jeanneboyarsky Read File with Stream 115

  116. @jeanneboyarsky Write File from String 116 Java 11

  117. @jeanneboyarsky Write File from List 117

  118. @jeanneboyarsky Append To File 118

  119. @jeanneboyarsky Walk Tree 119

  120. @jeanneboyarsky Convert to unchecked 120 Or unchecked wrapper

  121. @jeanneboyarsky Delete File 121

  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<String>? 122
  123. @jeanneboyarsky Lab & Break 123 https://github.com/boyarsky/2020- devnexus-java-idioms

  124. @jeanneboyarsky Agenda • Strings • Regular Expressions • Collections •

    Streams • Advanced Streams • File I/O • Other Useful APIs 124
  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
  126. @jeanneboyarsky Math 126

  127. @jeanneboyarsky Math 127

  128. @jeanneboyarsky Random 128

  129. @jeanneboyarsky Random 129

  130. @jeanneboyarsky Random 130

  131. @jeanneboyarsky Random 131

  132. @jeanneboyarsky LocalDate 132

  133. @jeanneboyarsky LocalDate 133

  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
  135. @jeanneboyarsky Before the last lab: Tomorrow 10:20am Java: Did you

    know that? + Win a book 135
  136. @jeanneboyarsky Lab & Break 136 https://github.com/boyarsky/2020- devnexus-java-idioms