zoukankan      html  css  js  c++  java
  • 接口基本知识 2016年12月24日

    1、首先搞明白容器Collection和collections的区别

        Collection是一个接口集合,继承Collection的类主要有List,Set和Queue;

        Collections是集合类的一个工具类,提供了许多静态方法,用于集合元素中的排序、搜索以及现场呢个安全等各种操作。

    2、this的用法

        一直以来对this的用法很模糊,这里做一个总结,说说自己对this的理解。

        1)this用来指代自己所在类的属性和方法。举个例子:

    public class ThisDemo {
        String name="Nick";
        public void print(String name){
            System.out.println("这个类中的名字: "+this.name);
            System.out.println("局部变量中的名字: "+name);
        }
        public static void main(String[] args){
            ThisDemo td=new ThisDemo();
            td.print("cena");
        }
    }

    运行结果为:

    这个类中的名字: Nick
    局部变量中的名字: cena

    2)还可以在一个类中定义两个构造参数,在一个构造参数中通过this引用来定义另一个构造参数。例如:

    public class NameAndAge {
        private String name;
        private int age;
        public NameAndAge(){
            this.age=24;
        }
        public NameAndAge(String name,int age){
            this();
            this.name=name;
        }
        public void print(){
            System.out.println("最终的姓名:" +name);
            System.out.println("最终的年龄: "+age);
        }
        public static void main(String[] args){
            NameAndAge naa=new NameAndAge("cena",20);
            naa.print();
        }
    }

    运行结果为:

    最终的姓名:cena
    最终的年龄: 24

    分析:在这个程序中有一个无参构造器和一个有两个参数的构造器,在两个参数的构造器中调用了无参构造器,可见在实例化这个类时,输入的年龄并未其作用,而是调用了this所在类中的年龄定义。

     3、Collection容器中,List必须按照插入的顺序保存元素,但是Set不能有重复元素,Queue按照排队规则来确定对象产生的顺序;

        Map允许用键值对来查找值。

    4、Arrays.asList()方法接受一个数组或是一个用逗号分隔的元素列表,并将其转换为一个List对象。

    5、Collections.addAll()方法接受一个Collection对象,以及一个数组或是一个用逗号分割的列表,将元素添加到Collection中。例如:

    public class AddingGroups {
        public static void main(String[] args){
            Collection<Integer> collection=new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));//将元素列表(1,2,3,4,5)转化为List添加到collection中
            Integer[] moreInts={6,7,8,9,10};
            collection.addAll(Arrays.asList(moreInts));//将数组转化为list添加到collection中,而Collections.AddAll()方法接受一个Colletion对象,以及一个                                                    数组或是一个用逗号分隔的列表,将元素添加到Collection中
            //collection.addAll(collection,11,12,13,14,15);
            //collection.addAll(collection,moreInts);
            List<Integer> list=Arrays.asList(16,17,18,19,20);
            list.set(1, 99);//将第一个位置的数设为99
            System.out.println(collection);
            System.out.println(list);
        }
    }

    运行结果为:

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    [16, 99, 18, 19, 20]

     6、Collection容器包括:List,以特定的耗损需保存一组元素;Set,元素不能重复;Queue,只允许在容器的一“端 ”插入对象,并从另一“端”移除对象。

        Map在每个槽内保存了两个对象,即键和与之相关联的值。

    7、ArrayListLinkList都是List类型,都是按照被插入的顺序保存元素,不同之处在于LinkList包含的操作多于ArrayList。

        HashSet、TreeSet和LinkedHashSet都是Set类型,每个相同的项只保存一次,但是三者实现元素存储的方式不同,HashSet使用的是相当复杂并且是最快的获取元素     的方式;如果存储顺序很重要,可以使用TreeSet,它按照比较结果的升序保存对象;LinkedHashSet按照被添加的顺序保存对象。

    8、对于每一个键,Map只接受存储一次,Map.put(Key,Value)方法增加想要的对象,Map.get(Key) 方法将产生与这个键相关联的值。

        HashMap提供了最快的查找技术,没有按照任何明显的顺序来保存其元素;TreeMap按照比较结果的升序保存键;LinkedHashMap按照插入顺序保存键,同时还保留了     HashMap的查询速度。

       举例说明:

    public class PrintingContainers {
        static Collection fill(Collection<String> collection){
            collection.add("rat");
            collection.add("cat");
            collection.add("dog");
            collection.add("dog");
            return collection;
        }
        static Map fill(Map<String,String> map){
            map.put("rat", "Fuzzy");
            map.put("cat", "Rags");
            map.put("dog", "Bosco");
            map.put("dog", "Spot");
            return map;
        }
        public static void main(String[] args){
            System.out.println(fill(new ArrayList<String>()));
            System.out.println(fill(new LinkedList<String>()));
            System.out.println(fill(new HashSet<String>()));
            System.out.println(fill(new TreeSet<String>()));
            System.out.println(fill(new LinkedHashSet<String>()));
            System.out.println(fill(new HashMap<String,String>()));
            System.out.println(fill(new TreeMap<String,String>()));
            System.out.println(fill(new LinkedHashMap<String,String>()));
        }
    }

    运行结果为:

    [rat, cat, dog, dog]
    [rat, cat, dog, dog]
    [dog, cat, rat]
    [cat, dog, rat]
    [rat, cat, dog]
    {dog=Spot, cat=Rags, rat=Fuzzy}
    {cat=Rags, dog=Spot, rat=Fuzzy}
    {rat=Fuzzy, cat=Rags, dog=Spot}

    分析:

    Collection用方括号扩住,Map用大括号扩住。

  • 相关阅读:
    SQL 将查询结果插入到另一张表中
    IntelliJ IDEA 的使用方法总结
    IntelliJ IDEA 常用快捷键使用说明
    从零打造在线网盘系统之Struts2框架配置全解析
    从零打造在线网盘系统之Struts2框架核心功能全解析
    从零打造在线网盘系统之Struts2框架起步
    从零打造在线网盘系统之Hibernate配置O/R映射
    从零打造在线网盘系统之Hibernate查询与更新技术
    从零打造在线网盘系统之Hibernate框架起步
    善用缓存提高你的Spring工程效率
  • 原文地址:https://www.cnblogs.com/qducn/p/6218448.html
Copyright © 2011-2022 走看看