简体   繁体   English

如何迭代Guava多重映射以成对打印出值

[英]How can I iterate through a Guava multimap to print out values as pairs

For each key in a Guava Multimap I need to take the corresponding values and make pairs with them that are not dependent on order.Uniqueness of pairs are not dependent on order for make pairs inside of key. 对于Guava Multimap中的每个键,我需要获取相应的值并与它们成对,而不依赖于order。对的唯一性不依赖于键内的make对的顺序。 [A,B] = [B,A]. [A,B] = [B,A]。 If the pair [B,A] is found in another key then this should treated as another pair. 如果在另一个密钥中找到对[B,A],那么这应该被视为另一对。 I don't know how iterate the values so I can print out pairs that aren't dependent on order. 我不知道如何迭代值,所以我可以打印出不依赖于顺序的对。 I'm not sure how to index this either. 我不知道如何索引这个。 For arrays, I could just use a double loop. 对于数组,我可以使用双循环。

Here is an example Multimap: 以下是Multimap示例:

BE0004429: [DB00515, DB00951, DB01582]
BE0000059: [DB00603, DB01285]
BE0001052: [DB00366, DB00472, DB00856, DB01104, DB01149]

I want to take that data and change it to this format. 我想获取该数据并将其更改为此格式。 Look carefully, I'm trying to take the values of each key and make pairs. 仔细看,我正在尝试获取每个键的值并进行配对。

I want take the first value in the each key and pair it up with the other values. 我想取每个键中的第一个值并将其与其他值配对。 Take the second value and pair it with the value after. 取第二个值并将其与后面的值配对。 Until there no more unique pairs. 直到没有更多独特的对。

DB00515, DB00951
DB00515, DB01582
DB01582, DB00951

DB00603, DB01285

DB00366, DB00472
DB00366, DB00856
DB00366, DB01104
DB00366, DB01149
DB00472, DB00856
DB00472, DB01104
DB00472, DB01149
DB00856, DB01104
DB00856, DB01149
DB01104, DB01149

I wrote this answer before the question was updated to more clearly describe the desired behavior, however I suspect this answer will still help some people who stumble on this question trying to understand different ways to iterate over a Multimap , so I'm leaving this answer here for posterity. 我在问题更新之前写了这个答案,以便更清楚地描述所需的行为,但是我怀疑这个答案仍然会帮助一些偶然发现这个问题的人试图理解迭代Multimap不同方法,所以我将离开这个答案这里是后人。 See my other answer, above, for what OP was actually looking for. 请参阅上面的其他答案,了解OP实际上在寻找什么。


Suppose we start with the following Multimap : 假设我们从以下Multimap开始:

Multimap<String, Integer> map = new ImmutableSetMultimap.Builder<String, Integer>()
                                .put("one", 1)
                                .putAll("several", 1, 2, 3)
                                .putAll("many", 1, 2, 3, 4, 5)
                                .build();

We can print it's contents several ways, let's try them: 我们可以通过多种方式打印它的内容,让我们试一试:

  1. As a raw string: 作为原始字符串:

     map.toString() 
     {one=[1], several=[1, 2, 3], many=[1, 2, 3, 4, 5]} 
  2. As a collection of key => collection pairs, with unique keys: 作为key =>集合对的集合,具有唯一键:

     for(String key : map.keySet()) { System.out.println(key+": "+map.get(key)); } // OR with the asMap() view for(Entry<String,Collection<Integer> e : map.asMap().entrySet()) { System.out.println(e.getKey()+": "+e.getValue()); } 
     one: [1] several: [1, 2, 3] many: [1, 2, 3, 4, 5] 
  3. As a collection of key => value pairs, with duplicate keys (and possibly values, if it's a ListMultimap): 作为key =>值对的集合,具有重复键(可能是值,如果它是ListMultimap):

     for(Entry<String, Integer> e : map.entries()) { System.out.println(e.getKey()+": "+e.getValue()); } 
     one: 1 several: 1 several: 2 several: 3 many: 1 many: 2 many: 3 many: 4 many: 5 
  4. As a collection of values, without concern for their keys: 作为值的集合,无需关心其键:

     for(Integer v : map.values()) { System.out.println(v); } 
     1 1 2 3 1 2 3 4 5 

Those are generally the ways you'd work with a Multimap . 这些通常是您使用Multimap You say you're trying to "print out pairs that aren't dependent on order" - it's unclear to me what that means, but if you're saying you want the output of option 3 to be in an arbitrary order, you could write your strings to a list, then shuffle it, like so: 你说你试图“打印出不依赖于订单的对” - 我不清楚这意味着什么,但如果你说你希望选项3的输出是任意顺序,你可以将您的字符串写入列表,然后将其洗牌,如下所示:

List<String> pairs = new ArrayList<>();
for(Entry<String, Integer> e : map.entries()) {
  pairs.add(e.getKey()+": "+e.getValue());
}
Collections.shuffle(pairs);
for(String s : pairs) {
  System.out.println(s);
}

This would print your Multimap's contents as pairs in arbitrary order. 这将以任意顺序将Multimap的内容打印为对。

There's two parts to this question, now that I understand it, and really the fact that you're working with Multimaps isn't very important. 这个问题有两个部分,现在我明白了,而且你使用Multimaps的事实并不是很重要。 It's valuable to try to break problems into its component parts; 尝试将问题分解为其组成部分是很有价值的; when you do the problems often become clearer, even trivial seeming. 当你这样做时,问题往往变得更加清晰,甚至是琐碎的看似。

  1. How do I print successive pairs from a collection? 如何从集合中打印连续对?

    We need to define a function which will take a Collection (this will let us work with arbitrary Multimaps), and generate your pairs: 我们需要定义一个将采用Collection的函数(这将让我们使用任意Multimaps),并生成你的对:

     public static <V> List<String> genPairs(Collection<V> col) { List<V> ls = ImmutableList.copyOf(col); List<String> ret = new ArrayList<>(); for (int i = 0; i < ls.size()-1; i++) { for (int j = i+1; j < ls.size(); j++) { ret.add(ls.get(i)+", "+ls.get(j)); } } return ret; } 

    Demo: 演示:

     for(String pair : genPairs(ImmutableList.of(1,2,3))) { System.out.println(pair); } 
     1, 2 1, 3 2, 3 
  2. How do I print successive pairs from the values of a Multimap? 如何从Multimap的值中打印连续的对?

    This part's easy, you just loop over the value collections and call the above function: 这部分很简单,你只需循环遍历值集合并调用上面的函数:

     for(Collection<String> col : multimap.asMap().values()) { for(String pair : genPairs(col)) { System.out.println(pair); } System.out.println(); } 

Hope this helps... 希望这可以帮助...

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class GuavaSample {

    public static void main(String[] args) {
        Multimap<String, String> multiMap = ArrayListMultimap.create();
        multiMap.put("BE0004429", "DB00515");
        multiMap.put("BE0004429", "DB00951");
        multiMap.put("BE0004429", "DB01582");

        multiMap.put("BE0000059", "DB00603");
        multiMap.put("BE0000059", "DB01285");

        multiMap.put("BE0001052", "DB00366");
        multiMap.put("BE0001052", "DB00472");
        multiMap.put("BE0001052", "DB00856");
        multiMap.put("BE0001052", "DB01104");
        multiMap.put("BE0001052", "DB01149");

        for (String key : multiMap.keySet()) {
            List<String> list = (List<String>) multiMap.get(key);
            for (int i = 0; i < list.size(); i++) {
                for (int j = i + 1; j < list.size(); j++) {
                    System.out.println(list.get(i) + "," + list.get(j));
                }
            }
            System.out.println();
        }
    }

}

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

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