How to add elements of a Java8 stream into an existing List

NOTE: nosid’s answer shows how to add to an existing collection using forEachOrdered(). This is a useful and effective technique for mutating existing collections. My answer addresses why you shouldn’t use a Collector to mutate an existing collection. The short answer is no, at least, not in general, you shouldn’t use a Collector to modify … Read more

What Java 8 Stream.collect equivalents are available in the standard Kotlin library?

There are functions in the Kotlin stdlib for average, count, distinct,filtering, finding, grouping, joining, mapping, min, max, partitioning, slicing, sorting, summing, to/from arrays, to/from lists, to/from maps, union, co-iteration, all the functional paradigms, and more. So you can use those to create little 1-liners and there is no need to use the more complicated syntax … Read more

Java 8 stream reverse order

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

Limit a stream by a predicate

Operations takeWhile and dropWhile have been added to JDK 9. Your example code IntStream .iterate(1, n -> n + 1) .takeWhile(n -> n < 10) .forEach(System.out::println); will behave exactly as you expect it to when compiled and run under JDK 9. JDK 9 has been released. It is available for download here: JDK 9 Releases.

Adding up BigDecimals using Streams

Original answer Yes, this is possible: List<BigDecimal> bdList = new ArrayList<>(); //populate list BigDecimal result = bdList.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); What it does is: Obtain a List<BigDecimal>. Turn it into a Stream<BigDecimal> Call the reduce method. 3.1. We supply an identity value for addition, namely BigDecimal.ZERO. 3.2. We specify the BinaryOperator<BigDecimal>, which adds two BigDecimal‘s, via … Read more

Java 8: How do I work with exception throwing methods in streams?

You need to wrap your method call into another one, where you do not throw checked exceptions. You can still throw anything that is a subclass of RuntimeException. A normal wrapping idiom is something like: private void safeFoo(final A a) { try { a.foo(); } catch (Exception ex) { throw new RuntimeException(ex); } } (Supertype … Read more

Java 8 – Best way to transform a list: map or foreach?

Don’t worry about any performance differences, they’re going to be minimal in this case normally. Method 2 is preferable because it doesn’t require mutating a collection that exists outside the lambda expression. it’s more readable because the different steps that are performed in the collection pipeline are written sequentially: first a filter operation, then a … Read more

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