Why is a combiner needed for reduce method that converts type in java 8

I’m having trouble fully understanding the role that the combiner fulfills in Streams reduce method. For example, the following code doesn’t compile: int length = asList(“str1”, “str2”).stream() .reduce(0, (accumulatedInt, str) -> accumulatedInt + str.length()); Compile error says : (argument mismatch; int cannot be converted to java.lang.String) but this code does compile: int length = asList(“str1”, … Read more

Java 8: Lambda-Streams, Filter by Method with Exception

I have a problem trying out the Lambda expressions of Java 8. Usually it works fine, but now I have methods that throw IOException‘s. It’s best if you look at the following code: class Bank{ …. public Set<String> getActiveAccountNumbers() throws IOException { Stream<Account> s = accounts.values().stream(); s = s.filter(a -> a.isActive()); Stream<String> ss = s.map(a … Read more

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

In Java 8, there is Stream.collect which allows aggregations on collections. In Kotlin, this does not exist in the same way, other than maybe as a collection of extension functions in the stdlib. But it isn’t clear what the equivalences are for different use cases. For example, at the top of the JavaDoc for Collectors … Read more

Java 8 stream reverse order

General question: What’s the proper way to reverse a stream? Assuming that we don’t know what type of elements that stream consists of, what’s the generic way to reverse any stream? Specific question: IntStream provides range method to generate Integers in specific range IntStream.range(-range, 0), now that I want to reverse it switching range from … Read more