java-stream

Understanding sequential vs parallel stream spliterators in Java 8 and Java 9

给你一囗甜甜゛ 提交于 2020-01-11 06:21:05
问题 A question about spliterators that at first glance is not straightforward. In streams, .parallel() changes the behaviour that the stream is processed. However I was expecting the spliterators created from sequential and parallel streams to be the same. For example, in sequential streams typically, the .trySplit() is never invoked , while in parallel streams it is, in order to hand over the split spliterator to another thread. Differences between stream.spliterator() vs stream.parallel()

Join strings with different last delimiter

£可爱£侵袭症+ 提交于 2020-01-11 02:10:15
问题 Using stream.collect(Collectors.joining(", ")) I can easily join all the strings of my stream delimited by a comma. A possible result would be "a, b, c" . But what if I want the last delimiter to be different. For example to be " and " such that I get "a, b and c" as result. Is there an easy solution? 回答1: If they are already in a list, no stream is needed; simply join a sublist of all but the last element and concat the other delimiter and the final element: int last = list.size() - 1;

Is Stream.count() guranteed to visit each element?

落花浮王杯 提交于 2020-01-10 19:33:30
问题 In other words, is the following line guranteed to print num lines? int num = list.stream().peek(System.out::println).count(); This question was triggered by a discussion in the comments of https://stackoverflow.com/a/41346586/2513200 I vaguely remember a discussion that optimizations that avoid iteration might be legal, but didn't find anything conclusive during a quick search. The JavaDocs for Stream.count contain this statement: This is a special case of a reduction and is equivalent to:

Java 8 Stream - Reduce function's combiner not getting executed [duplicate]

一曲冷凌霜 提交于 2020-01-10 04:35:21
问题 This question already has answers here : Java8 stream.reduce() with 3 parameters - getting transparency (2 answers) Closed 3 years ago . I am using a simple reduce method with three arguments viz. identity, accumulator and combiner. Here is my code... Integer ageSumComb = persons .stream() .reduce(0, (sum, p) -> { System.out.println("Accumulator: Sum= "+ sum + " Person= " + p); return sum += p.age; }, (sum1, sum2) -> { System.out.format("Combiner: Sum1= " + sum1 + " Sum2= "+ sum2); return

arg max in Java 8 streams?

别说谁变了你拦得住时间么 提交于 2020-01-10 01:45:11
问题 I often need the maximum element of a collection according to the maximization of a criterion which produces a double or int value. Streams have the max() function which requires me to implement a comparator, which I find cumbersome. Is there a more concise syntax, such as names.stream().argmax(String::length) in the following example? import java.util.Arrays; import java.util.Comparator; import java.util.List; public class ArgMax { public static void main(String[] args) { List<String> names

Converting Consumers to Functions

萝らか妹 提交于 2020-01-10 01:05:51
问题 Many lambdas for the Function interface take the form t -> { // do something to t return t; } I do this so often that I have written a method for it like this. static <T> Function<T, T> consumeThenReturn(Consumer<T> consumer) { return t -> { consumer.accept(t); return t; }; } This enables me to do really nice things like this: IntStream.rangeClosed('A', 'Z') .mapToObj(a -> (char) a) .collect(Collectors.collectingAndThen(Collectors.toList(), consumeThenReturn(Collections::shuffle))) .forEach

.forEach and .sort don't work and cannot set breakpoints in blocks

拥有回忆 提交于 2020-01-09 11:53:14
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

.forEach and .sort don't work and cannot set breakpoints in blocks

馋奶兔 提交于 2020-01-09 11:52:56
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

.forEach and .sort don't work and cannot set breakpoints in blocks

╄→гoц情女王★ 提交于 2020-01-09 11:52:46
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

.forEach and .sort don't work and cannot set breakpoints in blocks

懵懂的女人 提交于 2020-01-09 11:52:14
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");