Java中的集合类,集合类有哪些,如何增添删除元素,如何遍历

http://www.cnblogs.com/LittleHann/p/3690187.html

import java.util.*;

public class TestCollection {

    public static void main(String[] args) {

        Collection arrayList = new ArrayList();
        arrayList.add("====天晴朗=====");
        arrayList.add("====暖花儿朵朵绽放=====");
        arrayList.add("====暖花儿朵朵绽放=====");
        arrayList.add("====暖花儿朵朵绽放=====");
        arrayList.add("====暖花儿朵朵绽放=====");
        arrayList.add("====暖花儿朵朵绽放=====");

        System.out.println(arrayList);
        System.out.println(arrayList.size());
        Object[] objs = arrayList.toArray();
        for(int i = 0 ; i < objs.length ; i++){
            System.out.println(objs[i]);
        }


        Collection linkedList = new LinkedList();
        linkedList.add("====天晴朗=====");
        linkedList.add("====暖花儿朵朵绽放=====");
        linkedList.add("====暖花儿朵朵绽放=====");
        linkedList.add("====暖花儿朵朵绽放=====");
        linkedList.add("====暖花儿朵朵绽放=====");
        linkedList.add("====暖花儿朵朵绽放=====");

        System.out.println(linkedList);
        System.out.println(linkedList.size());
        Object[] linkedobjs = linkedList.toArray();
        for(int i = 0 ; i < linkedobjs.length ; i++){
            System.out.println(linkedobjs[i]);
        }

        Collection vector = new Vector();
        vector.add("====天晴朗=====");
        vector.add("====暖花儿朵朵绽放=====");
        vector.add("====暖花儿朵朵绽放=====");
        vector.add("====暖花儿朵朵绽放=====");
        vector.add("====暖花儿朵朵绽放=====");
        vector.add("====暖花儿朵朵绽放=====");

        System.out.println(vector);
        System.out.println(vector.size());
        Object[] vectors = vector.toArray();
        for(int i = 0 ; i < vectors.length ; i++){
            System.out.println(vectors[i]);
        }


        Collection hashSet = new HashSet();
        hashSet.add("====天晴朗=====");
        hashSet.add("====暖花儿朵朵绽放=====");
        hashSet.add("====暖花儿朵朵绽放=====");
        hashSet.add("====暖花儿朵朵绽放=====");
        hashSet.add("====33333=====");
        hashSet.add("=========");
        System.out.println(hashSet);
        System.out.println(hashSet.size());
        hashSet.remove("=========");
        System.out.println(hashSet);
        System.out.println(hashSet.size());

        //SortedSet是抽象的,不能被实例化
        //'SortedSet' is abstract; cannot be instantiated
        //Collection sortedset = new SortedSet();

        //EnumSet是抽象的,不能被实例化
        //'EnumSet' is abstract; cannot be instantiated
        //Collection enumset =new EnumSet();
        


    }


}

  

原文地址:https://www.cnblogs.com/qianjinyan/p/10545082.html