java 8 in anger – tricia gee – qcon

This is part of my live blogging from QCon 2015. See my QCon table of contents for other posts.

This session was from the Java Sig. It’s a repeat of the conference session I didn’t attend then since I knew I’d see it here.

Java 8 has lots of new features, but people think of streams/lambdas. This session isn’t slides. It’s building a real app live. Trisha starts with an architecture diagram – small serivces and MVC layer. They aren’t microservices because they aren’t reliable. Then she has a sketch of the screen.

The code is on github.. The master branch is the whole app. There’s also a skeleton branch if you want to try it/play along. The page for this presentation hasĀ slides and video.

The Java 8 features I see go by:

  • Stub Service uses a Supplier for generating random numbers.
  • JavaFX for basic UI. [I’m not familiar with JavaFX.]
  • Compute if absent and method reference:
    map.computeIfAbsent("key", TwitterUser::new);
  • Streams, comparators
    map.values().stream()
    .sorted(Comparators.comparing(TwitterUser::getTweets)
    .reversed()).limit(10).collect(Collectors.toList();
    
  • JavaFX
    laterPlatform.runLater(() -> items.setAll(x));
    
  • JavaFX comes with built in animation
  • Date/time to get current minute
    LocalDateTime.now().getMinute();
    
  • Loop from to y
    IntStream.range(0, 10).forEach(this::method);
    

    This coud be better or worse than a regular loop in terms of readability or performance.

  • Read fileStream
    lines = Files.lines(path);
    
  • Filter:
    lines.filter(s -> !s.equals("Ok"));
    
  • Check on stream:
    lines.peek(s -> method())
    
  • Convert array to stream, map
    functionStream.of(arr).map(String:toLowerCase)
    
  • Join strings with delimiter in between
    stream.collection(joining(",")
    

Intellij

  • automatically shortens to simplest thing that could work making code a method reference or lambda.
  • Keyboard shortcut to use appropriate functional interface. Complicated shortcut, but nice function

Leave a Reply

Your email address will not be published. Required fields are marked *