代码之家  ›  专栏  ›  技术社区  ›  Pindatjuh

Java集合引用副本

  •  0
  • Pindatjuh  · 技术社区  · 16 年前

    是否有一个集合的标准Java(1.5+)实现(即没有第三方)允许我将多个集合粘合到一个集合中?

    final SomeCollection x = new SomeCollection();
    final ArrayList a = new ArrayList();
    
    a.add("first");
    assert(a.size() == 1);
    
    x.embed(a); // don't know for sure, if this exists.
    assert(x.size() == 1);
    
    a.add("second");
    assert(a.size() == 2);
    assert(x.size() == 2); // the other array is used as a backend-collection.
    
    final ArrayList b = new ArrayList();
    b.add("third");
    assert(b.size() == 1);
    
    x.embed(b);
    assert(x.size() == 3); // x is a combination of the other two lists.
    

    谢谢

    3 回复  |  直到 16 年前
        1
  •  2
  •   cletus    16 年前

        2
  •  1
  •   kiwicptn    16 年前

        final List<String> x = new ArrayList<String>();    
        final List<String> a = new ArrayList<String>();
    
        a.add("first");    
        assert(a.size() == 1);  
    

    第二,集合(带有一个s)充满了集合“支持”另一个,例如。

        Collections.unmodifiableCollection(Collection<? extends T> c)
    
        3
  •  0
  •   lucentmind    16 年前

    如果您提到的“SomeCollection”类是java.util.Collection类型,那么您可以向其添加任何其他集合。 参见示例。

    Vector  v = new java.util.Vector();
    ArrayList alist = new java.util.ArrayList();
    v.addAll(alist);