如何从Guava MultiMap中获取每个条目及其相应的值?

时间:2022-11-12 20:44:31

I am reading from a huge csv file which contains duplicate entries. I was able to read the whole csv file into a Multimap. I am also able to obtain the keyset with duplicate values and write them to a file. I want to get the value associated with each of the keys and write it to a file but unable to do so. I cant seem to find any of the options that might help me. I tried using entries() method which according to the doc

我正在读取一个包含重复条目的巨大csv文件。我能够将整个csv文件读入Multimap。我还能够获取具有重复值的键集并将它们写入文件。我想获取与每个键关联的值并将其写入文件但不能这样做。我似乎无法找到任何可能对我有帮助的选项。我尝试使用根据doc的entries()方法

Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances

返回此多图中包含的所有键值对的视图集合,作为Map.Entry实例

but I am unable to get anything from it.

但是我无法从中得到任何东西。

I am using ArrayListMultiMap implementation of MultiMap. Reason for using ArrayList is that later I need to perform a search operation which is quicker in an ArrayList.

我正在使用MultiMap的ArrayListMultiMap实现。使用ArrayList的原因是后来我需要执行一个在ArrayList中更快的搜索操作。

I have pstored the keyset as a MultiSet which is why I am able to get all the duplicate keys.

我已将密钥集作为MultiSet进行存储,这就是我能够获取所有重复密钥的原因。

Value of each of the keys is an object which I want to be written to a file corresponding to that read key.

每个键的值是我想要写入对应于该读取键的文件的对象。

2 个解决方案

#1


1  

If you want each key and each value associated with that key, instead of each key-value pair, then you can do that with

如果您希望每个键和每个值与该键相关联,而不是每个键值对,那么您可以使用

for (Map.Entry<String, Collection<SomeClassObject>> entry : multimap.asMap().entrySet()) {
   String key = entry.getKey();
   Collection<SomeClassObject> valuesForKey = entry.getValue();
   // do whatever
}

#2


1  

As you've discovered, there are two ways to think about Multimaps. One is as a Map<K, Collection<V>>, and the other is as a Map<K, V>, where the keys do not have to be unique. In the documentation, Google stresses that the latter approach is preferred, although if you do multimap.asMap().entries() (N.B. Not just multimap.entries()), as Louis suggested, you will have entries like the former version.

正如您所发现的,有两种方法可以考虑Multimaps。一个是Map >,另一个是Map ,其中键不必是唯一的。在文档中,Google强调后一种方法是首选,但如果你做multimap.asMap()。entries()(N.B。不仅仅是multimap.entries()),正如路易斯建议的那样,你将拥有类似前一版本的条目。 ,v> ,collection>

For your particular problem, I'm not sure why you can't do something like the following:

对于您的特定问题,我不确定为什么您不能执行以下操作:

for (String key : multimap.keySet()) {
    Collection<SomeClassObject> values = multimap.get(key);
    //Do whatever with all the values for Key key...
}

#1


1  

If you want each key and each value associated with that key, instead of each key-value pair, then you can do that with

如果您希望每个键和每个值与该键相关联,而不是每个键值对,那么您可以使用

for (Map.Entry<String, Collection<SomeClassObject>> entry : multimap.asMap().entrySet()) {
   String key = entry.getKey();
   Collection<SomeClassObject> valuesForKey = entry.getValue();
   // do whatever
}

#2


1  

As you've discovered, there are two ways to think about Multimaps. One is as a Map<K, Collection<V>>, and the other is as a Map<K, V>, where the keys do not have to be unique. In the documentation, Google stresses that the latter approach is preferred, although if you do multimap.asMap().entries() (N.B. Not just multimap.entries()), as Louis suggested, you will have entries like the former version.

正如您所发现的,有两种方法可以考虑Multimaps。一个是Map >,另一个是Map ,其中键不必是唯一的。在文档中,Google强调后一种方法是首选,但如果你做multimap.asMap()。entries()(N.B。不仅仅是multimap.entries()),正如路易斯建议的那样,你将拥有类似前一版本的条目。 ,v> ,collection>

For your particular problem, I'm not sure why you can't do something like the following:

对于您的特定问题,我不确定为什么您不能执行以下操作:

for (String key : multimap.keySet()) {
    Collection<SomeClassObject> values = multimap.get(key);
    //Do whatever with all the values for Key key...
}