Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

@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

Slide 5

Slide 5 text

@jeanneboyarsky Introductions 5

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

@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

Slide 8

Slide 8 text

@jeanneboyarsky Idiom • Recurring construct • Has meaning • Common approach 8

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

@jeanneboyarsky Example #4 - What is it? 14

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

@jeanneboyarsky Is there a match? 16 IntelliJ warning!

Slide 17

Slide 17 text

@jeanneboyarsky 17 Is there a match?

Slide 18

Slide 18 text

@jeanneboyarsky Only whitespace 18

Slide 19

Slide 19 text

@jeanneboyarsky Even Better 19 Java 11 Java 11

Slide 20

Slide 20 text

@jeanneboyarsky String Concatentation 20 Best choice changes over time

Slide 21

Slide 21 text

@jeanneboyarsky Equal? 21

Slide 22

Slide 22 text

@jeanneboyarsky Case Insensitive Equal 22

Slide 23

Slide 23 text

@jeanneboyarsky Case Insensitive Compare 23

Slide 24

Slide 24 text

@jeanneboyarsky Creating Csv 24 Better to use Commons CSV Library

Slide 25

Slide 25 text

@jeanneboyarsky Multiples 25

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

@jeanneboyarsky Pass Mock object 27

Slide 28

Slide 28 text

@jeanneboyarsky And Test 28

Slide 29

Slide 29 text

@jeanneboyarsky If Cannot Change 29

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

@jeanneboyarsky Review - What best API? • Check if String empty? • Duplicate String? • Merge Strings? • Compare case sensitive strings? • Compare non-case sensitive strings? 31

Slide 32

Slide 32 text

@jeanneboyarsky Lab 32 Do NOT use streams or lambdas in this lab

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

@jeanneboyarsky Remove trailing pattern 38 Watch escaping

Slide 39

Slide 39 text

@jeanneboyarsky Remove middle 39 Off by one error?

Slide 40

Slide 40 text

@jeanneboyarsky Ugly regex 40

Slide 41

Slide 41 text

@jeanneboyarsky Better regex 41

Slide 42

Slide 42 text

@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

Slide 43

Slide 43 text

@jeanneboyarsky Splitting on a Regex 43

Slide 44

Slide 44 text

@jeanneboyarsky Contains only digits 44

Slide 45

Slide 45 text

@jeanneboyarsky Contains any digits 45 More to regex

Slide 46

Slide 46 text

@jeanneboyarsky Case Insensitive 46

Slide 47

Slide 47 text

@jeanneboyarsky Case Insensitive 47

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

@jeanneboyarsky Dot All 49 (?s)

Slide 50

Slide 50 text

@jeanneboyarsky Pattern Idiom 50

Slide 51

Slide 51 text

@jeanneboyarsky Pattern Replace 51

Slide 52

Slide 52 text

@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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

@jeanneboyarsky Create a Set 56 Java 9

Slide 57

Slide 57 text

@jeanneboyarsky Create a List 57 Java 9

Slide 58

Slide 58 text

@jeanneboyarsky Mutable List 58 Java 9

Slide 59

Slide 59 text

@jeanneboyarsky Mutable Copy 59

Slide 60

Slide 60 text

@jeanneboyarsky Immutable Copy 60 Java 10

Slide 61

Slide 61 text

@jeanneboyarsky Synchronized View 61

Slide 62

Slide 62 text

@jeanneboyarsky Unmodifiable View 62

Slide 63

Slide 63 text

@jeanneboyarsky Create a Map 63

Slide 64

Slide 64 text

@jeanneboyarsky Create a Map 64 Java 9

Slide 65

Slide 65 text

@jeanneboyarsky Sorting 65

Slide 66

Slide 66 text

@jeanneboyarsky Sorting 66

Slide 67

Slide 67 text

@jeanneboyarsky Removing from a List 67

Slide 68

Slide 68 text

@jeanneboyarsky Element in List 68

Slide 69

Slide 69 text

@jeanneboyarsky Replacing 69

Slide 70

Slide 70 text

@jeanneboyarsky Venn Diagram? 70

Slide 71

Slide 71 text

@jeanneboyarsky Defaulting 71

Slide 72

Slide 72 text

@jeanneboyarsky Add to map if not present 72

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

@jeanneboyarsky Printing 74

Slide 75

Slide 75 text

@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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

@jeanneboyarsky Stream Flow 80 Intermediate Operations Source Terminal Operation

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

@jeanneboyarsky Basic Stream 84

Slide 85

Slide 85 text

@jeanneboyarsky Find max 85

Slide 86

Slide 86 text

@jeanneboyarsky Join a String 86

Slide 87

Slide 87 text

@jeanneboyarsky Get First Elements 87 Java 9

Slide 88

Slide 88 text

@jeanneboyarsky Get element 88 Java 9

Slide 89

Slide 89 text

@jeanneboyarsky Strings 89

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

@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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

@jeanneboyarsky Infinite: Generating 96

Slide 97

Slide 97 text

@jeanneboyarsky Infinite: Iterative 97 Java 9

Slide 98

Slide 98 text

@jeanneboyarsky FlatMap 98

Slide 99

Slide 99 text

@jeanneboyarsky Creating Map 99

Slide 100

Slide 100 text

@jeanneboyarsky Creating Map With Conflict 100

Slide 101

Slide 101 text

@jeanneboyarsky Grouping By Key 101

Slide 102

Slide 102 text

@jeanneboyarsky Partitioning By Key 102

Slide 103

Slide 103 text

@jeanneboyarsky Grouping By Key 103

Slide 104

Slide 104 text

@jeanneboyarsky Reduce 104

Slide 105

Slide 105 text

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

Slide 106

Slide 106 text

@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

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

@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

Slide 110

Slide 110 text

@jeanneboyarsky Creating a Path 110

Slide 111

Slide 111 text

@jeanneboyarsky Absolute Path 111

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

@jeanneboyarsky Read File into String 113 Java 11

Slide 114

Slide 114 text

@jeanneboyarsky Read File into List 114

Slide 115

Slide 115 text

@jeanneboyarsky Read File with Stream 115

Slide 116

Slide 116 text

@jeanneboyarsky Write File from String 116 Java 11

Slide 117

Slide 117 text

@jeanneboyarsky Write File from List 117

Slide 118

Slide 118 text

@jeanneboyarsky Append To File 118

Slide 119

Slide 119 text

@jeanneboyarsky Walk Tree 119

Slide 120

Slide 120 text

@jeanneboyarsky Convert to unchecked 120 Or unchecked wrapper

Slide 121

Slide 121 text

@jeanneboyarsky Delete File 121

Slide 122

Slide 122 text

@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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

@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

Slide 126

Slide 126 text

@jeanneboyarsky Math 126

Slide 127

Slide 127 text

@jeanneboyarsky Math 127

Slide 128

Slide 128 text

@jeanneboyarsky Random 128

Slide 129

Slide 129 text

@jeanneboyarsky Random 129

Slide 130

Slide 130 text

@jeanneboyarsky Random 130

Slide 131

Slide 131 text

@jeanneboyarsky Random 131

Slide 132

Slide 132 text

@jeanneboyarsky LocalDate 132

Slide 133

Slide 133 text

@jeanneboyarsky LocalDate 133

Slide 134

Slide 134 text

@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

Slide 135

Slide 135 text

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

Slide 136

Slide 136 text

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