java-stream

Java 8: First use of stream() or parallelStream() very slow - Usage in practice meaningful?

时光总嘲笑我的痴心妄想 提交于 2021-02-18 23:03:45
问题 In the last few days I made some test with external iteration, streams and parallelStreams in Java 8 and measured the duration of the execution time. I also read about the warm up time which I have to consider. But one question still remains. The first time when I call the method stream() or parallelStream() on a collection the execution time is higher than it is for an external iteration. I already know, that when I call the stream() or parallelStream() more often on the same collection and

Is the skip() method a short circuiting-operation?

可紊 提交于 2021-02-18 21:12:40
问题 I am reading about Java streams' short-circuiting operations and found in some articles that skip() is a short-circuiting operation. In another article they didn't mention skip() as a short-circuiting operation. Now I am confused; is skip() a short-circuiting operation or not? 回答1: From the java doc under the "Stream operations and pipelines" section : An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result . A terminal

Does BigDecimal#min method qualify as a BinaryOperator?

ぃ、小莉子 提交于 2021-02-18 20:11:43
问题 The Stream.reduce method takes a BinaryOperator as an argument. The function signature of a BinaryOperator is (T,T) -> T . The BigDecimal::min method has only 1 parameter in its method signature (ie. (T) -> T ). Why doesn't the compiler complain when I pass BigDecimal::min to the Stream.reduce method? Sample code: List<BigDecimal> bigDecimalList = new ArrayList<>(); bigDecimalList.add(BigDecimal.valueOf(1)); bigDecimalList.add(BigDecimal.valueOf(2)); bigDecimalList.add(BigDecimal.valueOf(3));

Intermediate operation in Java streams [duplicate]

心不动则不痛 提交于 2021-02-18 19:20:05
问题 This question already has answers here : Java 8 Streams peek api (4 answers) Closed 2 years ago . In java 8, I am using Streams to print the output, but size is coming as 0. Why? public class IntermediateryAndFinal { public static void main(String[] args) { Stream<String> stream = Stream.of("one", "two", "three", "four", "five"); Predicate<String> p1 = Predicate.isEqual("two"); Predicate<String> p2 = Predicate.isEqual("three"); List<String> list = new ArrayList<>(); stream.peek(System.out:

Race condition occurring during use of Parallel Streams and Atomic Variables

不羁岁月 提交于 2021-02-18 18:01:14
问题 When the following piece of code is getting executed I am getting exceptions in a random manner. byte[][] loremIpsumContentArray = new byte[64][]; for (int i = 0; i < loremIpsumContentArray.length; i++) { random.nextBytes(loremIpsumContentArray[i] = new byte[CONTENT_SIZE]); } AtomicBoolean aBoolean = new AtomicBoolean(true); List<Long> resultList = IntStream.range(0, 64* 2) .parallel() .mapToObj(i -> getResult(i, aBoolean, repositoryPath, loremIpsumContentArray )) .collect(Collectors.toList()

Race condition occurring during use of Parallel Streams and Atomic Variables

孤人 提交于 2021-02-18 17:59:25
问题 When the following piece of code is getting executed I am getting exceptions in a random manner. byte[][] loremIpsumContentArray = new byte[64][]; for (int i = 0; i < loremIpsumContentArray.length; i++) { random.nextBytes(loremIpsumContentArray[i] = new byte[CONTENT_SIZE]); } AtomicBoolean aBoolean = new AtomicBoolean(true); List<Long> resultList = IntStream.range(0, 64* 2) .parallel() .mapToObj(i -> getResult(i, aBoolean, repositoryPath, loremIpsumContentArray )) .collect(Collectors.toList()

How to groupBy object properties and map to another object using Java 8 Streams?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-18 09:53:09
问题 Suppose I have a group of bumper cars, which have a size, a color and an identifier ("car code") on their sides. class BumperCar { int size; String color; String carCode; } Now I need to map the bumper cars to a List of DistGroup objects, which each contains the properties size , color and a List of car codes. class DistGroup { int size; Color color; List<String> carCodes; void addCarCodes(List<String> carCodes) { this.carCodes.addAll(carCodes); } } For example, [ BumperCar(size=3, color

IntStream leads to array elements being wrongly set to 0 (JVM Bug, Java 11)

爷,独闯天下 提交于 2021-02-18 03:52:32
问题 In the class P below, the method test seems to return identically false : import java.util.function.IntPredicate; import java.util.stream.IntStream; public class P implements IntPredicate { private final static int SIZE = 33; @Override public boolean test(int seed) { int[] state = new int[SIZE]; state[0] = seed; for (int i = 1; i < SIZE; i++) { state[i] = state[i - 1]; } return seed != state[SIZE - 1]; } public static void main(String[] args) { long count = IntStream.range(0, 0x0010_0000)

Java 8 collecting the list that is already present in object

醉酒当歌 提交于 2021-02-17 03:44:07
问题 I was just searching for better way to handle this scenario using java 8 streams. Object A has list of object b. What I get is a list of object A (List). I need to stream through list of object A and get all the listB's in each of the object A as a one single list. class A { List<B> listB } I have tried the below way it throws compilation List<A> as = someObject.getAs(); List<B> listofBs = as.stream().map(in -> in.getListB()).collect(Collectors.toList()); 回答1: To get a single list of all B's,

Aggregate List<String> into HashMap<String, T> using Stream API

北慕城南 提交于 2021-02-16 20:56:51
问题 I have a MultivaluedMap and a list of strings, and I would like to see which of those string are keys in the MultivaluedMap . For each of the strings that are keys in the MultivaluedMap , I want to construct a new Thing out of the value of that key, set that string as a new key in a new HashMap<String, Thing> , and set the new Thing I've created as the value for that new key in the HashMap . Right now, using a vanilla forEach , I have the following working solution: MultivaluedMap<String,