Skip to content

Latest commit

 

History

History
48 lines (40 loc) · 1.13 KB

File metadata and controls

48 lines (40 loc) · 1.13 KB

Java 8 and Beyond (Study Guide + Examples)

Contents

  • Lambdas and Method References
  • Functional Interfaces
  • Streams (intermediate and terminal ops)
  • Collectors (grouping, partitioning, mapping, reducing)
  • Optional
  • Default/Static methods in interfaces
  • Date/Time (java.time)
  • var (Java 10), Records (16), Sealed Classes (17), Pattern Matching
  • Text Blocks (15), Switch Expressions (14)

Lambdas

Function<Integer,Integer> f = x -> x * x;

Method References

List<String> names = List.of("a","b","c");
names.forEach(System.out::println);

Functional Interfaces

Function, Predicate, Supplier, Consumer, UnaryOperator, BinaryOperator, Comparator, Runnable, Callable.

Streams

Map<Integer, Long> freq = List.of(1,2,2,3,3,3)
    .stream()
    .collect(Collectors.groupingBy(x -> x, Collectors.counting()));

Optional

Optional<String> os = Optional.of("x");
String v = os.map(String::toUpperCase).orElse("NA");

Date/Time

LocalDateTime now = LocalDateTime.now();
ZonedDateTime z = now.atZone(ZoneId.of("UTC"));
Duration d = Duration.between(now, now.plusHours(5));