繁体   English   中英

一个集合,表示Java中两个集合的串联

[英]A collection that represents a concatenation of two collections in Java

是否有一个类表示集合与另一个集合的串联? 这个类本身应该是一个Collection,并且应该将所有方法委托给底层(内部)集合 - 不应该分配额外的内存,也不应该修改任何原始集合。

用法示例:

Collection<String> foo = ...
Collection<String> bar = ...

// this should be O(1) memory and time
Collection<String> combined = concat(foo, bar);

if (combined.contains("Zee"))
  ...

for (String str : combined)
  System.out.println(str);

与任何收藏品一样,请查看google-collections 如果您有Set ,特别是(不仅仅是一般集合),您需要:

Set<String> combined = Sets.union(foo, bar);

这会创建两组的不可修改的视图。 也就是说, foobar变化将反映在combined (但不支持combined.add()等)。

对于更通用的情况,你有Iterables.concat()但只是让你迭代连接的项目, Iterable接口显然不包含contains所以你有点在那里。

google-collections中的其他集合实用程序类( com.google.common.collect.Listscom.google.common.collect.Collections2 )不包含任何串联方法。 不明白为什么他们不能,但目前他们不这样做。

你的问题非常模糊。 特别是“与另一个项目另一个集合”是非常不清楚的。

您至少可以使用Collection#addAll()将另一个Collection的内容添加到当前Collection 这里Collection可以是其子接口/实现的任何内容,例如ListSet

例:

List<String> foos = Arrays.asList("foo1", "foo2", "foo3");
List<String> bars = Arrays.asList("bar1", "bar2", "bar3");
foos.addAll(bars); // Now foos contains everything.

编辑 :还是你真的想创建一个新的 Collection基于现有的Collection ,然后添加一个新的项目呢? 在这种情况下,只需使用现有的Collection作为构造函数参数构造一个新的Collection 例如:

List<String> foos = Arrays.asList("foo1", "foo2", "foo3");
List<String> bars = new ArrayList<String>(foos);
bars.add("bar"); // Now bars contains everything.

没有,但自己写作应该是直截了当的

package ch.akuhn.util;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Concat {

    public static <T> Iterable<T> all(final Iterable<T>... iterables) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return new Iterator<T>() {
                    Iterator<Iterable<T>> more = Arrays.asList(iterables).iterator();
                    Iterator<T> current = more.hasNext() ? more.next().iterator() : null;
                    @Override
                    public boolean hasNext() {
                        if (current == null) return false;
                        if (current.hasNext()) return true;
                        current = more.hasNext() ? more.next().iterator() : null;
                        return this.hasNext();
                    }

                    @Override
                    public T next() {
                        if (!hasNext()) throw new NoSuchElementException();
                        return current.next();
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }

}

然后

for (Object each: Concat.all(collection,whatever,etcetera,...)) {
    // ...
}

刚刚在这里编写了这段代码,编译风险自负!

PS, 如果你要为这堂课编写单元测试,请发送给我。

我认为你所要求的是一个Java结构,它允许你将集合放在一起而不需要修改原始集合。 换句话说,您有集合A和B,分别为N和M. 在连续调用之后,你仍然有集合A和B,它们的大小仍然是N和M,但是你也有集合C,它指向A和B,使其大小为N + M.

答案是否定的,Java没有任何开箱即用的功能......但是你可以编写一个快速包装器来包装一系列集合并将这些集合添加到它。 (它只会维护对每个集合的引用)并且您可以根据需要公开get / insert方法。

Apache Commons Collections还有一个更通用的CompositeCollection类,可以用作任意数量Collection的接口。

我不确定你的要求。 我对你的问题的解释是你在Collection上寻找add方法。 我不认为这是你问的问题。

尝试InterleavingEnumeration或apache的commons集合' ListUtils (ListUtils.union())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM