barbara
barbara

Reputation: 3201

Implementation of super MultiValue map

Is there any ready implementation of super MultiValue map?

I need a map

SuperMap<List<String>, List<String>> superMap

where key List is set of keys, and value List is set of values.

keys: {a, b, c} and values for any of these keys are values: {1, 2, 3, 4, 5} It means that key a should have a values {1, 2, 3, 4, 5} as well as the key b and c.

Updated with requirements from comments

I need to get all keys of one group? For example I need to get collection of keys for one similar value. In that case I cannot use your approach map.put("a", value); because I need to group (a, b it is first group, and c is related to second group).

UPD2

I'm looking for a simple and much concise solution to replace this code

Map<List<String>, List<String>> ops = new HashMap<List<String>, List<String>>() {{
        put(asList("a", "aaa"), asList("1", "2"));
        put(asList("b", "bbb"), asList("3", "4"));
}};

public static List<String> values(String optionKey) {
    for (List<String> key : ops.keySet()) {
        for (String k : key) {
            if (optionKey.equals(k)) {
                return ops.get(key);
            }
        }
     }
     return Collections.emptyList();
 }

with some smart impl form any well known lib (Google Guava or something).

Upvotes: 0

Views: 251

Answers (1)

m0skit0
m0skit0

Reputation: 25873

I think you're overcomplicating your data structure. You can simply use HashMap or similar implementation.

Map<String, List<String>> map = new LinkedHashMap<>();
List<String> value = new ArrayList<>();

// Fill the value list here...

map.put("a", value);
map.put("b", value);
map.put("c", value);

Upvotes: 1

Related Questions