获取 MultiMap 中的 EntrySet

发布于 2024-12-10 23:09:38 字数 502 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2024-12-17 23:09:38

[编辑以允许同时测试一组值]

[既然问题已得到澄清,则再次编辑]

此实现避免了显式循环,并且以更详细的方式编写使用 Guava 扩展的功能方式:

import java.util.Collection;
import java.util.Map.Entry;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;

public class TestIt {

    public static Iterable<Entry<Integer, String>> getEntrySetsForValues(
            Multimap<Integer, String> fromMap, final Collection<String> values) {
        return Iterables.filter(fromMap.entries(),
                new Predicate<Entry<Integer, String>>() {
                    @Override
                    public boolean apply(Entry<Integer, String> arg0) {
                        return values.contains(arg0.getValue());
                    }
                });
    }
}

测试程序:

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

public class Test {

    static Multimap<Integer, String> x = HashMultimap.create();

    public static void main(String[] args) {
        x.put(1, "a");
        x.put(1, "b");
        x.put(2, "d");
        x.put(3, "e");
        x.put(3, "f");
        x.put(4, "a");
        x.put(5, "b");
        x.put(5, "c");

        System.out.println(TestIt.getEntrySetsForValues(x,
                Sets.newHashSet("a", "c")));
    }
}

输出:

[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:

import java.util.Collection;
import java.util.Map.Entry;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;

public class TestIt {

    public static Iterable<Entry<Integer, String>> getEntrySetsForValues(
            Multimap<Integer, String> fromMap, final Collection<String> values) {
        return Iterables.filter(fromMap.entries(),
                new Predicate<Entry<Integer, String>>() {
                    @Override
                    public boolean apply(Entry<Integer, String> arg0) {
                        return values.contains(arg0.getValue());
                    }
                });
    }
}

A test program:

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

public class Test {

    static Multimap<Integer, String> x = HashMultimap.create();

    public static void main(String[] args) {
        x.put(1, "a");
        x.put(1, "b");
        x.put(2, "d");
        x.put(3, "e");
        x.put(3, "f");
        x.put(4, "a");
        x.put(5, "b");
        x.put(5, "c");

        System.out.println(TestIt.getEntrySetsForValues(x,
                Sets.newHashSet("a", "c")));
    }
}

The output:

[1=a, 4=a, 5=c]

I'd be curious to know how inefficient it is.

归属感 2024-12-17 23:09:38

您是否尝试过 Apache Common 的集合?他们有一个 multimap 实现,可能为您提供帮助,特别是 containsValue 方法。

假设值中没有重复元素,您可以使用 Set 来保存多值并加快搜索速度。只是一个(未经测试的)想法,如下所示:

public static <K, V> Map.Entry<K, Set<V>> getEntry(Map<K,Set<V>> map, Set<V> vals) {
    for (Map.Entry<K, Set<V>> entry : map.entrySet()) {
        boolean found = true;
        for (V val : vals) {
            if (!entry.getValue().contains(val)) {
                found = false;
                break;
            }
        }
        if (found)
            return entry;
    }
    return null;
}

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:

public static <K, V> Map.Entry<K, Set<V>> getEntry(Map<K,Set<V>> map, Set<V> vals) {
    for (Map.Entry<K, Set<V>> entry : map.entrySet()) {
        boolean found = true;
        for (V val : vals) {
            if (!entry.getValue().contains(val)) {
                found = false;
                break;
            }
        }
        if (found)
            return entry;
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文