zoukankan      html  css  js  c++  java
  • java集合框架总结一

    java集合框架用于存储数据,也被称为集合类

    位于java.util包下

    java.util包下常用接口和类

    Collection和Map是Java集合框架的根接口

    List集合是有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问。

    Set集合是无序集合,集合中的元素不可以重复,访问集合中的元素只能根据元素本身来访问(也是不能集合里元素不允许重复的原因)。

    Map集合中保存Key-value对形式的元素,访问时只能根据每项元素的key来访问其value。

    collection常用方法:

    Collection的常见方法:

    1,添加。
        boolean add(Object obj):
        boolean addAll(Collection coll):

    2,删除。
        boolean remove(object obj):
        boolean removeAll(Collection coll);
        void clear();
    3,判断:
        boolean contains(object obj):
        boolean containsAll(Colllection coll);
        boolean isEmpty():判断集合中是否为空。

    4,获取:
        int size():
        Iterator iterator()

    5,其他:
        boolean retainAll(Collection coll);取交集。
        Object[] toArray():将集合转成数组。

    import java.util.ArrayList;
    import java.util.Collection;
    
    /**
     * 2014-4-3
     * 集合框架Collection接口演示
     * @author Administrator
     *
     */
    public class CollectionDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Collection coll1=new ArrayList();
            Collection coll2=new ArrayList();
            show(coll1,coll2);
    
        }
        public static void show(Collection coll1,Collection coll2){
            //添加coll1,coll2
            coll1.add("abc");
            coll1.add("123");
            coll1.add("mno");
            System.out.println("coll1:"+coll1+'
    ');
    
            //把coll1中的集合元素添加到coll2中去。(包括与coll2相同那部分),coll1不变
            coll2.add("123");
            coll2.addAll(coll1);
            System.out.println("coll2:"+coll2+'
    ');
    
            //删除
            //coll1.remove(0);                    //错误写法
            //System.out.println(coll1);
            coll1.remove("abc");
            System.out.println("coll1:"+coll1+'
    ');
    
            //把c2中与c1相同的部分删除,c1不变
            coll2.removeAll(coll1);
            System.out.println("coll1:"+coll1);
            System.out.println("coll2:"+coll2+'
    ');
    
            //判断
            System.out.println("coll1中是否包含123:"+coll1.contains("123"));
    
            //判断c1与c2是否相同,相同返回true
            System.out.println("c1与c2是否相同:"+coll1.containsAll(coll2));
    
            System.out.println("coll2:"+coll2);
            System.out.println("c2中是否为空:"+coll2.isEmpty());
    
            //获取
            System.out.println(coll1);
            System.out.println("coll1中元素个数:"+coll1.size()+'
    ');
    
            //coll1与coll2取交集,相同的保存在coll1中,coll2不变
            System.out.println("coll1:"+coll1);
            System.out.println("coll2:"+coll2);
            coll1.retainAll(coll2);
            System.out.println("coll1:"+coll1);
            System.out.println("coll2"+coll2+'
    ');
    
            //将集合转化为数组
            coll2.add("mno");
            Object[] a=coll2.toArray();
            //for each循环
            System.out.println("输出数组b");
            for(Object x:a)
                System.out.println(x);
            System.out.println();
            for(Object y:a)
                System.out.println(y.toString());
            }
    }

    程序运行结果:

    coll1:[abc, 123, mno]
    
    coll2:[123, abc, 123, mno]
    
    coll1:[123, mno]
    
    coll1:[123, mno]
    coll2:[abc]
    
    coll1中是否包含123:true
    c1与c2是否相同:false
    coll2:[abc]
    c2中是否为空:false
    [123, mno]
    coll1中元素个数:2
    
    coll1:[123, mno]
    coll2:[abc]
    coll1:[]
    coll2[abc]
    
    输出数组b
    abc
    mno
    
    abc
    mno

    Iterator接口

    用于遍历集合中的元素,也被称为迭代器。Iterator只能向后迭代。

    常用方法:

    boolean hasNext():判断是否有下一个元素,如果仍有元素可以迭代,则返回true

    Object next():第一次调用返回第一个元素,以后返回集合里下一个元素。

    void remove();删除集合里上一次next方法返回的元素。

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    
    public class IteratorDemo {
    
        /**
         *2014-4-5
         *Iterator接口演示
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Collection c=new ArrayList();
            show(c);
    
        }
        public static void show(Collection c){
            c.add("123");
            c.add("abc");
            c.add("efg");
            c.add("mno");
            //通过Iterator访问集合中的元素
            Iterator i=c.iterator();
            while(i.hasNext()){
                String x=(String)i.next();
                if(x.equals("123"))
                    i.remove();
                //在迭代过程中,不能进行例如Collection这样的集合操作,只能进行迭代器操作
                //c.remove("123");
                else
                    System.out.println(x);
    
            }        
        }
    }

    程序运行结果:

    abc
    efg
    mno

    注意:普通情况下,当我们把对象存入集合中后,集合会忘记这个对象的类型,把所有集合元素都当成Object处理,JDK 1.5引入泛型之后,可以使用泛型来限制集合中元素的类型。

  • 相关阅读:
    正则表达式中/i,/g,/ig,/gi,/m的区别和含义
    Git 使用中显示“Another git process seems to be running in this repository...”问题解决
    MySQL数据库中varchar与char类型的区别
    上传本地代码到gitHub过程详解
    PHP实现四种基本排序算法
    最优秀的5个Linux文本编辑器 狼人:
    11款有用的Web开发在线工具 狼人:
    Java.net网站新改版上线 狼人:
    WebMatrix进阶教程(3):如何实现某种样式 狼人:
    新界面新功能Firefox 4.0 RC重磅登场 狼人:
  • 原文地址:https://www.cnblogs.com/shangshicc/p/3647509.html
Copyright © 2011-2022 走看看