本文介绍了如何从Guava MultiMap中获取每个条目以及与之关联的相应值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

but I am unable to get anything from it.

我正在使用 MultiMap ArrayListMultiMap 实现.使用 ArrayList 的原因是,稍后我需要执行在 ArrayList 中更快的搜索操作.

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.

我将键集存储为 MultiSet ,这就是为什么我能够获得所有重复键的原因.

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

每个键的值是一个对象,我想将其写入与该读取键相对应的文件中.

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

推荐答案

您已经发现,有两种方法可以考虑Multimap.一个是作为 Map< K,Collection< V>> ,而另一个是作为 Map< K,V> ,其中的键不必是独特.在文档中,Google强调后一种方法是首选,尽管 multimap.asMap().entries()(注意,不只是 multimap.entries()),正如Louis所建议的那样,您将获得与以前版本类似的条目.

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.

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

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...
}

这篇关于如何从Guava MultiMap中获取每个条目以及与之关联的相应值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:12