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

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 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
  2. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Agenda • Strings and Text Blocks • Regular

    Expressions • Collections & Streams • File I/O + Other Useful APIs 6
  3. 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
  4. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Agenda • Strings and Text Blocks • Regular

    Expressions • Collections & Streams • File I/O + Other Useful APIs 15
  5. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Formatting • %s - string • %d -

    int • %f - float/double (minimum width/precision) • %n - new line
  6. 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
  7. 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
  8. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Agenda • Strings and Text Blocks • Regular

    Expressions • Collections & Streams • File I/O + Other Useful APIs 34
  9. 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
  10. 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
  11. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Agenda • Strings and Text Blocks • Regular

    Expressions • Collections & Streams • File I/O + Other Useful APIs 55
  12. 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
  13. 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
  14. 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
  15. 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
  16. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Review • Sources • generate() • iterate() •

    Intermediate • flatMap() • Terminal • reduce() • Collectors • toMap() • groupingBy() • partitioningBy() 94
  17. 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
  18. twitter.com/jeanneboyarsky mastodon.social/@jeanneboyarsky Agenda • Strings and Text Blocks • Regular

    Expressions • Collections & Streams • File I/O + Other Useful APIs 97
  19. 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
  20. @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)
  21. 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<String>? 113
  22. 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