collectors

Elegantly create map with object fields as key/value from object stream in Java 8

随声附和 提交于 2019-12-04 14:06:52
I have the following class class Person { public String name; public int age; public List<String> hobbies; Person(String name, int age, List<String> hobbies) {this.name = name; this.age = age; this.hobbies = hobbies;} } How do I create a Map of age to hobbies like Map<Integer, Set<String>> ? The Java 8 way I cooked up is: Map<Integer, Set<String>> collect8 = persons.stream() .collect( toMap( p -> p.age, p -> p.hobbies.stream().collect(toSet()), (hobbies1, hobbies2) -> Stream.concat(hobbies1.stream(), hobbies2.stream()).collect(toSet()) ) ); Is there a more idiomatic way of doing this with

Java 8 - Count of words and then arrange in desc order

时光怂恿深爱的人放手 提交于 2019-12-04 14:06:04
问题 I have a list of words, say List<String> words = Arrays.asList("Hello alan i am here where are you"+ "and what are you doing hello are you there"); How can I get the top seven words which are repeated more than once in the list in descending order? And then the single entry words should be arranged in alphabetical order. So the output for the above should be those top seven words you (3) are (2) hello (2) alan (1) am (1) and (1) doing (1) I am looking this to do in Java 8 using streams, lamda

Java 8 streams group by 3 fields and aggregate by sum and count produce single line output

寵の児 提交于 2019-12-04 13:54:17
问题 I know there a similar questions asked in the forum but none of them seem to be addressing my problem fully. Now I'm very new to Java 8, so please bear with me. I have a list of Products, for example: Input: name category type cost prod1 cat2 t1 100.23 prod2 cat1 t2 50.23 prod1 cat1 t3 200.23 prod3 cat2 t1 150.23 prod1 cat2 t1 100.23 Output: Single line (name, category, type) summing the cost and count of products. Product { public String name; public String category; public String type;

Group, Sum byType then get diff using Java streams

只谈情不闲聊 提交于 2019-12-04 13:53:32
I would like to use stream to group, get the sum by type, then find the result of the different by type. So this is my data set. 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=SUBTRACT), Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) Sample(SampleId=6, SampleTypeId=2, SampleQuantity=10, SampleType=ADD), Sample(SampleId=7, SampleTypeId=2,

Java 8 Stream API toMap converting to TreeMap

北城以北 提交于 2019-12-04 11:34:18
问题 public class Message { private int id; private User sender; private User receiver; private String text; private Date senddate; .. } I have List<Message> list= new ArrayList<>(); I need to transform them to TreeMap<User,List<Message>> map I know how to do transform to HashMap using list.stream().collect(Collectors.groupingBy(Message::getSender)); But I need TreeMap with: Key - User with newest message senddate first Value - List sorted by senddate newest first Part of User class public class

An elegant way to specify initial capacity of Collector in java stream api

亡梦爱人 提交于 2019-12-04 06:31:55
I've tried to find a good way to set up initial capacity of collector in java stream api. The simplest example is there: data.stream() .collect(Collectors.toList()); I just want to pass an int with size of list into collector in order not to resize internal array. The first intention is to do it in such way: data.stream() .collect(Collectors.toList(data.size())); But unfortunately toList isn't overloaded to work with parameter. I found one solution but it smells: data.stream() .collect(Collectors.toCollection(() -> new ArrayList<>(data.size()))); Is there any way to express it simplier? I'd

Java 8 stream.collect( … groupingBy ( … mapping( … reducing ))) reducing BinaryOperator-usage

妖精的绣舞 提交于 2019-12-04 03:21:49
I played around with a solution using groupingBy , mapping and reducing to the following question: Elegantly create map with object fields as key/value from object stream in Java 8 . Summarized the goal was to get a map with age as key and the hobbies of a person as a Set . One of the solutions I came up with (not nice, but that's not the point) had a strange behaviour. With the following list as input: List<Person> personList = Arrays.asList( new Person(/* name */ "A", /* age */ 23, /* hobbies */ asList("a")), new Person("BC", 24, asList("b", "c")), new Person("D", 23, asList("d")), new

Complexity of grouping in Java8

橙三吉。 提交于 2019-12-04 03:09:44
I would like to learn the time complexity of the given statement below.(In Java8) list.stream().collect(groupingBy(...)); Any idea? 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 stream sources. So, assuming no intermediate operations that affect the time complexity, the groupingBy has to

Limit groupBy in Java 8

社会主义新天地 提交于 2019-12-03 23:21:42
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 Web")); studentClasses.add(new StudentClass("Sargent", 103, "Advanced Web")); studentClasses.add(new

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

陌路散爱 提交于 2019-12-03 17:17:43
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: Duplicate key exception, because my list has duplicate keys I want to add the inner elements of