Java集合-Collection

    xiaoxiao2024-01-19  183

    Collection接口是 (java.util.Collection)是Java集合类的顶级接口之一。所以不能直接实例化一个Collection,但是可以实例化它的一个子类,你或许经常把这些子类作为一个Collection统一探讨。在这篇文章中,你将看到如何处理。

    下面是本文的一个主题列表:

    Collection子类增加和移除元素检测一个Collection是否包含一个确定的元素Collection大小遍历一个Collection

    Collection子类 下面的接口继承了Collection接口:

    ListSetSortedSetNavigableSetQueueDeque

    Java没有一个可用的Collection接口的实现,所以你必须使用上述列表中的子类。Collection接口仅仅只是定义了每个子类共享的一系列方法。这使得忽略你具体使用的Collection类型而把他们当作一个Collection统一对待成为可能。这是一个标准的继承,没有什么神奇之处,但是有时候会成为一个很好的特性。本文的后面部分将描述这些常用操作的大部分使用方法。

    下面是一个Collection操作的一个方法:

    01 public class MyCollectionUtil{ 02  03   public static void doSomething(Collection collection) { 04  05     Iterator iterator = collection.iterator(); 06     while(iterator.hasNext()){ 07       Object object = iterator.next(); 08  09       //do something to object here... 10     } 11   } 12}

    下面是调用不用类型的Collection子类的方法的一些方式:

    1 Set  set  = new HashSet(); 2 List list = new ArrayList(); 3  4MyCollectionUtil.doSomething(set); 5MyCollectionUtil.doSomething(list);

    增加和移除元素

    不管你使用的是哪种类型的Collection子类,有一些标准的方法从一个Collection中增加和移除元素。增加和移除单个的元素就像下面这样:

    1 String     anElement  = "an element"; 2 Collection collection = new HashSet(); 3  4 boolean didCollectionChange = collection.add(anElement); 5 boolean wasElementRemoved   = collection.remove(anElement);

    add()方法向Collection中增加元素,如果Collection结构改变了,作为add()方法的结果,将返回true。如果一个Set的实例中已经存在了这个元素,那么不会重复增加,这个Set实例的结构也不会发生变化。另一方面,如果在一个List上调用这个方法,而这个List已经存在了这个元素,那么这个List将有两个这个元素。

    remove()方法移除一个元素。如果Collection中存在这个元素并且被移除了,这个方法将返回true。如果元素不存在,将返回false。

    你也可以增加和移除Collection对象,下面是一些示例:

    01 Set  aSet  = ... // get Set  with elements from somewhere 02 List aList = ... // get List with elements from somewhere 03  04 Collection collection = new HashSet(); 05  06 collection.addAll(aSet);    //returns boolean too, but ignored here. 07 collection.addAll(aList);   //returns boolean too, but ignored here. 08  09 collection.removeAll(aList);   //returns boolean too... 10 collection.retainAll(aSet);    //returns boolean too...

    addAll()方法增加通过参数传递过来的Collection中的所有元素。这个Collection对象本身不会被添加,仅仅只是添加它的元素。但是如果你调用add()方法传递这个参数,这个Collection对象本身将被添加,而不是它的元素。

    正是由于addAll()方法依赖于Collection的子类。有一些子类允许一样的元素被多次添加,而另外一些则不允许。

    removeAll()方法移除通过参数传递过来的Collection中的所有元素。如果传递过来的Collection中某些元素在目标Collection中不存在,这些元素将会忽略。

    retainAll()方法正好和removeAll()方法相反。不是移除所有给定参数Collection中元素,而是保留保留这些元素,移除其他的。记住,只有目标Collection中已经存在了这些元素,这些元素才可以保留。在参数Collection中存在而目标Collection中不存在的元素,将不会被新增到目标Collection。它们将被忽略:

    来看看一些使用伪代码的例子:

    01Collection colA = [A,B,C] 02 Collection colB = [1,2,3] 03  04Collection target = []; 05  06 target.addAll(colA);     //target now contains [A,B,C] 07 target.addAll(colB);     //target now contains [A,B,C,1,2,3] 08  09 target.retainAll(colB);  //target now contains [1,2,3] 10  11 target.removeAll(colA);  //nothing happens - already removed 12 target.removeAll(colB);  //target is now empty.

    检测一个Collection是否包含一个确定的元素

    Collection接口有两个方法来检查一个Collection是否包含一个或多个元素。这就是contains()和containsAll()方法。它们在下面被说明:

    1 Collection collection   = new HashSet(); 2 boolean containsElement = collection.contains("an element"); 3  4 Collection elements     = new HashSet(); 5 boolean containsAll     = collection.containsAll(elements);

    如果这个Collection含有这个元素,contains()方法将返回true,反之,则亦然。 如果这个Collection包含给定Collection参数中的所有元素,containsAll()方法将返回true,反之,则亦然。

    Collection大小

    你可以通过调用size()方法来检测一个Collection的大小,”Size“表示Collection中的元素个数,下面是一个示例:

    1 int numberOfElements = collection.size();

    迭代一个Collection

    你可以迭代collection中的所有元素,collection中包含的Iterator将完成这件事情,就像下面这样:

    1 Collection collection = new HashSet(); 2//... add elements to the collection 3  4Iterator iterator = collection.iterator(); 5 while(iterator.hasNext()){ 6     Object object = iterator.next(); 7     //do something to object; 8}

    你也可以使用新的for循环:

    1 Collection collection = new HashSet(); 2//... add elements to the collection 3  4 for(Object object : collection) { 5   //do something to object; 6} 转载自 并发编程网 - ifeve.com
    最新回复(0)