java-stream

Peek() really to see the elements as they flow past a certain point in a pipeline

╄→гoц情女王★ 提交于 2020-01-13 05:31:09
问题 My problem in most simple expressible way: According to JavaDoc : Peek() method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline. I have a pipe of 10 Meters and at the distance of 3 and 7 meter from input head i have two markers [aka peek() ] for checking/debugging my elements. Now from Input end i am giving input of 1,2,3,4,5 . At the point x = 4 meter , i have a filter() which filters all elements less than and equal to 3

Why is `parallelStream` faster than the `CompletableFuture` implementation?

倾然丶 夕夏残阳落幕 提交于 2020-01-12 18:48:32
问题 I wanted to increase the performance of my backend REST API on a certain operation that polled multiple different external APIs sequentially and collected their responses and flattened them all into a single list of responses. Having just recently learned about CompletableFuture s, I decided to give it a go, and compare that solution with the one that involved simply changing my stream for a parallelStream . Here is the code used for the benchmark-test: package com.alithya.platon; import java

Java8 internal iteration

可紊 提交于 2020-01-12 18:47:19
问题 Does java8 forEach method use an iterator or not really? I google it to the bone, could not find it precisely. Only the fact that it will iterate in the same order the data are. Any tips? 回答1: The default implementation of Iterable#forEach is based on a iterator. default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } But in ArrayList is overridden to this, and not uses the iterator, it uses a for loop over its internal array

Map values in Collectors.groupingBy()

旧城冷巷雨未停 提交于 2020-01-12 13:43:08
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Map values in Collectors.groupingBy()

只愿长相守 提交于 2020-01-12 13:42:49
问题 For the sake of this example, let's assume I have a simple type Tuple with two attributes: interface Tuple<T, U> { T getFirst(); U getSecond(); } Now I want to transform a collection of (first, second) tuples into a map which maps each first value to a set of all second values contained in tuples with that specific first value. The method groupSecondByFirst() shows a possible implementation doing what I want: <T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) { Map<T, Set<U>>

Solve no final variable inside Java 8 Stream

萝らか妹 提交于 2020-01-12 09:58:10
问题 Is there is a way to convert the following code to Java 8 Stream. final List ret = new ArrayList(values.size()); double tmp = startPrice; for (final Iterator it = values.iterator(); it.hasNext();) { final DiscountValue discountValue = ((DiscountValue) it.next()).apply(quantity, tmp, digits, currencyIsoCode); tmp -= discountValue.getAppliedValue(); ret.add(discountValue); } Java 8 streams complains about no final variable tmp ? Is there a way to solve such situations ? Local variable tmp

Catching exceptions out of 'stream()' or 'parallelStream()' loses correct values

会有一股神秘感。 提交于 2020-01-12 07:41:18
问题 In the following code, when catching NumberFormatException out of for iteration, the strings in appropriate form appearing in strList before the first bad one (i.e., "illegal_3" ) have been parsed successfully (i.e., "1" and "2" have been parsed as integers 1 and 2 ). public void testCaughtRuntimeExceptionOutOfIteration() { List<String> strList = Stream.of("1", "2", "illegal_3", "4", "illegal_5", "6").collect(Collectors.toList()); List<Integer> intList = new ArrayList<>(); try{ for (String

Stack using the Java 8 collection streaming API

元气小坏坏 提交于 2020-01-12 06:36:08
问题 I have a method which generates an object each time I execute it, and I need to reverse the order with which I am getting them. So I thought the natural way to do it would be a Stack, since it is LIFO. However, the Java Stack does not seem to play well with the new Java 8 streaming API. If I do this: Stack<String> stack = new Stack<String>(); stack.push("A"); stack.push("B"); stack.push("C"); List<String> list = stack.stream().collect(Collectors.toList()); System.out.println("Collected: " +

RxJava vs Java 8 Parallelism Stream

断了今生、忘了曾经 提交于 2020-01-11 15:47:11
问题 What are all the similarities and diferences between them, It looks like Java Parallel Stream has some of the element available in RXJava, is that right? 回答1: Rx is an API for creating and processing observable sequences. The Streams API is for processing iterable sequences. Rx sequences are push-based ; you are notified when an element is available. A Stream is pull-based ; it "asks" for items to process. They may appear similar because they both support similar operators/transforms, but the

Stream - Collect by property and max

时间秒杀一切 提交于 2020-01-11 08:54:07
问题 Problem Statement Given the following class (simplified for the question): public static class Match { private final String type; private final int score; public Match(String type, int score) { this.type = type; this.score = score; } public String getType() { return type; } public int getScore() { return score; } } I have a Stream<Match> that contains multiple instances of the class, the same type appears multiple times, but with different scores: Stream.of(new Match("A", 1), new Match("A", 2