java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

java.util.Arrays.asList() produces a list from which it is impossible to remove elements, so it throws on a removal attempt. You could wrap it with ArrayList: List<Integer> ints = new java.util.ArrayList<>(java.util.Arrays.asList(1,20,20)); Update Arrays.asList() returns return new ArrayList<>(a); where ArrayList is not java.util.ArrayList, but java.util.Arrays.ArrayList (internal class), which does not allow removal.

Non-terminal forEach() in a stream?

Yes there is. It is called peek() (example from the JavaDoc): Stream.of(“one”, “two”, “three”, “four”) .peek(e -> System.out.println(“Original value: ” + e)) .filter(e -> e.length() > 3) .peek(e -> System.out.println(“Filtered value: ” + e)) .map(String::toUpperCase) .peek(e -> System.out.println(“Mapped value: ” + e)) .collect(Collectors.toList());

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

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

Register a Stream “completion” hook

Any solution intercepting the terminal operations except flatMap-based solution (as proposed by @Holger) would be fragile to the following code: Stream<String> stream = getAutoCloseableStream(); if(stream.iterator().hasNext()) { // do something if stream is non-empty } Such usage is absolutely legal by the specification. Do not forget that iterator() and spliterator() are terminal stream operations, but after … Read more

Convert IntStream to Map

Here is my code, it will work for you. Function Reference version public class AppLauncher { public static void main(String a[]){ Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),AppLauncher::computeSmth)); System.out.println(map); } public static Integer computeSmth(Integer i){ return i*i; } } Lambda expression version public class AppLauncher { public static void main(String a[]){ Map<Integer,Integer> map = IntStream.range(1,10).boxed().collect(Collectors.toMap(Function.identity(),i->i*i)); System.out.println(map); } }

tech