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

Cleanest way to create a Guava Multimap from a Java 8 stream

You can just use the Guava Multimaps factory: ImmutableMultimap<String, Foo> foosById = Multimaps.index(foos, Foo::getId); or wrap a call to Multimaps.index with a Collector<T, A, R> interface (shown below, in an unoptimized naive implementation). Multimap<String, Foo> collect = foos.stream() .collect(MultimapCollector.toMultimap(Foo::getId)); and the Collector: public class MultimapCollector<T, K, V> implements Collector<T, Multimap<K, V>, Multimap<K, V>> { private … Read more

Java 8 Collectors.toMap SortedMap

I don’t think you can get much better than this: .collect(Collectors.toMap(keyMapper, valueMapper, (v1,v2) ->{ throw new RuntimeException(String.format(“Duplicate key for values %s and %s”, v1, v2));}, TreeMap::new)); where the throw lambda is the same as throwingMerger() but I can’t directly call that since it’s package private (you can of course always make your own static method … Read more

How to split a String into a Stream of Strings?

Arrays.stream/String.split Since String.split returns an array String[], I always recommend Arrays.stream as the canonical idiom for streaming over an array. String input = “dog,cat,bird”; Stream<String> stream = Arrays.stream(input.split( “,” )); stream.forEach(System.out::println); Stream.of/String.split Stream.of is a varargs method which just happens to accept an array, due to the fact that varargs methods are implemented via arrays … Read more

Stream Way to get index of first element matching boolean

Occasionally there is no pythonic zipWithIndex in java. So I came across something like that: OptionalInt indexOpt = IntStream.range(0, users.size()) .filter(i -> searchName.equals(users.get(i))) .findFirst(); Alternatively you can use zipWithIndex from protonpack library Note That solution may be time-consuming if users.get is not constant time operation.

Collectors.toMap() keyMapper — more succinct expression?

You can use a lambda: Collectors.toMap(p -> p.getLast(), Function.identity()) or, more concisely, you can use a method reference using ::: Collectors.toMap(Person::getLast, Function.identity()) and instead of Function.identity, you can simply use the equivalent lambda: Collectors.toMap(Person::getLast, p -> p) If you use Netbeans you should get hints whenever an anonymous class can be replaced by a lambda.

Is there any way to reuse a Stream? [duplicate]

If you want to have the effect of reusing a stream, you might wrap the stream expression in a Supplier and call myStreamSupplier.get() whenever you want a fresh one. For example: Supplier<Stream<String>> sup = () -> someList.stream(); List<String> nonEmptyStrings = sup.get().filter(s -> !s.isEmpty()).collect(Collectors.toList()); Set<String> uniqueStrings = sup.get().collect(Collectors.toSet());

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)