Skip to content

joeljames/java8-examples

Repository files navigation

java8-examples

Introduction to java8 features. This module provides short and simple examples of lambda expressions, method references, functional interface, new collections API, new maps API, and streams API.

Developer Setup

We use sdkman to manage java versions. Install sdkman by following the instructions here. You could also enable automatically switching to the correct version of java when you cd into this directory. This can be configured by updating the config in ~/.sdkman/etc/config to sdkman_auto_env=true Run the command below to install the configured version of java:

sdk env install

Table of Contents

  • Lambda Expressions
    • Creating a Comparator
    • Creating a Runnable
  • Method References
    • Function
    • BinaryOperator
    • Consumer
    • Supplier
    • Optional
  • New Collections API
    • forEach
    • removeIf
    • replaceAll
    • sort
  • New Maps API
    • getOrDefault
    • putIfAbsent
    • computeIfAbsent
    • sortMap
    • merge
  • Stream Building API
    • Collections to Stream
    • Stream.generate
    • Stream.iterate
    • filter and map
    • findFirst
    • forEach
    • IntStream
      • average, max
      • mapToInt
      • range
    • anyMatch
    • Stream why order matters
    • Reusing stream object
    • Collectors
      • toList
      • groupingBy
      • averagingDouble
      • summarizingDouble
      • joining
      • toMap
      • Create custom collector (Collector.of)
    • flatMap
    • reduce
      • excepts BinaryOperator(accumulator)
      • excepts identity and BinaryOperator(accumulator)
      • excepts identity, BiFunction and accumulator
    • parallelStream
  • Avoiding Null check
  • String
    • join
    • chars
    • regex
    • regex as predicate
  • File IO
    • listing files in a directory
    • listing files of directory and sub directory
    • Files.readAllLines to read files
    • Files.lines to read files as stream
    • Files.newBufferedReader for buffered reading
    • Files.newBufferedWriter for buffered writing
  • Concurrency and Executors
    • CompletableFuture example
    • creating runnable tasks
    • using executors
    • safe stopping of an executor
    • using callables
    • executors InvokeAll
    • executors InvokeAny
    • ScheduleExecutors for running tasks periodically
    • scheduleAtFixedRate
    • scheduleWithFixedDelay
  • Synchronization
    • Problems accessing shared resources with multiple threads simultaneously
    • ReentrantLock
    • Different methods which ReentrantLock locks support
    • ReentrantReadWriteLock
    • StampedLock
    • StampedLock with optimistic locking
    • StampedLock convert read lock to write lock (tryConvertToWriteLock)
    • Semaphores
  • Functional Interface Predicate Example
  • Functional Interface Comparator Example
  • Bytes to String
  • By Reference
  • Graph
  • Java Flight Recorder

Thanks for reading!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages