java-stream

How collectors are used when turning the stream in parallel

我怕爱的太早我们不能终老 提交于 2019-12-03 12:26:00
I actually tried to answer this question How to skip even lines of a Stream<String> obtained from the Files.lines . So I though this collector wouldn't work well in parallel: private static Collector<String, ?, List<String>> oddLines() { int[] counter = {1}; return Collector.of(ArrayList::new, (l, line) -> { if (counter[0] % 2 == 1) l.add(line); counter[0]++; }, (l1, l2) -> { l1.addAll(l2); return l1; }); } but it works. EDIT: It didn't actually work; I got fooled by the fact that my input set was too small to trigger any parallelism; see discussion in comments . I thought it wouldn't work

Builder pattern with a Java 8 Stream

て烟熏妆下的殇ゞ 提交于 2019-12-03 12:25:11
I am building an object with a simple loop: WebTarget target = getClient().target(u); for (Entry<String, String> queryParam : queryParams.entrySet()) { target = target.queryParam(queryParam.getKey(), queryParam.getValue()); } I want to do the same thing using the Java8 Stream API but I cannot figure out how to do it. What makes me struggle is that target is reassigned every time, so a simple .forEach() will not work. I guess I need to use a .collect() or reduce() since I am looking for a single return value but I am lost at the moment! aioobe There's unfortunately no foldLeft method in the

Java 8 stream short-circuit

℡╲_俬逩灬. 提交于 2019-12-03 12:06:16
问题 Reading up a bit on Java 8, I got to this blog post explaining a bit about streams and reduction of them, and when it would be possible to short-circuit the reduction. At the bottom it states: Note in the case of findFirst or findAny we only need the first value which matches the predicate (although findAny is not guaranteed to return the first). However if the stream has no ordering then we’d expect findFirst to behave like findAny . The operations allMatch , noneMatch and anyMatch may not

Grouping by List of Map in Java 8

守給你的承諾、 提交于 2019-12-03 11:52:20
I have a List like this: List<Map<String, Long>> Is there a way, using lambda, to convert this list to: Map<String, List<Long>> Example: Map<String, Long> m1 = new HashMap<>(); m1.put("A", 1); m1.put("B", 100); Map<String, Long> m2 = new HashMap<>(); m2.put("A", 10); m2.put("B", 20); m2.put("C", 100); List<Map<String, Long>> beforeFormatting = new ArrayList<>(); beforeFormatting.add(m1); beforeFormatting.add(m2); After formatting: Map<String, List<Long>> afterFormatting; which would look like: A -> [1, 10] B -> [100, 20] C -> [100] You need to flatMap the entry set of each Map to create a

How to convert Map to List in Java 8

♀尐吖头ヾ 提交于 2019-12-03 11:40:11
问题 How to convert a Map<String, Double> to List<Pair<String, Double>> in Java 8? I wrote this implementation, but it is not efficient Map<String, Double> implicitDataSum = new ConcurrentHashMap<>(); //.... List<Pair<String, Double>> mostRelevantTitles = new ArrayList<>(); implicitDataSum.entrySet().stream(). .sorted(Comparator.comparing(e -> -e.getValue())) .forEachOrdered(e -> mostRelevantTitles.add(new Pair<>(e.getKey(), e.getValue()))); return mostRelevantTitles; I know that it should works

Why Java 8 Stream interface does not have min() no-parameter version?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 11:31:05
java.util.stream.Stream interface has two versions of sorted method – sorted() which sorts elements in natural order and sorted(Comparator) . Why min() method was not introduced to Stream interface, which would return minimal element from natural-ordering point of view? Holger It should be clear that for min , max , and sorted , adding a method to Stream that does not require a comparator introduces a way to lose the generic type safety. The reason is that the current version of the Java language does not support restricting methods to instances of a specific parameterization, i.e. limit them

Will parallel stream work fine with distinct operation?

依然范特西╮ 提交于 2019-12-03 11:20:08
I was reading about statelessness and came across this in doc : Stream pipeline results may be nondeterministic or incorrect if the behavioral parameters to the stream operations are stateful. A stateful lambda (or other object implementing the appropriate functional interface) is one whose result depends on any state which might change during the execution of the stream pipeline. Now if I have the a list of string ( strList say) and then trying to remove duplicate strings from it using parallel streams in the following way: List<String> resultOne = strList.parallelStream().distinct().collect

How does Java 8 mapToInt ( mapToInt(e -> e) )improves performance exactly?

五迷三道 提交于 2019-12-03 11:19:22
问题 I'm reading the book "Java 8 Lambdas", and at some point the author says "It’s a good idea to use the primitive specialized functions wherever possible because of the performance benefits.". He is referring here to mapToInt, mapToLong, etc. The thing is I don't know where the performance comes from to be honest. Let's consider an example: // Consider this a very very long list, with a lot of elements List<Integer> list = Arrays.asList(1, 2, 3, 4); //sum it, flavour 1 int sum1 = list.stream()

Converting a text file to Map<String, List<String>> using lambda

谁说我不能喝 提交于 2019-12-03 10:58:43
I am trying to convert the following text input file: A=groupA1 A=groupA2 A=groupA3 B=groupB1 B=groupB2 into Map<String, List<String>> by splitting each line on "=" So far I manged to get this sort of output: KEY: A VALUE: A=groupA1 VALUE: A=groupA2 VALUE: A=groupA3 KEY: B VALUE: B=groupB1 VALUE: B=groupB2 using such code: File reqFile = new File("test.config"); try (Stream<String> stream = Files.lines(reqFile.toPath())) { Map<String, List<String>> conf = stream.collect(Collectors.groupingBy(s -> s.split("=")[0])); for (Map.Entry<String, List<String>> entry: conf.entrySet()) { System.out

Stack using the Java 8 collection streaming API

橙三吉。 提交于 2019-12-03 10:54:54
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: " + list); The output I get is: Collected: [A, B, C] Why isn't it outputing them in the expected LIFO order