Java 8 stream to collect a Map of List of items

listOfData.stream() .flatMap(e -> e.entrySet().stream()) .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); update: Slightly different variant to user1692342‘s answer for completeness. list.stream() .map(e -> Arrays.asList(e.get(“Role”), e.get(“Name”))) .collect(Collectors.groupingBy(e -> e.get(0), Collectors.mapping(e -> e.get(1), Collectors.toList())));

Java 8 parallel sorting vs Scala parallel sorting

One can implement a parallel sort in scala e.g. http://blog.yunglinho.com/blog/2013/03/19/parallel-external-merge-sort/ . I don’t know why ParSeq doesn’t offer sorting in its API. Note that there are a number of alternatives to .par, so don’t necessarily assume that your results from ParSeq are representative of Scala parallelism in general. (But benchmarks are valuable and I wish … Read more

How do Java 8 parallel streams behave on a thrown exception?

When an exception is thrown in one of the stages, it does not wait for other operations to finish, the exception is re-thrown to the caller. That is how ForkJoinPool handles that. In contrast findFirst for example when run in parallel, will present the result to the caller only after ALL operations have finished processing … Read more

Why don’t primitive Stream have collect(Collector)?

We did in fact prototype some Collector.OfXxx specializations. What we found — in addition to the obvious annoyance of more specialized types — was that this was not really very useful without having a full complement of primitive-specialized collections (like Trove does, or GS-Collections, but which the JDK does not have). Without an IntArrayList, for … Read more

Java 8 Stream: How to compare current element with next element?

My free StreamEx library allows you to process the pairs of the stream elements using additional pairMap intermediate operation. Like this: StreamEx.of(input).pairMap((current, next) -> doSomethingWith(current, next)); Where input is a Collection, array or Stream. For example, this way you can easily check whether input is sorted: boolean isSorted = StreamEx.of(input) .pairMap((current, next) -> next.compareTo(current)) .allMatch(cmp … Read more

How do I create a Stream of regex matches?

Well, in Java 8, there is Pattern.splitAsStream which will provide a stream of items split by a delimiter pattern but unfortunately no support method for getting a stream of matches. If you are going to implement such a Stream, I recommend implementing Spliterator directly rather than implementing and wrapping an Iterator. You may be more familiar … 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

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