Parallel flatMap always sequential

There are two different aspects. First, there is only a single pipeline which is either sequential or parallel. The choice of sequential or parallel at the inner stream is irrelevant. Note that the downstream consumer you see in the cited code snippet represents the entire subsequent stream pipeline, so in your code, ending with .collect(Collectors.toSet());, … Read more

Is there a good way to extract chunks of data from a java 8 stream?

My approach to bulk operations with chunking is to use a partitioning spliterator wrapper, and another wrapper which overrides the default splitting policy (arithmetic progression of batch sizes in increments of 1024) to simple fixed-batch splitting. Use it like this: Stream<OriginalType> existingStream = …; Stream<List<OriginalType>> partitioned = partition(existingStream, 100, 1); partitioned.forEach(chunk -> … process the … Read more

What is the difference between intermediate and terminal operations?

A Stream supports several operations and these operations are divided into intermediate and terminal operations. The distinction between this operations is that an intermediate operation is lazy while a terminal operation is not. When you invoke an intermediate operation on a stream, the operation is not executed immediately. It is executed only when a terminal … Read more

Java 8 stream map to list of keys sorted by values

You say you want to sort by value, but you don’t have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort. And you want to get the keys; use map to transform entries to keys. List<Type> types = countByType.entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getValue)) .map(Map.Entry::getKey) .collect(Collectors.toList());

Get last element of Stream/List in a one-liner

It is possible to get the last element with the method Stream::reduce. The following listing contains a minimal example for the general case: Stream<T> stream = …; // sequential or parallel stream Optional<T> last = stream.reduce((first, second) -> second); This implementations works for all ordered streams (including streams created from Lists). For unordered streams it … Read more

How to extract the dynamic values of the id attributes of the table elements using Selenium and Java

Until you find the element first, you can’t retrieve the attribute values of it. Use findElements method to fetch all links using the following locator table tr td[class=”journalTable-journalPost”] a Then iterate through each element using for-each to fetch id for each element. Sample code: List<WebElement> listOfLinks = driver.findElements(By.cssSelector(“table tr td[class=”journalTable-journalPost”] a”)); for(WebElement link: listOfLinks) { … Read more

Java 8 stream emitting a stream

In Java 9, you could use static final Pattern LINE_WITH_CONTINUATION = Pattern.compile(“(\\V|\\R\\+)+”); … try(Scanner s = new Scanner(file)) { s.findAll(LINE_WITH_CONTINUATION) .map(m -> m.group().replaceAll(“\\R\\+”, “”)) .forEach(System.out::println); } Since Java 8 lacks the Scanner.findAll(Pattern) method, you may add a custom implementation of the operation as a work-around public static Stream<MatchResult> findAll(Scanner s, Pattern pattern) { return StreamSupport.stream(new Spliterators.AbstractSpliterator<MatchResult>( 1000, … Read more

non-interference requirement on Java 8 streams

Well the oracle example is self-explanatory here. First one is this: List<String> l = new ArrayList<>(Arrays.asList(“one”, “two”)); Stream<String> sl = l.stream(); l.add(“three”); String s = l.collect(Collectors.joining(” “)); If you change l by adding one more elements to it before you call the terminal operation (Collectors.joining) you are fine; but notice that the Stream consists of … Read more