Java Collections Framework (List, Set, Map)

A separate set of basic framework


List interface features:
1. It is an element of the set of ordered access. For example, the elements are stored sequentially 11,22,33. Then the collection, storage element is completed in order 11,22,33).

2. It is a collection with an index by indexing operation can be accurately set of elements (the index of the array is a reason).

3. There may be a set of repeated elements by elements equals method to compare whether the repetitive elements.

4.List interface provides a special iterator, called the ListIterator , in addition to allowing Iterator normal operation of the external interface provided, the iterator allows element insertion and replacement, and bidirectional access. Also provides a method to obtain the specified start position from the list and the list iterator.

List Common Interface implementation class:

## vector set of
Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index.

ListIterator by the iterator and methods of Vector returned iterator is fail-fast

Features: 1 2. The underlying thread synchronization is achieved arrays

Unique method

Enumeration<E> elements()  

## ArrayList set
1. The bottom array is implemented
2. The thread synchronization is not
3. query faster
collection of stored data structure is an array java.util.ArrayList` structure. Convenient element to add, delete collections.

## LinkedList集合
1.底层是一个链表实现的
2.不同步。线程不安全
3.增删是比较快的,原因在底层.
LinkedList`集合数据存储的结构是链表结构。方便元素添加、删除的集合

##区别
Vector & ArrayList

1)Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的

方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。

2)当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,

而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。

ArrayList & LinkedList
ArrayList的内部实现是基于内部数组Object[],所以从概念上讲,它更象数组,
但LinkedList的内部实现是基于一组连接的记录,所以,它更象一个链表结构,所以,它们在性能上有很大的差别:

 


## Enumeration和Iterator接口的区别

 

Iterator替代了Enumeration,Enumeration是一个旧的迭代器了。

与Enumeration相比,Iterator更加安全,因为当一个集合正在被遍历的时候,它会阻止其它线程去修改集合。

区别有三点:

Iterator的方法名比Enumeration更科学

Iterator有fail-fast机制,比Enumeration更安全

Iterator能够删除元素,Enumeration并不能删除元素

 

## ListIterator有什么特点
ListIterator继承了Iterator接口,它用于遍历List集合的元素。

ListIterator可以实现双向遍历,添加元素,设置元素

 

Set接口特点:

1.存取无序

2.没有索引,不能通过普通for循环遍历

3.不允许元素重复

Set接口常用实现类

##TreeSet集合

底层:红黑树,可以实现元素的自然排序和定制排序
解析:treeSet底层是用TreeMap实现的,构造方法中会创建一个TreeMap的实例,用于存放元素。添加元素时,需要先判断当前有无比较器,如果有,则根据比较器的比较规则进行排序,如果没有,则根据元素本身的特性进行排序。

##HashSet集合

底层:哈希表
解析:hashSet实现了Set接口,底层采用的是hash表,实际上采用的是一个HashMap的实例,在HashSet中,所有的元素都是存储到HashMap键值对的key上面,而value有一个统一的值。  

hashSet不存入相同的元素是因为,重写了hashCode()和equals()

##LinkedHashSet

LinkedHashSet内部使用的是LinkHashMap。这样做的意义或者好处就是LinkedHashSet中的元素顺序是可以保证的,也就是说遍历序和插入序是一致的。

## HashSet与TreeSet的区别

HashSet 【线程不安全,存取速度快,底层是hash实现】
TreeSet 【红黑树数据结构,默认对元素自然排序】

Map接口

Map是Java.util包中的另一个接口,它和Collection接口没有关系,是相互独立的,但是都属于集合类的一部分。Map包含了key-value对。Map不能包含重复的key,但是可以包含相同的value。

Map接口常用实现类:

##HashMap:

数组方式存储key/value,线程非安全允许null作为key和value,key不可以重复,value允许重复,不保证元素迭代顺序是按照插入时的顺序,key的hash值是先计算key的hashcode值,然后再进行计算,每次容量扩容会重新计算所以key的hash值,会消耗资源,要求key必须重写equals和hashcode方法

  默认初始容量16,加载因子0.75,扩容为旧容量乘2,查找元素快,如果key一样则比较value,如果value不一样,则按照链表结构存储value,就是一个key后面有多个value;

##Hashtable

  Hashtable与HashMap类似,是HashMap的线程安全版,它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢,它继承自Dictionary类,不同的是它不允许记录的键或者值为null,同时效率较低,

特有方法

Enumeration<E> elements()  

##LinkedHashMap

LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的,在遍历的时候会比HashMap慢,有HashMap的全部特性。

##TreeMap

  基于红黑二叉树的NavigableMap的实现,线程非安全,key不允许null,key不可以重复,value允许重复,存入TreeMap的元素应当实现Comparable接口或者实现Comparator接口,会按照排序后的顺序迭代元素,两个相比较的key不得抛出classCastException。主要用于存入元素的时候对元素进行自动排序,迭代输出的时候就按排序顺序输出

##HashMap&##Hashtable的区别

是否允许为null:

HashMap允许为null

Hashtable不允许为null

contains方法

Hashtable有contains方法

HashMap把Hashtable的contains方法去掉了,改成了containsValue和containsKey

继承不同:

HashMap<K,V> extends AbstractMap<K,V>

public class Hashtable<K,V> extends Dictionary<K,V>

Guess you like

Origin www.cnblogs.com/jiezai/p/11069056.html