Why is the max recursion depth I can reach non-deterministic?

The observed behavior is affected by the HotSpot optimizer, however it is not the only cause. When I run the following code public static void main(String[] argv) { System.out.println(System.getProperty(“java.version”)); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); System.out.println(countDepth()); } static int countDepth() { try { return 1+countDepth(); } catch(StackOverflowError err) { return 0; } } with JIT … Read more

Default methods and interfaces extending other interfaces

This is exactly addressed by the JLS in 15.12.3. “Compile-Time Step 3: Is the Chosen Method Appropriate?”. If the form is TypeName . super . [TypeArguments] Identifier, then: […] If TypeName denotes an interface, let T be the type declaration immediately enclosing the method invocation. A compile-time error occurs if there exists a method, distinct … Read more

Java 8 Consumer/Function Lambda Ambiguity

This line is definitely ambiguous: doStuff(getPattern(x -> String.valueOf(x))); Reread this from the linked JLS chapter: A lambda expression (§15.27) is potentially compatible with a functional interface type (§9.8) if all of the following are true: The arity of the target type’s function type is the same as the arity of the lambda expression. If the … Read more

Chain of Map method references

Nope, these are the two ways of doing it. Anything else would end up being only less clear. But, since you asked, here are some options. static<T,U,R> Function<T,R> chain( Function<? super T, ? extends U> f1, Function<? super U, ? extends R> f2) { return t -> f2.apply(f1.apply(t)); } stream.map(chain(Status::getUser, User::getId)) Or static<T,R> Function<T,R> func(Function<T,R> … Read more

ExecutorService vs CompletableFuture

Functionally, the two approaches are more or less the same: you submit your tasks for execution; you don’t wait for the result. Technically, however, there are some subtle differences: In the second approach, you didn’t specify an executor, so it will use the common ForkJoinPool. You would have to pass an executor as second argument … Read more

Show progress of Java 8 stream processing

First of all, Streams are not meant to achieve these kind of tasks (as opposed to a classic data structure). If you know already how many elements your stream will be processing you might go with the following option, which is, I repeat, not the goal of streams. Stream<MyData> myStream = readData(); final AtomicInteger loader … Read more

How to perform Stream functions on an Iterable? [duplicate]

My similar question got marked as duplicate, but here is the helper methods I’ve used to avoid some of the boilerplate: public static <T> Stream<T> stream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), false); } public static <T> Stream<T> parallelStream(Iterable<T> in) { return StreamSupport.stream(in.spliterator(), true); }

Empty methods noticeably slower in Java 11 than Java 8

You are measuring empty benchmarks, not empty methods. In other words, measuring the minimal infrastructure code that handles the benchmark itself. This is easy to dissect, because you’d expect only a few instructions on the hot path. JMH’s -prof perfasm or -prof xperfasm would give you those hottest instructions in seconds. I think the effect … Read more