collectors

Complexity of grouping in Java8

时间秒杀一切 提交于 2019-12-05 17:22:26
问题 I would like to learn the time complexity of the given statement below.(In Java8) list.stream().collect(groupingBy(...)); Any idea? 回答1: There is no general answer to that question, as the time complexity depends on all operations. Since the stream has to be processed entirely, there is a base time complexity of O(n) that has to be multiplied by the costs of all operations done per element. This, assuming that the iteration costs itself are not worse than O(n) , which is the case for most

Java 8 streams: conditional Collector

丶灬走出姿态 提交于 2019-12-05 16:23:30
I want to use Java 8 streams to convert a List of String values to a single String. A List of values like "A", "B" should return a String like "Values: 'A', 'B' added". This works fine, however I want to change the Pre- and Postfix depending on the amount of values. For example, if I have a List of only "A" I want the resulting String to be "Value 'A' added". import java.util.stream.Collectors; import java.util.ArrayList; import java.util.List; public class HelloWorld { public static void main(String[] args) { List<String> values = new ArrayList<>(); values.add("A"); values.add("B"); values

Java 9 Collectors.flatMapping rewritten in Java 8

风格不统一 提交于 2019-12-05 14:49:50
问题 I got in touch with a new feature since java-9 called Collectors.flatMapping that takes place as a downstream of grouping or partitioning. Such as (example taken from here): List<List<Integer>> list = Arrays.asList( Arrays.asList(1, 2, 3, 4, 5, 6), Arrays.asList(7, 8, 9, 10)); Map<Integer, List<Integer>> map =list.stream() .collect(Collectors.groupingBy( Collection::size, Collectors.flatMapping( l -> l.stream().filter(i -> i % 2 == 0), Collectors.toList()))); {4=[8, 10], 6=[2, 4, 6]} This is

Limit groupBy in Java 8

余生颓废 提交于 2019-12-05 14:07:06
问题 How can I limit groupBy by each entry? For example (based on this example: stream groupBy): studentClasses.add(new StudentClass("Kumar", 101, "Intro to Web")); studentClasses.add(new StudentClass("White", 102, "Advanced Java")); studentClasses.add(new StudentClass("Kumar", 101, "Intro to Cobol")); studentClasses.add(new StudentClass("White", 101, "Intro to Web")); studentClasses.add(new StudentClass("White", 102, "Advanced Web")); studentClasses.add(new StudentClass("Sargent", 106, "Advanced

Why doesn't Collectors.toList() work on primitive collections?

强颜欢笑 提交于 2019-12-05 05:35:54
(This is probably related to https://stackoverflow.com/a/30312177/160137 , but I'm afraid I still don't get it. So I'm asking my question this way, in the hope that it'll lead to an answer I can more easily understand.) Normally when I have a Stream I can convert it to a collection using one of the static methods in the Collectors class: List<String> strings = Stream.of("this", "is", "a", "list", "of", "strings") .collect(Collectors.toList()); The similar process doesn't work on primitive streams, however, as others have noticed: IntStream.of(3, 1, 4, 1, 5, 9) .collect(Collectors.toList()); //

Creating Map composed of 2 Lists using stream().collect in Java

会有一股神秘感。 提交于 2019-12-05 03:26:22
As for example, there are two lists: List<Double> list1 = Arrays.asList(1.0, 2.0); List<String> list2 = Arrays.asList("one_point_zero", "two_point_zero"); Using Stream, I want to create a map composed of these lists, where list1 is for keys and list2 is for values. To do it, I need to create an auxiliary list: List<Integer> list0 = Arrays.asList(0, 1); Here is the map: Map<Double, String> map2 = list0.stream() .collect(Collectors.toMap(list1::get, list2::get)); list0 is used in order list1::get and list2::get to work. Is there a simpler way without creation of list0? I tried the following code

How to add inner elements of Map when keys are duplicate with Java Stream API

非 Y 不嫁゛ 提交于 2019-12-05 02:43:18
问题 I have a List of List<Map<String, Object>> like this [ {"A": 50, "B": 100, "C": 200, "D": "Auction" }, { "A": 101322143.24, "B": 50243301.2, "C": 569, "D": "Sold Promissory Buyer" }, { "A": 500, "B": 1000, "C": 1500, "D": "Auction" }] I am using this stream API method to convert this list into Map finalSalesReportForSoldProperty.stream().collect(Collectors.toMap(tags -> ((String) tags.get("D")).replaceAll("[\\- ]", ""), Function.identity())); But it throws me java.lang.IllegalStateException:

Java Streams: get values grouped by inner map key

岁酱吖の 提交于 2019-12-04 23:27:50
I have Map<A, Map<B, C>> and I want to get Map<B, List<C>> from it using Java Streams. I try to do it as follows: public <A, B, C> Map<B, List<C>> groupsByInnerKey(Map<A, Map<B, C>> input) { return input.values() .stream() .flatMap(it -> it.entrySet().stream()) .collect(Collectors.groupingBy(Map.Entry::getKey)); } What I expect: flatMap gives a Stream of Map.Entry<B, C> collect(Collectors.groupingBy(...)) takes function which is applied to Map.Entry<B, C> and returns B , thus it collects values of C into List<C> . But it doesn't compile, literally: Non-static method cannot be referenced from a

Group, Collectors, Map (Int to String), Map (Map to Object)

喜你入骨 提交于 2019-12-04 19:53:10
This is a continuation of my previous question at Group, Sum byType then get diff using Java streams . As suggested, I should post as a separate thread instead of updating the original one. So with my previous set of question, I have achieved that, and now, with the continuation. Background: I have the following dataset Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType

How collectors are used when turning the stream in parallel

两盒软妹~` 提交于 2019-12-04 18:57:44
问题 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