java common collection list and Set, Map difference and applicable scenarios summary

  Reprint please note from: http://blog.csdn.net/qq_22118507/article/details/51576319

               The difference between list and Set, Map and applicable scenarios


1. List and Set are inherited from the Collection interface, while Map is not

2. List features: elements have an order in which they are placed, and elements can be repeated. Set features: elements have no order in which they are placed, and elements cannot be repeated. Repeated elements will be overwritten. The location is determined by the HashCode of the element, and its location is actually fixed. Objects added to Set  must define the equals() method  .
In addition, list supports for loops, that is, traversing through subscripts, or iterators can be used, but set You can only use iteration, because it is out of order, and you cannot use subscriptsto get the desired value.) 

3. Comparison of Set and List: Set: The efficiency of retrieving elements is low, and the efficiency of deletion and insertion is high. Insertion and deletion will not cause the position of elements to change. List: Similar to an array, a List can grow dynamically, and the efficiency of finding elements is high, and the efficiency of inserting and deleting elements is low, because it will cause the position of other elements to change. 
 
 

4.Map is suitable for storing data of key-value pairs

5. Thread-safe collection classes and non-thread-safe collection classes 

LinkedList, ArrayList, HashSet are not thread safe, Vector is thread safe;
HashMap is not thread safe, HashTable is thread safe;
StringBuilder is not thread safe, StringBuffer is thread safe.


The following is the specific usage introduction:

Differences and applicable scenarios between ArrayList and LinkedList


Arraylist

Advantages: ArrayList implements a data structure based on dynamic arrays . Because the addresses are continuous, once the data is stored, the query operation efficiency will be higher (it is connected in memory).

Disadvantages: Because the addresses are continuous, ArrayList needsto move data,so the efficiency of insertion and deletion operations is relatively low.   

LinkedList

Advantages: LinkedList is based on the data structure of linked list, and the address is arbitrary, so there is no need to wait for a continuous address when opening up memory space. For add and remove operations , LinedList has an advantage . LinkedList is suitable for scenarios where head and tail operations are to be performed or inserted at a specified position

Disadvantage: Because LinkedList needs to move the pointer , the query operation performance is relatively low.

Applicable scenario analysis:

 Use ArrayList when you need to access data, and use LinkedList when you need to add, delete, and modify data multiple times.



Differences and applicable scenarios between ArrayList and Vector


 ArrayList有三个构造方法:

 

Java代码  
  1. public ArrayList(int initialCapacity)//构造一个具有指定初始容量的空列表。    
  2. public ArrayList()//构造一个初始容量为10的空列表。    
  3. public ArrayList(Collection<? extends E> c)//构造一个包含指定 collection 的元素的列表   

 Vector有四个构造方法:

 

Java代码  
  1. public Vector()//使用指定的初始容量和等于零的容量增量构造一个空向量。    
  2. public Vector(int initialCapacity)//构造一个空向量,使其内部数据数组的大小,其标准容量增量为零。    
  3. public Vector(Collection<? extends E> c)//构造一个包含指定 collection 中的元素的向量    
  4. public Vector(int initialCapacity,int capacityIncrement)//使用指定的初始容量和容量增量构造一个空的向量    


ArrayListVector都是用数组实现的,主要有这么三个区别:


1.Vector是多线程安全的,线程安全就是说多线程访问同一代码,不会产生不确定的结果。而ArrayList不是,这个可以从源码中看出,Vector类中的方法很多有synchronized进行修饰,这样就导致了Vector在效率上无法与ArrayList相比;

 

2.两个都是采用的线性连续空间存储元素,但是当空间不足的时候,两个类的增加方式是不同。

 

3.Vector可以设置增长因子,而ArrayList不可以。


4.Vector是一种老的动态数组,是线程同步的,效率很低,一般不赞成使用。

适用场景分析:

1.Vector是线程同步的,所以它也是线程安全的,而ArrayList是线程异步的,是不安全的。如果不考虑到线程的安全因素,一般用ArrayList效率比较高。
2.如果集合中的元素的数目大于目前集合数组的长度时,在集合中使用数据量比较大的数据,用Vector有一定的优势。


HashSetTreeset的适用场景


1.TreeSet 是二差树(红黑树的树据结构)实现的,Treeset中的数据是自动排好序的,不允许放入null 

2.HashSe是哈希表实现的,HashSet中的数据是无序的,可以放入null,但只能放入一个null,两者中的值都不能重复,就如数据库中唯一约束 

3.HashSet要求放入的对象必须实现HashCode()方法,放入的对象,是以hashcode码作为标识的,而具有相同内容的String对象,hashcode是一样,所以放入的内容不能重复。但是同一个类的对象可以放入不同的实例

  

   适用场景分析:HashSet是基于Hash算法实现的,其性能通常都优于TreeSet为快速查找而设计的Set,我们通常都应该使用HashSet,在我们需要排序的功能时,我们才使用TreeSet

 

                             HashMap与TreeMap、HashTable的区别及适用场景

 

HashMap 非线程安全  

HashMap基于哈希表实现。使用HashMap要求添加的键类明确定义了hashCode()equals()[可以重写hashCode()equals()],为了优化HashMap空间的使用,您可以调优初始容量和负载因子。 

 

TreeMap非线程安全基于红黑树实现。TreeMap没有调优选项,因为该树总处于平衡状态。 


适用场景分析:

HashMap和HashTable:HashMap去掉了HashTable的contains方法,但是加上了containsValue()和containsKey()方法。HashTable同步的,而HashMap是非同步的,效率上比HashTable要高。HashMap允许空键值,而HashTable不允许。

HashMap适用于Map中插入、删除和定位元素。 

Treemap适用于按自然顺序或自定义顺序遍历键(key) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728813&siteId=291194637