Java 8 stream map to list of keys sorted by values

You say you want to sort by value, but you don’t have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort. And you want to get the keys; use map to transform entries to keys. List<Type> types = countByType.entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getValue)) .map(Map.Entry::getKey) .collect(Collectors.toList());

Why StringJoiner when we already have StringBuilder?

StringJoiner is very useful, when you need to join Strings in a Stream. As an example, if you have to following List of Strings: final List<String> strings = Arrays.asList(“Foo”, “Bar”, “Baz”); It is much more simpler to use final String collectJoin = strings.stream().collect(Collectors.joining(“, “)); as it would be with a StringBuilder: final String collectBuilder = … Read more

What’s the difference between ZonedDateTime and OffsetDateTime?

Q: What’s the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: “OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds … Read more

Java 8 Supplier Exception handling with CompletableFuture

The factory methods using the standard functional interfaces aren’t helpful when you want to handle checked exceptions. When you insert code catching the exception into the lambda expression, you have the problem that the catch clause needs the CompletableFuture instance to set the exception while the factory method needs the Supplier, chicken-and-egg. You could use … Read more

Reference to methods with different parameters in Java8

From the Oracle method references tutorial: Reference to an Instance Method of an Arbitrary Object of a Particular Type The following is an example of a reference to an instance method of an arbitrary object of a particular type: String[] stringArray = { “Barbara”, “James”, “Mary”, “John”, “Patricia”, “Robert”, “Michael”, “Linda” }; Arrays.sort(stringArray, String::compareToIgnoreCase); The … Read more

How to combine date and time into a single object?

You just need to use the correct methods, instead of calling constructors. Use parse to create local date and local time objects, then pass the two objects to the of method of LocalDateTime: LocalDate datePart = LocalDate.parse(“2013-01-02”); LocalTime timePart = LocalTime.parse(“04:05:06”); LocalDateTime dt = LocalDateTime.of(datePart, timePart); EDIT Apparently, you need to combine two Date objects … Read more

Drawbacks of javac -parameters flag

The addition of parameter names to the class file format is covered by JEP 118, which was delivered in Java 8. There was a little bit of discussion about why inclusion of parameter names was made optional in OpenJDK email threads here and here. Briefly, the stated reasons to make parameter names optional are concerns … Read more

How to combine 3 or more CompletionStages?

The only way to combine multiple stages that scales well with a growing number of stages, is to use CompletableFuture. If your CompletionStages aren’t CompletableFutures you may still convert them using .toCompletableFuture(): CompletableFuture<A> aCompletionStage = getA().toCompletableFuture(); CompletableFuture<B> bCompletionStage = getB().toCompletableFuture(); CompletableFuture<C> cCompletionStage = getC().toCompletableFuture(); CompletableFuture<D> dCompletionStage = getD().toCompletableFuture(); CompletionStage<Combined> combinedDataCompletionStage = CompletableFuture.allOf( aCompletionStage, bCompletionStage, cCompletionStage, … Read more