collectors

Java8 stream groupingBy enum and counting

耗尽温柔 提交于 2019-11-30 17:13:06
问题 With the classes: public class Person { private String name; private Color favouriteColor; } public enum Color {GREEN, YELLOW, BLUE, RED, ORANGE, PURPLE} Having a List<Person> using the Java8 Stream API can I trasform it in a Map<Color, Long> having the count of each Color , also for the color that aren't included in the list. Example: List<Person> list = List.of( new Person("Karl", Color.RED), new Person("Greg", Color.BLUE), new Person("Andrew", Color.GREEN) ); Trasforming this list in a Map

Collectors.groupingBy doesn't accept null keys

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 16:47:58
In Java 8, this works: Stream<Class> stream = Stream.of(ArrayList.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); But this doesn't: Stream<Class> stream = Stream.of(List.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); Maps allows a null key, and List.class.getSuperclass() returns null. But Collectors.groupingBy emits a NPE, at Collectors.java, line 907: K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); It works if I create

Java 8 Stream “collect and group by” objects that map to multiple keys [duplicate]

余生颓废 提交于 2019-11-30 14:28:34
This question already has an answer here: Java 8 grouping by from one-to-many 4 answers I have the following objects: public class Item { String value; List<Person> owners; Person creator; } public class Person { String name; int id; Person manager; } now i have the a list containing 3 Item objects: i1 -> {value="1", owners=[p1, p2, p3], creator=p4} i2 -> {value="2", owners=[p2, p3], creator=p5} i3 -> {value="3", owners=[p5], creator=p1} the Person objects are as follows: p1 -> {manager=m1, ...} p2 -> {manager=m2, ...} p3 -> {manager=m3, ...} p4 -> {manager=m2, ...} p5 -> {manager=m1, ...} I

Java Collectors.groupingBy()---is List ordered?

本秂侑毒 提交于 2019-11-30 08:17:43
For the Collectors.groupingBy() that returns Map<K,List<T>> is it implied that the List<T> is in order that the stream is evaluated? I see no explicit description of the ordering of the list, whereas the concurrent version explicitly states no ordering. If it weren't ordered somehow, I'd expect it to be a Collection though, and I don't see what other ordering it could possibly be, other than order received. I'm hoping it's guaranteed that the last value in each list is the last value received for that group. The documentation for groupingBy() says: Implementation Requirements: This produces a

How to apply Filtering on groupBy in java streams

荒凉一梦 提交于 2019-11-30 04:44:25
How do you group first and then apply filtering using Java streams? Example : Consider this Employee class: I want to group by Department with a list of an employee having a salary greater than 2000. public class Employee { private String department; private Integer salary; private String name; //getter and setter public Employee(String department, Integer salary, String name) { this.department = department; this.salary = salary; this.name = name; } } This is how I can do this List<Employee> list = new ArrayList<>(); list.add(new Employee("A", 5000, "A1")); list.add(new Employee("B", 1000, "B1

Java 8 Streams: why does Collectors.toMap behave differently for generics with wildcards?

馋奶兔 提交于 2019-11-30 02:58:09
Assume that you have a List of numbers. The values in the List can be of type Integer , Double etc. When you declare such a List it is possible to declare it using a wildcard ( ? ) or without a wildcard. final List<Number> numberList = Arrays.asList(1, 2, 3D); final List<? extends Number> wildcardList = Arrays.asList(1, 2, 3D); So, now I want to stream over the List and collect it all to a Map using the Collectors.toMap (obviously the code below is just an example to illustrate the problem). Lets start off by streaming the numberList : final List<Number> numberList = Arrays.asList(1, 2, 3D, 4D

Java 8 Stream “collect and group by” objects that map to multiple keys [duplicate]

泪湿孤枕 提交于 2019-11-29 20:58:59
问题 This question already has answers here : Java 8 grouping by from one-to-many (4 answers) Closed 3 years ago . I have the following objects: public class Item { String value; List<Person> owners; Person creator; } public class Person { String name; int id; Person manager; } now i have the a list containing 3 Item objects: i1 -> {value="1", owners=[p1, p2, p3], creator=p4} i2 -> {value="2", owners=[p2, p3], creator=p5} i3 -> {value="3", owners=[p5], creator=p1} the Person objects are as follows

Accumulate a Java Stream and only then process it

为君一笑 提交于 2019-11-29 16:46:26
I had a document that looked like the following: data.txt 100, "some text" 101, "more text" 102, "even more text" I processed it using regex and returned a new processed documents as the follow: Stream<String> lines = Files.lines(Paths.get(data.txt); Pattern regex = Pattern.compile("([\\d{1,3}]),(.*)"); List<MyClass> result = lines.map(regex::matcher) .filter(Matcher::find) .map(m -> new MyClass(m.group(1), m.group(2)) //MyClass(int id, String text) .collect(Collectors.toList()); This returns a list of MyClass processed. Can run in parallel and everything is ok. The problem is that I now have

How to create a map with Java stream API using a value outside the stream?

孤者浪人 提交于 2019-11-29 09:10:53
I want to init a Map<String, BigDecimal> and want to always put the same BigDecimal value from outside of the stream. BigDecimal samePrice; Set<String> set; set.stream().collect(Collectors.toMap(Function.identity(), samePrice)); However Java complains as follows: The method toMap(Function, Function) in the type Collectors is not applicable for the arguments (Function, BigDecimal) Why can't I use the BigDecimal from outside? If I write: set.stream().collect(Collectors.toMap(Function.identity(), new BigDecimal())); it would work, but that's of course not what I want. The second argument (like

Java 8 Streams: why does Collectors.toMap behave differently for generics with wildcards?

一曲冷凌霜 提交于 2019-11-29 00:32:16
问题 Assume that you have a List of numbers. The values in the List can be of type Integer , Double etc. When you declare such a List it is possible to declare it using a wildcard ( ? ) or without a wildcard. final List<Number> numberList = Arrays.asList(1, 2, 3D); final List<? extends Number> wildcardList = Arrays.asList(1, 2, 3D); So, now I want to stream over the List and collect it all to a Map using the Collectors.toMap (obviously the code below is just an example to illustrate the problem).