简体   繁体   English

如何通过复杂键对多图排序?

[英]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. 映射的键表示连接ID。 1 connected to 2 and 3 connected to 7 . 1连接到23连接至7 I want to get all symbols filtered by connection id 我想按连接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. 您的多图键实际上不代表连接ID,而是代表连接ID列表。

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<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. 如果您使用的是Java 8或更高版本,则可以为此使用流。

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. 这个例子将产生所有具有值的列表1中的关键,但它可以很容易地进行修改,以做那些与1212 ,或任何你想要的东西。 Here's one that will do 1 or 2 : 这是将执行12的一个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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