按类别将一个 Set 拆分为多个 Set

发布于 2024-12-06 00:38:06 字数 1015 浏览 2 评论 0 原文

如果这个问题已在其他地方得到解答,我深表歉意,但我还没有找到好的答案!


我有一个 Set 对象,需要按其底层 类进行拆分 类型,将每个对象放入一个 Set 中(显然这意味着初始集合中的每个对象只会出现在新 Set 的一个中代码>对象)。

我当前的方法如下:

public static Map<Class,Set<Foo>> splitByClass(Set<Foo> foos) {

    // Create a map for the result
    Map<Class,Set<Foo>> FooMap = new HashMap<Class,Set<Foo>>();

    for (Foo foo: foos) {
        Class type = foo.getClass();

        // If a set for this key exists, add the item to that.
        if (FooMap.containsKey(type)) {
            FooMap.get(type).add(foo);
        } else {
            // Otherwise make a new set, add the item and the set to the map.
            Set<Foo> set = new HashSet<Foo>();
            set.add(foo);
            FooMap.put(type, set);
        }
    }        
    return FooMap;
}

我的问题:是否有一种更通用的方法可以根据某些评估方法(例如检查类类型)将Set拆分为子集?

Apologies if this has been answered elsewhere, but I have yet to find a good answer!


I have a Set of objects that I need to split by their underlying class type, into a Set for each (obviously this means that each object in the initial set will only appear in one of the new Set objects).

My current approach to this is as follows:

public static Map<Class,Set<Foo>> splitByClass(Set<Foo> foos) {

    // Create a map for the result
    Map<Class,Set<Foo>> FooMap = new HashMap<Class,Set<Foo>>();

    for (Foo foo: foos) {
        Class type = foo.getClass();

        // If a set for this key exists, add the item to that.
        if (FooMap.containsKey(type)) {
            FooMap.get(type).add(foo);
        } else {
            // Otherwise make a new set, add the item and the set to the map.
            Set<Foo> set = new HashSet<Foo>();
            set.add(foo);
            FooMap.put(type, set);
        }
    }        
    return FooMap;
}

My question: Is there a more generic way to split a Set into subsets, based on some evaluation method such as checking Class type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淑女气质 2024-12-13 00:38:06

您可以使用 Guava Multimap ,它会让生活变得更加简单:

Multimap<Class<? extends Foo>, Foo> mmap = HashMultimap.create();
for (Foo foo : foos) {
    mmap.put(foo.getClass(), foo);
}
return mmap;

此代码与上面的代码几乎相同,只是您必须将返回类型更改为 Multimap, Foo>

这是一个适用于任何提供的接口类型的通用版本:

public static <X> Multimap<Class<X>, X> splitByClass(
    final Iterable<? extends X> implementations, final Class<X> interfaceType) {

    final Multimap<Class<X>, X> mmap = HashMultimap.create();
    for (final X implementation : implementations) {
        @SuppressWarnings("unchecked")
        // this should be safe:
        final Class<X> implementationType = (Class<X>) implementation.getClass();
        mmap.put(implementationType, implementation);
    }
    return mmap;
}

You can use a Guava Multimap, it will make life a lot simpler:

Multimap<Class<? extends Foo>, Foo> mmap = HashMultimap.create();
for (Foo foo : foos) {
    mmap.put(foo.getClass(), foo);
}
return mmap;

This code is pretty much equivalent to your above code, except that you must change the return type to Multimap<Class<? extends Foo>, Foo>.

And here's a generic version that works with any supplied interface type:

public static <X> Multimap<Class<X>, X> splitByClass(
    final Iterable<? extends X> implementations, final Class<X> interfaceType) {

    final Multimap<Class<X>, X> mmap = HashMultimap.create();
    for (final X implementation : implementations) {
        @SuppressWarnings("unchecked")
        // this should be safe:
        final Class<X> implementationType = (Class<X>) implementation.getClass();
        mmap.put(implementationType, implementation);
    }
    return mmap;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文