Convert java.util.Date to java.time.LocalDate

Short answer Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); Explanation Despite its name, java.util.Date represents an instant on the time-line, not a “date”. The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC). The equivalent class to java.util.Date in JSR-310 is … Read more

How to force max to return ALL maximum values in a Java Stream?

I believe the OP is using a Comparator to partition the input into equivalence classes, and the desired result is a list of members of the equivalence class that is the maximum according to that Comparator. Unfortunately, using int values as a sample problem is a terrible example. All equal int values are fungible, so … Read more

How can I throw CHECKED exceptions from inside Java 8 streams?

The simple answer to your question is: You can’t, at least not directly. And it’s not your fault. Oracle messed it up. They cling on the concept of checked exceptions, but inconsistently forgot to take care of checked exceptions when designing the functional interfaces, streams, lambda etc. That’s all grist to the mill of experts … Read more

When to use: Java 8+ interface default method, vs. abstract method

There’s a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface. The constraint on the default method is that it can be implemented only in the terms … Read more

Why filter() after flatMap() is “not completely” lazy in Java streams?

TL;DR, this has been addressed in JDK-8075939 and fixed in Java 10 (and backported to Java 8 in JDK-8225328). When looking into the implementation (ReferencePipeline.java) we see the method https://stackoverflow.com/questions/29229373/why-filter-after-flatmap-is-not-completely-lazy-in-java-streams @Override final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) { do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink)); } which will be invoke for findFirst operation. The special thing … Read more

What are functional interfaces used for in Java 8?

@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface. But you can use lambdas without this annotation as well as you can override … Read more

Why in Java 8 split sometimes removes empty strings at start of result array?

The behavior of String.split (which calls Pattern.split) changes between Java 7 and Java 8. Documentation Comparing between the documentation of Pattern.split in Java 7 and Java 8, we observe the following clause being added: When there is a positive-width match at the beginning of the input sequence then an empty leading substring is included at … Read more