comrade_adibit
comrade_adibit

Reputation: 3

Optimal way to transfer values from a Map<K, V<List>> to a Map<otherKey, otherValue<List>>

Heres what i got to work with: Map<Faction, List<Resource>>

So the list looks like this right now:

("rr", "wool")
("rr", "wool")
("rr", "lumber")
("bb", "wool")

So my goal is to have a Map<Resource, Integer>

Example of target contained values: ("Wool", 4), ("Grain", 3), ("Lumber", 2)


So I'm trying to do this (in pseudocode):


I've played around with streams and foreach loops but have produced no valuable code yet because I struggle yet in the conception phase.

Upvotes: 0

Views: 40

Answers (1)

Nowhere Man
Nowhere Man

Reputation: 19565

It seems that actual input data in Map<Faction, List<Resource>> look like:

{rr=[wool, wool, lumber], bb=[lumber, wool, grain]}

Assuming that appropriate enums are used for Resource and Faction, the map of resources to their amount can be retrieved using flatMap for the values in the input map:

Map<Faction, List<Resource>> input; // some input data

Map<Resource, Integer> result = input
    .values() // Collection<List<Resource>>
    .stream() // Stream<List<Resource>>
    .flatMap(List::stream) // Stream<Resource>
    .collect(Collectors.groupingBy(
        resource -> resource,
        LinkedHashMap::new, // optional map supplier to keep insertion order
        Collectors.summingInt(resource -> 1)
    ));

or Collectors.toMap may be applied:

...
    .collect(Collectors.toMap(
        resource -> resource,
        resource -> 1,
        Integer::sum,      // merge function to summarize amounts
        LinkedHashMap::new // optional map supplier to keep insertion order
    ));

Upvotes: 1

Related Questions