How to get multiple values from an object using a single stream operation?

JDK 12 and above has Collectors.teeing (webrev and CSR), which collects to two different collectors and then merges both partial results into a final result. You could use it here to collect to two IntSummaryStatistics for both the x coordinate and the y coordinate: List<IntSummaryStatistics> stats = points.stream() .collect(Collectors.teeing( Collectors.mapping(p -> p.x, Collectors.summarizingInt()), Collectors.mapping(p -> … Read more

Group sequences of values

Unfortunately, the Stream API is not very well suited to tackle problems that involve dependant operations on the Stream element, like this one. However, you can use the StreamEx library for this: public static void main(String[] args) { IntStream seq = IntStream.of(1, 2, 3, -1, -1, 1, 2, 1, 2); IntUnaryOperator next = i -> … Read more

Visualization of Java Stream parallelization

Current Stream API implementation uses collector combiner to combine the intermediate results in exactly the same way as they were previously split. Also the splitting strategy depends on the source and common pool parallelism level, but does not depend on exact reduction operation used (the same for reduce, collect, forEach, count, etc.). Relying on this … Read more

Nested Java 8 parallel forEach loop perform poor. Is this behavior expected?

The problem is that the rather limited parallelism you have configured is eaten up by the outer stream processing: if you say that you want eight threads and process a stream of more than eight items with parallel() it will create eight worker threads and let them process items. Then within your consumer you are … Read more

Does collect operation on Stream close the stream and underlying resources?

There is a trick to make the Stream implementation calling close() after the terminal operation: List<String> rows = Stream.of(Files.lines(inputFilePath)).flatMap(s->s) .collect(Collectors.toList()); It simply creates a stream encapsulating the stream of lines as a single item and uses flatMap with an identity function (Function.identity() would work as well) to turn it into a stream of lines again. … Read more

java streams in Java 7

In the official API, no. There is no more public updates for Java 7. If you’re a customer, you might still get minor updates, but this not (or very very very unlikely) for back-porting the Stream API. With a bit of digging you may look at StreamSupport. I’ve never tested it but apparently its goal … Read more

How to apply Filtering on groupBy in java streams

You can make use of the Collectors.filtering API introduced since Java-9 for this: Map<String, List<Employee>> output = list.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.filtering(e -> e.getSalary() > 2000, Collectors.toList()))); Important from the API note : The filtering() collectors are most useful when used in a multi-level reduction, such as downstream of a groupingBy or partitioningBy. A filtering collector differs … Read more

Most elegant way to join a Map to a String in Java 8

You can grab the stream of the map’s entry set, then map each entry to the string representation you want, joining them in a single string using Collectors.joining(CharSequence delimiter). import static java.util.stream.Collectors.joining; String s = attributes.entrySet() .stream() .map(e -> e.getKey()+”=”+e.getValue()) .collect(joining(“&”)); But since the entry’s toString() already output its content in the format key=value, you … Read more