集合(Collection)与迭代器(Iterator)

时间:2025-04-16 15:50:33
 public TestColleaction() {
  // TODO Auto-generated constructor stub
 }
 /List类/
 void testArrayList()
 {
  ArrayList al=new ArrayList();
  (1);
  (2);
  (3);  
  Iterator it=();//用迭代器将其迭代化,将其序列化为一个序列,
          //这样就可以遍历整个序列而不必关心底层结构
  (());
  (());
  while(())
  {
   (());
  }
 } 
 /**
  * Vector是同步、线程安全的,所以如果需要的是速度,并且不在多线程环境中使中,最好选ArrayList
  * ArrayList是非同步,当然也不是线程安全的
  * 且每次Vector容量的自动扩展是按100%扩展,但是ArrayList是按50%扩展,这样使用ArrayList
  * 就会节省内存空间
  */
 void testVector()
 {
  Vector vector=new Vector();
  (1);
  (2);
  (3);
  (4);
  //(());//读第一个值
  //(());//默认永远读第一个,通常用于循环中
  //(());
  Iterator it=();
  while(())
  {
   (());
  }
 }
 /*
  * LinkedList实现了List接口,允许null元素。此外LinkedList提供额外的get,remove,
  * insert方法在 LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈(stack),
  * 队列(queue)或双向队列(deque)。
  */
 void testLinkedList()
 {
  LinkedList ll=new LinkedList();
  (1);
  (2);
  (3);
  (4);
  (5);//链表操作可以进行前插或者是后插,中间任意插入
  (6);
  //(());//读第一个值
  //(());//默认永远读第一个,通常用于循环中
  //(());
  ();//把第一个弹出栈
  (10);//压入栈
  Iterator it=();
  while(())
  {
   (());
  }
 }
 /List类/
 /Map类/
 void testHashMap()
 {
  HashMap hm=new HashMap();
  //HashMap及Hashtable都是散列表,不可以排序,就算是排好了序也会被打乱
  (1, null);//可以放入空值
  (2, "21");
  (3, "33");
  (4, "w434");
  (5, "5we");
  (6, "df6");
  (7, "7we");
  (8, "re8");
  //Map类都要先转换为最老的迭代Collection后,才能够转换为新的迭代Iterator
  Collection c=();
  Iterator it=();
  while(())
  {
   (());
  }
 }
 void testHashTable()
 {
  Hashtable ht=new Hashtable();
  //(1, null);//不可以放入空值,这里会报错,并且Hashtable是同步的
  //HashMap及Hashtable都是散列表,不可以排序,就算是排好了序也会被打乱
  (2, "21");
  (3, "33");
  (4, "w434");
  (5, "5we");
  (6, "df6");
  (7, "7we");
  (8, "re8");
  Collection c=();
  Iterator it=();
  while(())
  {
   (());
  }
 }
 void testTreeMap()
 {
  TreeMap tm=new TreeMap();
  //(1, null);//不可以放入空值,这里会报错,并且Hashtable是同步的
  //TreeMap可以自动排序
  (2, "21");
  (3, "33");
  (4, "w434");
  (5, "5we");
  (6, "df6");
  (7, "7we");
  (8, "re8");
  Collection c=();
  Iterator it=();
  //输出将会按参数自动排序
  while(())
  {
   (());
  }
 }
 /Map类/
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  TestColleaction tc=new TestColleaction();
  //();
  ();
  //();
  //();
  //();
  //();
 }