java-stream

Nested parallel streams in Java

蹲街弑〆低调 提交于 2021-01-24 08:44:27
问题 I want to understand the ordering constraints between nested streams in Java. Example 1: public static void main(String[] args) { IntStream.range(0, 10).forEach(i -> { System.out.println(i); IntStream.range(0, 10).forEach(j -> { System.out.println(" " + i + " " + j); }); }); } This code executes deterministically, so the inner loop runs forEach on each j before the outer loop runs its own forEach on the next i : 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1

Nested parallel streams in Java

喜你入骨 提交于 2021-01-24 08:43:26
问题 I want to understand the ordering constraints between nested streams in Java. Example 1: public static void main(String[] args) { IntStream.range(0, 10).forEach(i -> { System.out.println(i); IntStream.range(0, 10).forEach(j -> { System.out.println(" " + i + " " + j); }); }); } This code executes deterministically, so the inner loop runs forEach on each j before the outer loop runs its own forEach on the next i : 0 0 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1

Would Stream.toList() perform better than Collectors.toList()

限于喜欢 提交于 2021-01-24 07:03:35
问题 JDK is introducing an API Stream.toList() with JDK-8180352. Here is a benchmarking code that I have attempted to compare its performance with the existing Collectors.toList : @BenchmarkMode(Mode.All) @Fork(1) @State(Scope.Thread) @Warmup(iterations = 20, time = 1, batchSize = 10000) @Measurement(iterations = 20, time = 1, batchSize = 10000) public class CollectorsVsStreamToList { @Benchmark public List<Integer> viaCollectors() { return IntStream.range(1, 1000).boxed().collect(Collectors

Would Stream.toList() perform better than Collectors.toList()

夙愿已清 提交于 2021-01-24 07:01:28
问题 JDK is introducing an API Stream.toList() with JDK-8180352. Here is a benchmarking code that I have attempted to compare its performance with the existing Collectors.toList : @BenchmarkMode(Mode.All) @Fork(1) @State(Scope.Thread) @Warmup(iterations = 20, time = 1, batchSize = 10000) @Measurement(iterations = 20, time = 1, batchSize = 10000) public class CollectorsVsStreamToList { @Benchmark public List<Integer> viaCollectors() { return IntStream.range(1, 1000).boxed().collect(Collectors

Java 8 collect() only isPresent() Optional values [duplicate]

江枫思渺然 提交于 2021-01-21 00:45:34
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Java 8 collect() only isPresent() Optional values [duplicate]

让人想犯罪 __ 提交于 2021-01-21 00:36:49
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Java 8 collect() only isPresent() Optional values [duplicate]

我的未来我决定 提交于 2021-01-21 00:35:42
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . Is there a more elegant way of practically achieving this in Java 8? list.stream() .map(e -> myclass.returnsOptional(e)) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); I'm talking about filter(Optional::isPresent) followed by map(Optional::get) , I want to elegantly collect in a list only Optional results which have a value. 回答1: In your case

Convert Stream to IntStream

只愿长相守 提交于 2021-01-20 21:54:05
问题 I have a feeling I'm missing something here. I found myself doing the following private static int getHighestValue(Map<Character, Integer> countMap) { return countMap.values().stream().mapToInt(Integer::intValue).max().getAsInt(); } My problem is with the silly conversion from Stream to IntStream via the mapToInt(Integer::intValue) Is there a better way of doing the conversion? all this is to avoid using max() from Stream , which requires passing a Comparator but the question is specifically

What is the advantage of IntStream over usual Stream?

最后都变了- 提交于 2021-01-19 06:39:07
问题 How is IntStream , DoubleStream , or LongStream better than regular stream in Java 8? Do these threads have high performance or maybe usability? 回答1: Stream<Integer> etc. have to work with boxed values ( Integer instead of primitive int ) which takes significantly more memory and usually a lot of boxing/unboxing operations (depending on your code). Why only Int/Double/Long ? Just because they were expected to be used most often. Same applies to OptionalInt and friends and all the functional

What is the advantage of IntStream over usual Stream?

让人想犯罪 __ 提交于 2021-01-19 06:39:00
问题 How is IntStream , DoubleStream , or LongStream better than regular stream in Java 8? Do these threads have high performance or maybe usability? 回答1: Stream<Integer> etc. have to work with boxed values ( Integer instead of primitive int ) which takes significantly more memory and usually a lot of boxing/unboxing operations (depending on your code). Why only Int/Double/Long ? Just because they were expected to be used most often. Same applies to OptionalInt and friends and all the functional