服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用collections


  下面这张表格总结了用一个集合能做的所有事情(亦可对set和list做同样的事情,尽管list还提供了一些额外的功能)。map不是从collection继承的,所以要单独对待。
  
使用collections


  boolean add(object) *保证集合内包含了自变量。如果它没有添加自变量,就返回false(假)
  boolean addall(collection) *添加自变量内的所有元素。如果没有添加元素,则返回true(真)
  void clear() *删除集合内的所有元素
  boolean contains(object) 若集合包含自变量,就返回“真”
  boolean containsall(collection) 若集合包含了自变量内的所有元素,就返回“真”
  boolean isempty() 若集合内没有元素,就返回“真”
  iterator iterator() 返回一个反复器,以用它遍历集合的各元素
  boolean remove(object) *如自变量在集合里,就删除那个元素的一个实例。如果已进行了删除,就返回“真”
  boolean removeall(collection) *删除自变量里的所有元素。如果已进行了任何删除,就返回“真”
  boolean retainall(collection) *只保留包含在一个自变量里的元素(一个理论的“交集”)。如果已进行了任何改变,就返回“真”
  int size() 返回集合内的元素数量
  object[] toarray() 返回包含了集合内所有元素的一个数组
  
  *这是一个“可选的”方法,有的集合可能并未实现它。若确实如此,该方法就会遇到一个unsupportedoperatiionexception,即一个“操作不支持”违例,详见第9章。
  
  下面这个例子向大家演示了所有方法。同样地,它们只对从集合继承的东西有效,一个arraylist作为一种“不常用的分母”使用:
  
  //: collection1.java
  // things you can do with all collections
  package c08.newcollections;
  import java.util.*;
  
  public class collection1 {
   // fill with 'size' elements, start
   // counting at 'start':
   public static collection
   fill(collection c, int start, int size) {
  for(int i = start; i < start + size; i++)
   c.add(integer.tostring(i));
  return c;
   }
   // default to a "start" of 0:
   public static collection
   fill(collection c, int size) {
  return fill(c, 0, size);
   }
   // default to 10 elements:
   public static collection fill(collection c) {
  return fill(c, 0, 10);
   }
   // create & upcast to collection:
   public static collection newcollection() {
  return fill(new arraylist());
  // arraylist is used for simplicity, but it's
  // only seen as a generic collection
  // everywhere else in the program.
   }
   // fill a collection with a range of values:
   public static collection
   newcollection(int start, int size) {
  return fill(new arraylist(), start, size);
   }
   // moving through a list with an iterator:
   public static void print(collection c) {
  for(iterator x = c.iterator(); x.hasnext();)
   system.out.print(x.next() + " ");
  system.out.println();
   }  
   public static void main(string[] args) {
  collection c = newcollection();
  c.add("ten");
  c.add("eleven");
  print(c);
  // make an array from the list:
  object[] array = c.toarray();
  // make a string array from the list:
  string[] str =
   (string[])c.toarray(new string[1]);
  // find max and min elements; this means
  // different things depending on the way
  // the comparable interface is implemented:
  system.out.println("collections.max(c) = " +
   collections.max(c));
  system.out.println("collections.min(c) = " +
   collections.min(c));
  // add a collection to another collection
  c.addall(newcollection());
  print(c);
  c.remove("3"); // removes the first one
  print(c);
  c.remove("3"); // removes the second one
  print(c);
  // remove all components that are in the
  // argument collection:
  c.removeall(newcollection());
  print(c);
  c.addall(newcollection());
  print(c);
  // is an element in this collection?
  system.out.println(
   "c.contains(/"4/") = " + c.contains("4"));
  // is a collection in this collection?
  system.out.println(
   "c.containsall(newcollection()) = " +
   c.containsall(newcollection()));
  collection c2 = newcollection(5, 3);
  // keep all the elements that are in both
  // c and c2 (an intersection of sets):
  c.retainall(c2);
  print(c);
  // throw away all the elements in c that
  // also appear in c2:
  c.removeall(c2);
  system.out.println("c.isempty() = " +
   c.isempty());
  c = newcollection();
  print(c);
  c.clear(); // remove all elements
  system.out.println("after c.clear():");
  print(c);
   }
  } ///:~
  
  通过第一个方法,我们可用测试数据填充任何集合。在当前这种情况下,只是将int转换成string。第二个方法将在本章其余的部分经常采用。
  newcollection()的两个版本都创建了arraylist,用于包含不同的数据集,并将它们作为集合对象返回。所以很明显,除了collection接口之外,不会再用到其他什么。
  print()方法也会在本节经常用到。由于它用一个反复器(iterator)在一个集合内遍历,而任何集合都可以产生这样的一个反复器,所以它适用于list和set,也适用于由一个map生成的collection。
  main()用简单的手段显示出了集合内的所有方法。
  在后续的小节里,我们将比较list,set和map的不同实现方案,同时指出在各种情况下哪一种方案应成为首选(带有星号的那个)。大家会发现这里并未包括一些传统的类,如vector,stack以及hashtable等。因为不管在什么情况下,新集合内都有自己首选的类。

扫描关注微信公众号