zoukankan      html  css  js  c++  java
  • java容器类总结

    1.java容器分类图

      说明:左图为简化图(其中粗线部分是重点的容器),右图为完整容器分类图

                             

    2.容器类接口和抽象容器类

    2.1 说明

      容器接口是容器的基础。使用接口可以将容器的实现与容器接口分开,因而可以使用相同的方法访问容器而不需关心容器具体的数据结构。

      同理,Iterator接口也使用户能够使用相同的方法访问不同的容器类。

    2.2 容器接口(Collection,Map,Iterator)

      1)collection接口

        * boolean add(Object obj): 添加对象,集合发生变化则返回true
        * Iterator iterator():返回Iterator接口的对象
        * int size()
        * boolean isEmpty()
        * boolean contains(Object obj)
        * void clear()
        * <T> T[] toArray(T[] a)

      2)Map接口(存放键值对,Map中的值也可以是一个容器)

        * Object get(Object key)
        * Object put(Object key, Object value)
        * Set keySet() : returns the keys set      Set<K> keySet()
        * Set entrySet(): returns mappings set    Set<Map.Entry<K,V>> entrySet()
        * containsKey()
        * containsValue() 

      3)Iterator接口

        * Object next()
        * boolean hasNext()
        * void remove()

       注意:remove函数不能连续执行多次,否则返回IllegalStateException 

                 ( if the next method has not yet been called, or the remove method has already been called after the last call to the next method.)

          通常用法:

      Iterator it=collection.iterator();
        while(it.hasNext())
        {
         Object obj=it.next();
         //do something 
        }

    2.3 子接口(List,Set,ListIterator,SortedMap,SortedSet)

      1)List(有顺序可以重复,有顺序所以操作时可以在方法中加入索引参数,如下:)

      * boolean add(E element)
      * void add(int index, E element)
      * E set(int index, E element)
      * E get(int index);

      2)Set(无顺序不可以重复,无序因而不能通过索引操作对象)

      3)ListIterator(Iterator for List,List是双向表,因而在Iterator上增加了一些新的方法,允许traverse the List in either direction)

      * boolean hasPrevious();
      * E previous();
      * int previousIndex()

      4) SortedMap

      说明:保证按照键的升序排列的映射,可以按照键的自然顺序( Comparable 接口)进行排序, 或者通过创建有序映射时提供的比较器进行排序
      (A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time)
      public interface SortedMap<K,V>extends Map<K,V>

      * Comparator comparator()
      * Object firstKey()
      * Object lastKey()


      5)SortedSet  

      主要用于排序操作,实现此接口的子类都是排序的子类

     public interface SortedSet<E>extends Set<E>
      * Comparator comparator()
      * E first() :返回第一个元素
      * E last()
    * SortedSet<E> headSet(E toElement): 返回less than toElement
      *
    SortedSet<E> tailSet(E fromElement)
      *
    SortedSet<E> subSet(E fromElement)

    2.4抽象容器类

      1)说明:使用抽象容器类可以方便的定义类,而不用在每个类中都实现容器接口container 中的所有的方法

      2)包含:

       * AbstractCollection      public abstract class AbstractCollection<E>extends Objectimplements Collection<E>
       * AbstractList              public abstract class AbstractList<E>extends AbstractCollection<E>implements List<E>
       * AbstractSet           public abstract class AbstractSet<E>extends AbstractCollection<E>implements Set<E>
      * AbstactMap                public abstract class AbstractMap<K,V>extends Object implements Map<K,V>
       * AbstractSequentialList    public abstract class AbstractSequentialList<E> extends AbstractList<E>

     

    3.具体容器类

    3.1概括

    1)collection: ArrayList,LinkedLsit,Vector,Stack

             TreeSet,HashSet,LinkedHashSet

    2) Map:     HashMap,LinkedHashMap,WeakHashMap, TreeMap, HashTable, IdentityHashTable(其中key的比较是通过==而不是equals)

    3.2常用的容器类

    1)ArrayList LinkedList(均非同步,多线程时需要考虑线程安全问题),Vector(同步),Stack

        1. List 接口支持通过索引的方法来访问元素:ArrayList 随机访问快改慢;LinkedList改快随机访问慢;Vector实现了同步,因而比ArrayList慢

        2. LinkedList使用双向链表实现LinkedList提供额外的get,remove,insert方法在LinkedList的首部或尾部。这些操作使LinkedList可被用作堆栈(stack),队列(queue)或双向队列(deque)。

        3. ArrayList没有定义增长算法,当需要插入大量元素是,可调用ensureCapacity方法提高添加效率

          4. Vector类似与ArrayList,但是是同步的,多线程安全(另外一点区别是ArrayList扩容时默认增长一半,Vector增长一倍)。无论是单线程还是多线程,Vector都比ArrayList慢

        5. Stack继承自Vector,实现一个后进先出的堆栈

        6.若需要实现同步可以调用Collections工具类的synchronizedList方法,如下:

    List list = Collections.synchronizedList(new ArrayList());
    synchronized(list) {
          Iterator i = list.iterator(); // Must be in synchronized block
          while (i.hasNext())
              foo(i.next());
      }
    或者:
    List list = Collections.synchronizedList(new LinkedList());

       7.定义如下:(注意LinkedList实现了Deque)

    public class ArrayList<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable
    public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, Serializable
    public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable

      

    2)TreeSet, HashSet, LinkedHashSet(HashSet,TreeSet不是线程安全的)

      1. TreeSet是SortedSet接口的唯一实现类,TreeSet可以确保集合元素处于排序状态,效率很高,可提高程序的效率;TreeSet通过compareTo或者compare排序,因而只要值相等即使equals不等(不同对象)也不能加到集合中(fails to obey Set interface)

      2. HashSet,效率很高,和TreeSet不同的是通过比较对象的equals区分不同对象,这样不同的对象可以不被重复的加入到集合中。

         hashCode()函数不好确定,对象默认的hashCode函数试对象的内存地址值,hashCode函数的好坏是HashSet性能的关键。

      3. LinkedHashSet,和HashSet相同,同样是根据元素的hashCode值来决定元素的存储位置,但是它同时使用链表维护元素的次序。LinkedHashSet在迭代访问Set中的全部元素时,性能比HashSet好,但是插入时性能稍微逊色于HashSet。

      4. Set可以插入null,最多一个null

    3) HashMap(非同步), HashTable(线程安全), TreeMap,  WeakHashMap

      1.HashTable与HashMap区别:(详情请查看HashTable与HashMap

                     1) Hashtable继承自Dictionary类,而HashMap继承自AbstractMap类。但二者都实现了Map接口。

                     2) Hashtable 中的方法是Synchronize的,而HashMap中的方法在缺省情况下是非Synchronize

                     3)Hashtable中,key和value都不允许出现null值;HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应 的值为null

                     4) HashTable直接使用对象的hashCode。而HashMap重新计算hash值。

      2. WeakHashMap是一种改进的HashMap,它对key实行“弱引用”,WeakHashMap使用元素的引用而不是值作为key,也就是说必须在引用相同(a==b)的情况下才能找到相关的值。另外,如果一个key不再被外部所引用,那么该key可以被GC回收。

        3. TreeMapSortedMap接口的基于红黑树的实现。此类保证了映射按照升序顺序排列关键字, 根据使用的构造方法不同,可能会按照键的类的自然顺序进行排序

        4.定义如下:

    public class Hashtable  extends Dictionary  implements Map, Cloneable, Serializable    
    public class HashMap  extends AbstractMap  implements Map, Cloneable, Serializable 
    public class TreeMap<K,V>extends AbstractMap<K,V>implements NavigableMap<K,V>, Cloneable, Serializable

     

    4.容器类使用补充

      1)使用抽象编程思想,创建时使用父类引用指向子类对象,返回时返回抽象接口

      2)如果涉及到堆栈,队列等操作,应该考虑用List,对于需要快速插入,删除元素,应该使用LinkedList,如果需要快速随机访问元素,应该使用ArrayList。

      3)如果程序在单线程环境中使用非同步的类,其效率较高

      4)可以使用Collections 工具类中unmodifiableList/unmodifiableMap/unmodifiableSet/unmodifiableSortedMap/unmodifiableSortedSet等创建不能修改的List,Map,List等

      5)可以使用Collections工具类中Collections.synchronizedList(new ArrayList())等实现同步

      6) 可以使用Arrays.equal()判断两个数组是否相等

  • 相关阅读:
    springboot启动时不加载数据库
    ElasticSearch常用的查询操作
    Windows10 搭建ElasticSearch集群服务
    windows10安装ElasticSearch7.5遇到两个警告解决方法
    MybatisPlus自动生成代码配置
    初识RabbitMQ ------基本概念
    深拷贝与浅拷贝的区别
    Java8中 LocalDateTime与Date互相转换
    Spring中常用的工具类StringUtils、DateUtils、CollectionUtils等
    SpringBoot定时任务 @Scheduled cron 表达式说明
  • 原文地址:https://www.cnblogs.com/wishyouhappy/p/3669198.html
Copyright © 2011-2022 走看看