Java 8 threads - grouping into a single value

I am currently working with List<Map<String, Object>>where I am trying to group different keys on a map. This seems to work using Java 8 Streams:

Map<Object, Map<Object, List<Map<String, Object>>>> collect =
   list
   .stream()
   .collect(Collectors.groupingBy(
       item -> item.get("key1"),
       Collectors.groupingBy(item -> item.get("key2"))
   ));

As expected, give me a Map<Object, Map<Object, List<Map<String, Object>>>>, which works well, where the possible grouped results are greater than 1.

I have various examples where the grouping performed will always result in one item in the lower level list, for example.

List of lines

{
  [reference="PersonX", firstname="Person", dob="test", lastname="x"],
  [reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]
}

Grouped by link

Currently - grouped in a list with 1 Map<String,Object>

[PersonX, { [reference="PersonX", firstname="Person", dob="test", lastname="x"]}],
[JohnBartlett, { [reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]}]

Preference - no list, only one Map<String,Object>

[PersonX, [reference="PersonX", firstname="Person", dob="test", lastname="x"]],
[JohnBartlett, [reference="JohnBartlett", firstname="John", dob="test", lastname="Bartlett"]]

Is there a way in threads to force the output for these instances to be Map<Object, Map<Object, Map<String, Object>>>- thus, one Map<String,Object>, and not Listone?

.

+4
1

, , , , :

 .collect(Collectors.groupingBy(
   item -> item.get("key1"),
   Collectors.toMap(item -> item.get("key2"), Function.identity())
 ));

BinaryOperator, ( )

+7

Source: https://habr.com/ru/post/1673697/


All Articles