简体   繁体   中英

How to sort a multimap by complex key?

Let's assume that we have a multimap

Multimap<List<Integer>, String> map = HashMultimap.create();

map.put(asList(1), "a");
map.put(asList(1), "b");
map.put(asList(1), "c");
map.put(asList(2), "d");
map.put(asList(3), "e");
map.put(asList(3), "f");
map.put(asList(1), "g");
map.put(asList(1), "h");
map.put(asList(1), "i");
map.put(asList(1), "j");
map.put(asList(1), "k");
map.put(asList(1, 2), "l");
map.put(asList(1, 2), "m");
map.put(asList(1, 2), "n");
map.put(asList(1, 2), "o");
map.put(asList(3), "p");
map.put(asList(3), "q");
map.put(asList(3), "r");
map.put(asList(7,3), "s");
map.put(asList(7), "t");

The key of map represents a connection id. 1 connected to 2 and 3 connected to 7 . I want to get all symbols filtered by connection id

1&2 : a,b,c,d,g,h,i,j,k,l,m,n,o
7&3 : e,f,p,q,r,s,t

How can I do that via Guava?

The key of your multimap actually does not represent a connection id but a list of connection ids.

What you have:

{[1]=[a, b, c, g, h, i, j, k], [2]=[d], [3]=[p, q, r, e, f], [1, 2]=[l, m, n, o], [7]=[t], [7, 3]=[s]}

What I think you need:

{1=[a, b, c, g, h, i, j, k, l, m, n, o], 2=[d, l, m, n, o], 3=[e, f, p, q, r, s], 7=[s, t]}

I recommend simply using Multimap<Integer, String> instead of Multimap<List<Integer>, String> :

Multimap<Integer, String> multimap = ArrayListMultimap.create();
multimap.put(1, "a");
multimap.put(1, "b");
multimap.put(1, "c");
multimap.put(2, "d");
multimap.put(3, "e");
multimap.put(3, "f");
multimap.put(1, "g");
multimap.put(1, "h");
multimap.put(1, "i");
multimap.put(1, "j");
multimap.put(1, "k");
multimap.put(1, "l");
multimap.put(1, "m");
multimap.put(1, "n");
multimap.put(1, "o");
multimap.put(2, "l");
multimap.put(2, "m");
multimap.put(2, "n");
multimap.put(2, "o");
multimap.put(3, "p");
multimap.put(3, "q");
multimap.put(3, "r");
multimap.put(7, "s");
multimap.put(3, "s");
multimap.put(7, "t");
System.out.println("1 & 2 : " + ImmutableSet.copyOf(Multimaps.filterKeys(multimap, i ->
        i == 1 || i == 2).values()));
System.out.println("7 & 3 : " + ImmutableSet.copyOf(Multimaps.filterKeys(multimap, i ->
        i == 7 || i == 3).values()));

Example Output:

1 & 2 : [a, b, c, g, h, i, j, k, l, m, n, o, d]
7 & 3 : [e, f, p, q, r, s, t]

You can use streams for this if you have Java 8 or newer.

List<String> ones = map.entries().stream()
    .filter(entry -> entry.getKey().contains(1))
    .map(Map.Entry::getValue)
    .collect(Collectors.toList());

This example will produce a list with all the values that have 1 in their key but it can easily be modified to do ones with 1 or 2 , 1 and 2 , or anything else you want. Here's one that will do 1 or 2 :

List<String> onesAndTwos = map.entries().stream()
    .filter(entry -> entry.getKey().contains(1) || entry.getKey().contains(2))
    .map(Map.Entry::getValue)
    .collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM