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

comrade_adibit

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

  • Faction is the enum String value of a player faction (e.g "rr" for team red, "bb" for team blue)
  • Resources is String enum value of a Resource e.g "Wool", "Lumber"

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>

  • where Resource is String name of a Resource from an enum
  • Integer represents the amount of Resource Cards

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


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

  • extract all resources belonging to Faction "rr" and put them into a map <Resources, Integer> where each resource type should be represented once and Integer represents the sum of the amount of Resource cards --> repeat this step for 3 more player Faction

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

Alex Rudenko

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
    ));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java 8 convert Map<K, List<V>> to Map<V, List<K>>

Map<K,V> back to Map<V,Map<K,V>> after groupingBy value, instead of Map<Obj, List<Entry<K,V>>>

List values in a map

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

Converting Map<K, V> to Map<V,List<K>>

Pandas - map column from dict values in a list

List<Object[]> to Map<K, V> in java 8

How to create a List<T> from Map<K,V> and List<K> of keys?

Java 8 List<V> into Map<K, V>

Java 8 List<V> into Map<K, V> with Function

Java - Retrive Indivudual Values from a List in a Map

Convert List of List of Elements to Map < K, List<V>>

Java 8 List<T> into Map<K, V>

Map dictionary values to a list

Java 8 Streams: Make Collectors.groupingBy return Map<K, List<V>> instead of Map<K, List<List<V>>>

How could transform list to Map<K,V> but not List<V>

Kotlin Is there a way to flatten Map<K out T , List<V out T> to List<T>

Creating a list of widgets from map with keys and values

How to convert List<Map<K,V>> into Map<K,List<V>> In java

Groovy groupby List generated from Map values

ListView and Map with values as List

Shortest way to extract Map from List in Java

Conversion from List<Map<K,V>> to List<V> (Get all the unique values from a List of Maps)

Convert RDD[(K,V) to Map[K,List[V]]

Extract all String values from a list of map

Get a list of distinct values from map

Convert Map<K, List<V>> to Map<K, V> where V have 2 lists of objects

Given a map of type Map<K, List<V>>, how to update value within List<V> using Java streams?

Copy Values From List and Map To A File In Terraform

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive