获取 MultiMap 中的 EntrySet
我有一个 MultiMap,并且需要使用列表中的值之一获取 MultiMap 的 EntrySet 的最佳方法。现在,我正在迭代整个映射的条目集并检查列表的值是否包含我需要的值。这适用于地图上有限数量的输入,但现在变成了 800 毫秒 - 1 秒的工作,这根本无法削减它。感谢您的提前帮助。
示例:
public static void main(String[] args) {
MultiMap multi = new MultiHashMap();
multi.put("Key1", new ArrayList<String>(Arrays.asList(new String[] { "Value1", "Value2", "Value3" })));
}
我希望能够通过仅输入 Value1 和 Value2 作为参数来获取 Key1、Value1、Value2 和 Value3
另外,如果有任何帮助,这是读取数据源后从缓存中获取的
I have a MultiMap and need the best possible way to get an EntrySet of a MultiMap using one of the values in the list. Right now, I'm iterating through the entry set of the whole map and checking if the value of the list contains my needed values. This works for a limited number of inputs on the map, but its now turned into 800ms - 1 second work, which just wont cut it. Thanks for the help in advanced.
Example:
public static void main(String[] args) {
MultiMap multi = new MultiHashMap();
multi.put("Key1", new ArrayList<String>(Arrays.asList(new String[] { "Value1", "Value2", "Value3" })));
}
I want to be able to get Key1, Value1, Value2 and Value3 by only inputting Value1 and Value2 as arguments
Also, if it's any help, this is from a cache after reading a data source
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[编辑以允许同时测试一组值]
[既然问题已得到澄清,则再次编辑]
此实现避免了显式循环,并且以更详细的方式编写使用 Guava 扩展的功能方式:
测试程序:
输出:
[1=a, 4=a, 5=c]
我很想知道它的效率有多低。
[Edited to allow for group of values to be tested simultaneously]
[Edited again now that the question has been clarified]
This implementation avoids explicit loops, and is written in a more functional manner using Guava extensions:
A test program:
The output:
[1=a, 4=a, 5=c]
I'd be curious to know how inefficient it is.
您是否尝试过 Apache Common 的集合?他们有一个 multimap 实现,可能为您提供帮助,特别是
containsValue
方法。假设值中没有重复元素,您可以使用 Set 来保存多值并加快搜索速度。只是一个(未经测试的)想法,如下所示:
Have you tried Apache Common's Collections? they have a multimap implementation that might help you, in particular the
containsValue
method.Assuming that there are no duplicate elements in the values, you could use a Set for holding the multi-values and speed-up the search. Just an (untested) idea, something like this: