2014/11/14

Devoxx 2014: Conference day 3 Notes (14/11/2014)


50 new things we can do with Java 8

José Paumard
Date
  • Instant.now()
    • Duration.between(start, end).toMillis()
  • LocalDate.now() — .of() —
    • Period: time between 2 dates
    • now.with(TemporalAdjuster.next(DayOfWeek.SUNDAY));
  • LocalTime.now() — of(10,20) //10:20
  • ZoneTime / ZoneId.of(“Europe/London”)
    • ZonedDateTime — Period
  • Formatter (DateTimeFormatter.ISO_DATE_TIME etc.)
  • Bridge with java.util.Date:
    • Date.from(instant)
    • TimeStamp.from(instant)
    • Date.from / toLocalDate()
Stream
  • IntStream
  • Patter.compile(…).splitAsStream(txt)
String concat
  • compiler does stringbuilder conversion
  • StringJoiner: concat with delimeters + pre-/postfix
    • String.join(…)
Numbers
  • Long.max(), Long.sum()
  • lambda: .reduce(…,Long::max)
  • static Long.hashCode(l)
I/O reading text files
  • Files.lines(Path)
    • path=Paths.get(…);
    • autoclosable with try(resource){} pattern
  • Files.list(path)
  • Files.walk(path): recursive find
Iterable
  • forEach consumes (e.g; strings.forEacht(System.out::println))
  • boolean b = list.removeIf(s-> s.length() > 4)
  • list.replaceAll(String::toUpperCase)
  • list.sort(Comparator.naturalOrder())
Comparator
  • Comparator.naturalOrder()
  • Comparator.comparingBy(Person::getLastName) .thenComparing(Person::getFirstName).thenComparing(..)
  • comparator.reversedOrder() / comp.reversed()
  • Comparator.nullsFirst() /nullsLast()
Optional
  • Optional.empty();
  • Optional.of(“one”)
  • opt.isPresent() /.get() / .orElse(“”) / .orElseThrow()
  • Stream integration ifPresent / filter / map / flatMap
    • flatMap().collect(…)
Map
  • map.forEach( (key, value) -> …)
  • better Compare-And-Swap functions for concurrency:
    • map.get(key) -> map.getOrDefault(key, default)
    • map.putIfAbsent(key, object)
    • map.replace(key, old, new) -> extra test on old value
    • map.remove(key, old) - extra test on old value
    • map.compute() / computeIfAbsent() / computeIfPresent()
Type Annotations
  • @NonNull
Parallel Arrays
  • Arrays.parallelPrefix
  • parallelSort
Completable Future -> graphs of tasks (“reactive programming”)
  • CompletableFuture.supplyAsync()
  • thenApply() thenAccept()
  • thenCompose()
  • allof(): combine multiple futures -> join()
  • thenCombine() / applyToEither: get the first result
Atomic Variables
  • AtomicLong -> updateAndGet(lambda) / accumulateAndGet(lambda)
  • multithreaded ;increment() / .sum() computes final result
StampedLock
  • s.writeLock() s.readLock()
  • tryOptimisticRead()
ConcurrentHashMap
  • completely replaced, no locking
  • size(): do not use! -> map.mappingCount()
  • search(threshold, lambda) : threshold before going parallel
  • reduce() / reduceKey() / reduceEntries()
  • ConcurrentHashMap.newKeySet() // values are Boolean.TRUE
Books
  • Java SE8 for the Really Impatient - Cay Horstman

Introduction to Android Wear - A Glimpse Into the Future

Cyril Mottier
principles:
  • contextual
  • glanceable
  • low interaction

In Full Flow: Java 8 Lambdas in the Stream

Paul Sandoz
Stream Basics
  • Stream:
    • abstract view over data
    • aggregate ops
  • not RxJava, not CompletableFuture, not Distributed Stream
  • e.g.;
    • list.stream().filter(s -> s.length() > 0).count()
    • mapToLong(e -> 1L)
    • reduce(0, Long::sum);
    • reduce(“”, (a, b) -> a + “ “ + b)
    • Optional
  • performance:
    • parallel can go slower for small datasets (due to overhead)
    • reduce can generate a lot of extra classes -> .collect()
Parallel Game
  • a stream is sequential by default
  • can be made parallel()
  • Greedy -> takes more work -> multicore
  • implementation under the hood:
    • forkjoinpool -> split problem in parts
  • when to use?
The future
  • near term:
    • better parallel resource control
    • improved splitting implementations
      • Files.lines()
      • unordered input to limit()
    • more operations:
      • takeWhile()
      • skipWhile()
      • Matcher.stream()
    • helper methods
      • Optional.stream()
      • Stream.ofNullable(T t)
    • longer term:
    • Value types
    • extending generics over values
    • IntStream extends Stream
    • Tuples
Post a Comment