zoukankan      html  css  js  c++  java
  • JAVA集合框架

    转自:http://www.cnblogs.com/JimLy-BUG/p/5217479.html

    1、Collection和Collections区别:

      Collection是java的一个集合接口,集合类的顶级接口

      Collections是一个包装类(工具类),不能被实例化(由于其构造函数设为私有的private),其中包含了各种关于集合操作的静态方法,服务于Collection集合框架

    2、List与Set接口的区别:

      List和Set都是由Collection派生而来,其中List内的元素有序且可重复,set内的元素无序且不可重复。。。

    List接口有三个实现类:ArrayList,Vector,LinkedList

    复制代码
         Set<String> s = new HashSet<String>();
            s.add(null);
            s.add(null);
            System.out.println(s.size());    //1
            
            List<String> l = new ArrayList<String>();
            l.add(null);
            l.add(null);
            System.out.println(l.size());    //2
    复制代码

    3、HashMap,HashTable,TreeMap之间的区别:

      想要理清集合类或接口之间的关系。。。首先就得看看源码他们之间存在何种的继承实现关系。。。 

     ~HashMap:public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>

        ~AbstractMap:public abstract class AbstractMap<K,V> implements Map<K,V>

      ~HashTable:public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>

        ~Dictionary:public abstract class Dictionary<K,V>

      ~TreeMap:public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>

        ~NavigableMappublic interface NavigableMap<K,V> extends SortedMap<K,V>

          ~SortedMap:public interface SortedMap<K,V> extends Map<K,V>

      (1):HashTable中的方法是同步的,在多线程的应用程序中,不用专门的操作就可以安全的使用HashTable,而对于HashMap

          则需要额外的同步机制,其可以通过集合工具类Collections中的Collections.synchronizedMap(Map m)来实

          现多线程安全,TreeMap中的方法也是不同步的。在单线程情况下,HashMap的效率要高于HashTable

      (2):在HashMap中可以允许null作为键且这样的键只有一个,可以允许一个或多个的键对应的值为null,当get()方法返回null时,

          既可以表示HashMap中没有该键,也可以表示存在该键且该键所对应的值为null,因此在HashMap中不能用get()方法来

          判断HashMap中是否存在该键,而是应该用containsKey()来判断

    复制代码
    Map<String, Object> map = new HashMap<String, Object>();
    
    map.put(null, "hello");    
    map.put(null, "world");    
    map.put("linjm01", null);    
    map.put("linjm02", null);

    System.out.println("hello:" + map.get(null));    //hello:world
    System.out.println("linjm01:" + map.get("linjm01"));    //linjm01:null
    System.out.println("linjm02:" + map.get("linjm02"));    //linjm02:null
    System.out.println("linjm03:" + map.get("linjm03"));    //linjm03:nul

    Map<String, Object> table = new Hashtable<String, Object>();
    table.put(null, "world");
    table.put("table01", null);
            
    System.out.println("table:" + table.get(null));    //java.lang.NullPointerException
    System.out.println("table:" + table.get("table01"));    //java.lang.NullPointerExceptio
    复制代码

         (3):存取值的输出顺序:其中TreeMap中的元素是按照键来进行排序的TreeMap自定义排序用法

    复制代码
         Map<String, Object> map = new HashMap<String, Object>();
            
            map.put("map1", 1);
            map.put("map2", 2);
            map.put("map3", 3);
            
            for (Entry<String, Object> entry : map.entrySet()) {
                System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
            }
            
            /**
             *    map输出: 
             *    key:map3 value:3
             *    key:map2 value:2
             *    key:map1 value:1
             * */
            
            Map<String, Object> table = new Hashtable<String, Object>();
            
            table.put("table1", 10);
            table.put("table2", 11);
            table.put("table3", 12);
            
            for (Entry<String, Object> entry : table.entrySet()) {
                System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
            }
            
            /**
             *    table输出: 
             *    key:map3 value:3
             *    key:map2 value:2
             *    key:map1 value:1
             * */
           
            Map<String, Object> tree = new TreeMap<String, Object>();
            
            tree.put("world", 101);
            tree.put("hello", 102);
            tree.put("tree", 103);
            tree.put("hi", 104);
            tree.put("welcome", 105);
            
            for (Entry<String, Object> entry : tree.entrySet()) {
                System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
            }
            
            /**
             * tree输出:
             * key:hello value:102
             * key:hi value:104
             * key:tree value:103
             * key:welcome value:105
             * key:world value:101
             * */
    复制代码

       (4):HashMap与HashTable的数据结构都是哈希表,而TreeMap的数据结构是二叉树。

       (5):ConcurrentHashMap是线程安全的,不过不同于hashTable,具体请参考JDK1.5查看原理,

        主要采用表分段、锁分离技术实现线程安全。。。

        HashTable使用Synchronized是锁住整张Hash表让线程独占、、、

       而ConcurrentHashMap则不同。。将整张Hash表分为多个片段。。。每个片段都有自己的锁进行控制。。。

       因此可以实现并发地实现修改操作。。。

    4、ArrayList、Vector、LinkedList三者的异同点:

       (1):在初始化ArrayList时没指定初始化长度的话,默认的长度是10;

         ArrayList在增加新元素且超过了原始容量的话,ArrayList的扩容方案为原始容量的(3/2+1)1.5倍;

         Vector的扩容方案为原始容量的2倍;

       (2):ArrayList是非线程安全,而Vector类中的方法是同步的,在多线程的应用程序中,直接用Vector是安全的,

        在单线程的应用程序中,使用ArrayList的效率要高于Vector,说白了,Vector就是ArrayList在多线程应用下的一个替代

       (3):LinkedList是链式线性表,ArrayList是数组线性表,其区别:

        1):LinkedList适用于需要频繁地进行插入和删除操作,但随机访问速度慢,查找一个元素需要从头开始一个一个找

        2):ArrayList随机访问速度快,但不适用于需要频繁地进行插入和删除操作,因为每次插入和删除都需要移动数组中的元素

        3):两者都不是线程安全的

        4):LinkedList实现了Deque接口,而Deque接口又继承了Queue接口,因此LinkedList也可以被当做堆栈来使用      

     附录:

    复制代码
         /** List接口 */
            List<String> l1 = new ArrayList<String>();
            
            List<String> l2 = new Vector<String>();
            
            List<String> l3 = new LinkedList<String>();
            
            
            /** Set接口 */
            Set<String> s1 = new HashSet<String>();
            
            Set<String> s2 = new LinkedHashSet<String>();
            
            Set<String> s3 = new TreeSet<String>();
            
            
            /** Map接口 */
            Map<String, Object> map = new HashMap<String, Object>();
            
            Map<String, Object> table = new Hashtable<String, Object>();
            
            Map<String, Object> tree = new TreeMap<String, Object>();
    复制代码

    对于java中数组转list和list转数组的用法:  

    复制代码
        /** 数组转list */
            String[] array = {"hello", "world", "simope", "wsview", "java"};
            List<String> list = new ArrayList<String>();
            list = Arrays.asList(array);
            for (String str : list) {
                System.out.print(str + " ");
            }
            
            System.out.println("
    ==============================");
            
            /** list转数组 */
            String[] arr = new String[list.size()];
            list.toArray(arr);
            for (int i = 0, len = arr.length; i < len; i++) {
                System.out.print(arr[i] + " ");
            }
    复制代码

     其中Arrays类中提供的方法都为静态方法。。。主要用于实现数组的快速排序和搜索等。。。

    集合中也有对应的类专门服务于集合操作。。。Collections类。。。

  • 相关阅读:
    ironic port bind
    pdb /usr/bin/neutron-server
    networking_generic_switch
    [CodeForces586D]Phillip and Trains
    [CodeForces598D]Igor In the Museum
    [poj3468]A Simple Problem with Integers
    [bzoj1503][NOI2004]郁闷的出纳员
    [bzoj1208][HNOI2004]宠物收养所
    [luogu3384][模板]树链剖分
    [CodeForces869A]The Artful Expedient
  • 原文地址:https://www.cnblogs.com/cxzdy/p/5338512.html
Copyright © 2011-2022 走看看