collectors Questions

4

Solved

I want to be able to convert a List to a HashMap where the key is the elementName and the values is a list of something random (in this case its the Element Name). So in short I want (A->List(A)...
Yaakov asked 23/7, 2014 at 17:27

14

Solved

Collectors.toMap throws a NullPointerException if one of the values is null. I don't understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason ...
Khelat asked 8/7, 2014 at 11:44

2

Solved

I have a list of orders and I want to group them by user using Java 8 stream and Collectors.groupingBy: orderList.stream().collect(Collectors.groupingBy(order -> order.getUser()) This return ...
Rundlet asked 14/6, 2016 at 14:57

14

Solved

I have this list (List<String>): ["a", "b", null, "c", null, "d", "e"] And I'd like something like this: [["a", "b"], ["c"], ["d", "e"]] In other words I want to split my list in sublis...
Electronegative asked 17/3, 2015 at 9:52

4

Solved

I have a Map<String, List<String>>. I want to transform this map to a List after filtering on the map's key. Example: Map<String, List<String>> words = new HashMap<&gt...
Lees asked 18/7, 2017 at 21:13

4

Solved

JDK 16 now includes a toList() method directly on Stream instances. In previous Java versions, you always had to use the collect method and provide a Collector instance. The new method is obviously...
Soul asked 30/1, 2021 at 15:44

8

Solved

In Java 8, this works: Stream<Class> stream = Stream.of(ArrayList.class); HashMap<Class, List<Class>> map = (HashMap)stream.collect(Collectors.groupingBy(Class::getSuperclass)); ...
Raised asked 25/3, 2014 at 3:45

5

Solved

I was trying to convert a list of Integers into a string of comma-separated integers. Collectors.joining(CharSequence delimiter) - Returns a Collector that concatenates the input elements, separate...
Geotaxis asked 22/8, 2019 at 4:34

10

Solved

I know how to "transform" a simple Java List from Y -> Z, i.e.: List<String> x; List<Integer> y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList(...
Moresque asked 18/9, 2014 at 2:14

1

How can the following method be written using Java Stream API? public Map<String, String> getMap() { Map<String, String> mapB = new HashMap<>(); for (String parameterKey : lis...
Rhine asked 17/10, 2022 at 14:4

4

In Java Streams - what is the difference between stream.max(Comparator) and stream.collect(Collectors.maxBy(Comparator)) in terms of performance. Both will fetch the max based on the comparator bei...
Genesis asked 29/9, 2018 at 18:24

8

Solved

Let consider a hashmap Map<Integer, List> id1 = new HashMap<Integer,List>(); I inserted some values into both hashmap. For Example, List<String> list1 = new ArrayList<String&g...
Platina asked 14/4, 2015 at 10:54

3

Solved

I have an Item class which contains a code, quantity and amount fields, and a list of items which may contain many items (with same code). I want to group the items by code and sum up their quantit...
Nuthouse asked 17/9, 2022 at 14:27

3

I have a stream of objects which I would like to collect the following way. Let's say we are handling forum posts: class Post { private Date time; private Data data } I want to create a list whi...
Phocis asked 22/12, 2015 at 13:42

6

Solved

I have an array of arrays of two strings. var array = new String[][]{ {"a", "1"}, {"a", "2"}, {"b", "3"}, ... }; How can I collect t...
Johannisberger asked 3/5, 2022 at 5:38

1

Solved

I am trying to implement a simple collector, which takes a list of collectors and simultaneously collects values in slightly different ways from a stream. It is quite similar to Collectors.teeing, ...

4

Solved

i was having working code with earlier version of java 8 which i was using to get unique values from list but since i upgraded to JDK 66 its giving me an error Type mismatch: cannot convert from ...
Magnetics asked 25/1, 2016 at 10:5

2

Solved

I've a two POJOs, Sample code below class A { String name; Object entries; // this can be a List<String> or a string - hence Object datatype //getters and setter here } class B { int s...
Hash asked 27/4, 2017 at 6:30

5

Solved

Suppose I have a group of bumper cars, which have a size, a color and an identifier ("car code") on their sides. class BumperCar { int size; String color; String carCode; } Now I need...
Plumbery asked 18/1, 2019 at 12:29

3

In java 8 , Stream API help us to do our job with very clean and with less number of code. I am big fan of these stream API. But there are few operations which kind of help in solving the sam...
Additory asked 16/6, 2021 at 8:11

7

If I have collections Point , how do I compute average of x,y using Java 8 stream on a single iteration. Following example creates two stream & iterates twice on the input collection to compu...
Lacker asked 13/1, 2015 at 15:35

1

Solved

I have this list of data List<Status> statuses = [ { id: 123 mainSatus: HA MoreInfo: { name: max status: MM } }, { id: 456 mainSatus: HA MoreInfo: { name: max status: KK } }, ...
Bernete asked 27/5, 2021 at 9:14

2

Solved

I have a class CarPart defined as: class CarPart { String name; BigDecimal price; Supplier supplier; } A class Report: class Report { List<Part> parts; BigDecimal total; } and a class ...
Rooky asked 20/5, 2021 at 16:52

7

This is really a question about a minor detail, but I'm under the impression to get something wrong here. If you add duplicate keys using Collectors.toMap-method it throws an Exception with message...
Nork asked 14/10, 2016 at 9:27

4

Solved

We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = ma...
Gerda asked 20/1, 2019 at 7:21

© 2022 - 2025 — McMap. All rights reserved.