How can I reverse a Java 8 stream and generate a decrementing IntStream of values?

For the specific question of generating a reverse IntStream, try something like this: static IntStream revRange(int from, int to) { return IntStream.range(from, to) .map(i -> to – i + from – 1); } This avoids boxing and sorting. For the general question of how to reverse a stream of any type, I don’t know of … Read more

Extremely slow parsing of time zone with the new java.time API

As noted in your question and in my comment, ZoneRulesProvider.getAvailableZoneIds() creates a new set of all the available time zones’ string representation (the keys of the static final ConcurrentMap<String, ZoneRulesProvider> ZONES) each time a time zone needs to be parsed.1 Fortunately, a ZoneRulesProvider is an abstract class which is designed to be subclassed. The method … Read more

Iterate two Java-8-Streams together [duplicate]

static <A, B> Stream<Pair<A, B>> zip(Stream<A> as, Stream<B> bs) { Iterator<A> i=as.iterator(); return bs.filter(x->i.hasNext()).map(b->new Pair<>(i.next(), b)); } This does not offer parallel execution but neither did the original zip implementation. And as F. Böller has pointed out it doesn’t work if bs is infinite and as is not. For a solution which works for all … Read more

Java 8 Generics: Reducing a Stream of Consumers to a single Consumer

You use a one-argument Stream.reduce(accumulator) version that has the following signature: Optional<T> reduce(BinaryOperator<T> accumulator); The BinaryOperator<T> accumulator can only accept elements of type T, but you have: <? extends Consumer<? super T>> I propose you to use a three-argument version of the Stream.reduce(…) method instead: <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator … Read more

Sorting a List in parallel without creating a temporary array in Java 8

There doesn’t appear to be any straightforward way to sort a List in parallel in Java 8. I don’t think this is fundamentally difficult; it looks more like an oversight to me. The difficulty with a hypothetical Collections.parallelSort(list, cmp) is that the Collections implementation knows nothing about the list’s implementation or its internal organization. This … Read more

Java 8 poor GUI performance compared to Java 6

According to my profiler, the operation spends most of the time in the method Thread.holdsLock, which can be indeed a costly operation, which is called by Component.checkTreeLock which is called indirectly by Component.updateCursorImmediately. Generally, you can avoid costly visual updates when updating multiple components by calling getContentPane().setVisible(false); right before the operation and getContentPane().setVisible(true); right afterwards, … Read more

How to use a method reference on a static import?

Let’s look at the relevant part of the Java Language Specification, 15.13. Method Reference Expressions. It lists the following ways to a create method reference: MethodReference: ExpressionName :: [TypeArguments] Identifier ReferenceType :: [TypeArguments] Identifier Primary :: [TypeArguments] Identifier super :: [TypeArguments] Identifier TypeName . super :: [TypeArguments] Identifier ClassType :: [TypeArguments] new ArrayType :: new … Read more

Cake pattern with Java8 possible?

With inspiration from other answers I came up with the following (rough) class hierarchy that is similar to the cake pattern in Scala: interface UserRepository { String authenticate(String username, String password); } interface UserRepositoryComponent { UserRepository getUserRepository(); } interface UserServiceComponent extends UserRepositoryComponent { default UserService getUserService() { return new UserService(getUserRepository()); } } class UserService { … Read more

tech