More interviews Collections Framework

Reference: https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/collection/Java common set of interview questions frame .md

 

1, the difference between ArrayList and LinkedList?

1) Whether thread safety: ArrayList and LinkedList are not synchronized, which is not guaranteed to be thread safe;

2) the underlying data structures: ArrayList using Object underlying array; using the LinkedList underlying data structures are doubly linked list;

3) the impact position of the element insertions and deletions whether by:

  • ArrayList using storage array, so inserting and removing elements of the time complexity of the impact receiving element location. For example, when performing add (E e) method, the ArrayList will default element appended to the end of the list, the time complexity of this case is O (1); however, if you want i insert and delete elements in the specified position if (add (int index, E element)), on the time complexity is O (ni). Since when performing the above operations, after the i-th bit in the first set and the i-th element (ni) elements have to perform the operation / rearward to the forward one;
  • LinkedList linked list memory, the insertion, deletion elements are not affected by the time complexity of positions of the elements, are approximations O (1), and the array is approximately O (n).

4) whether to support fast random access: LinkedList does not support efficient random element access, and support for ArrayList. Quick access to fast random access is via the element object element number (corresponding to the get (int index));

5) Memory footprint: wasted space ArrayList is mainly reflected in the end of the list of the list will reserve some space capacity, cost and space LinkedList is reflected in each of its elements need to consume more space than ArrayList (direct precursor after direct drive data + +).

List traversal options:

1) implements the interface list RandomAccess preferentially used for general circulation, followed by the foreach;

2) does not implement list RandomAccess interface preference iterator traversal (bottom layer is the foreach traversal), the large size of the data achieved by the iterator, do not use common for loops.

Added: doubly linked list and two-way circular list

 

2, ArrayList and the Vector difference? Why replace Vector ArrayList it?

All methods of the Vector class are synchronized. By the two threads can safely access a Vector object, but one thread to access the code words of Vector to spend a lot of time on the synchronous operation.

ArrayList is not synchronized, it is recommended that do not require guaranteed thread-safe when using ArrayList.

 

3, ArrayList expansion mechanism?

 

4.HashMap and HashTable difference?

1) whether the thread safety: HashMap is not thread-safe, HashTable are thread-safe; internal HashTable basic methods have been modified in synchronized. (Thread-safe can be used ConcurrentHashMap);

2) Efficiency: Because the thread safety problems, HashMap HashTable than fast; in addition, HashTable basically been eliminated, do not use the code;

3) Support for Null Null value and the Key: HashMap Null as the key, so that only a key, but can have one or more keys corresponding to the value of null. But in the HashTable put into the keys as long as there is a null, direct throw NullPointException;

4) The initial capacity and the size of each expansion different sizes:

  • When creating an initial value if the capacity is not specified, the default size is 11 HashTable, after each expansion, the capacity of the original becomes 2n + 1; HashMap default initialization size is 16, after each expansion, the capacity becomes 2 times the original ;
  • When you create if given the capacity of the initial value of HashTable ,, then you can directly use a given size; and HashMap will expand its size is a power of 2, it said HashMap always use a power of 2 as Hash tables size.

5)底层数据结构:JDK1.8以后的HashMap在解决Hash冲突时有了较大的变化,当链表长度大于阈值(默认为8)时,将链表转化为红黑树,以减少搜素时间;HashTable没有这样的机制。

 

5.HashMap与HashSet的区别?

HashSet底层就是基于HashMap实现的。

1)HashMap实现了Map接口;HashSet实现了Set接口;

2)HashMap存储键值对,HashSet仅存储对象;

3)HashMap调用put()方法向Map中添加元素;HashSet调用set()方法向Set中添加元素‘

4)HashMap使用键(key)计算HashCode值;HashSet使用成员对象来计算HashCode的值,对于两个对象来说HashCode可能相同,所以equals()方法来判断对象的相等性。

 

6.HashMap的底层实现?

JDK1.8之前H是Map底层是数组和链表结合在一起使用,也就是链表散列。HashMap通过key的HashCode经过扰动函数处理后得到hash值,然后通过(n-1)&hash 判断当前元素存放位置(n:数组长度),如果当前位置存在元素的话,就判断该元素与要存入的元素的hash值以及key是否相同,如果相同的话,直接覆盖,不相同就通过拉链法解决冲突。

所谓扰动函数就是HashMap的hash方法。使用hash方法是为了防止一些实现比较差的hashCode()方法,换句话说,使用扰动函数之后可以减少碰撞。

所谓拉链法就是:将链表和数组相结合。也就是说创建一个链表数组,数组中每一个格就是一个链表。若遇到哈希冲突,则将冲突的值加入链表中即可。

JDK1.8之后,相较于之前的版本,JDK1.8之后在解决哈希冲突时有了较大的变化,当链表长度大于阈值(8)时,将链表转化为红黑树,以减少搜索时间。

 

7.HashMap多线程操作导致死循环问题?

参考:https://coolshell.cn/articles/9606.html

 

8.ConcurrentHashMap和HashTable的区别?

ConcurrntHashMap和HashTable的区别主要体现在 实现线程安全的方式上。

1)底层数据结构:JDK1.7的ConcurrentHashMap底层采用分段数组+链表实现,JDK1.8采用的数据结构跟HashMap1.8一样,数组+链表/红黑二叉树。HashTable采用的是数组+链表的形式,数组是HashMap的主体,链表则是为了解决Hash冲突而存在的;

2)实现线程安全的方式:

  • 在JDK1.7的时候,ConcorrentHashMap(分段锁)对整个桶数组进行了分割分段(Segment),每一把锁只锁容器其中一部分数据,多线程访问容器里不同数据段的数据,就不会存在所竞争,提高并发访问率。到JDK1.8的时候已经摒弃了Segment概念,而是直接使用node数组+链表+红黑树的数据结构来实现,并发控制使用syncheronized和CAS来操作。整个看起来就像是优化过且线程安全的HashMap;
  • HashTable(同一把锁):使用synchronized来保证线程安全,效率低下。当一个线程访问同步方法时,其他线程也访问同步方法,可能会进入阻塞或轮询状态,如使用put添加元素,另一个线程不能使用put添加元素,也不能使用get,竞争会越来越激烈效率会越来越低。

 

9.集合框架底层数据结构总结:

Collection

1)List

  • Array List:Object数组;
  • Vector:Object数组;
  • LinkedList:双向链表(JDK1.7之前是循环双向链表);

2)Set

  • HashSet(无序,唯一):基于HashMap实现的,底层采用HashMap来保存元素;
  • LinkedHashSet:LinkedHashSet继承自HashSet,并且其内部是通过LinkedHashMap来实现的。有点类似于我们之前说的LinkedHashMap;
  • TreeSet(有序,唯一):红黑树;

3)Map

  • HashMap:JDK1.8之前HashMap由数组+链表组成,数组是HashMap的主体,链表则是为了解决Hash冲突而存在的。JDK1.8之后在解决Hash冲突时有了较大的变化,当链表阈值大于8时,将链表转化为红黑树,以减少搜索时间;
  • LinkedHashMap:LinkedHashMap继承自HashMap,所以它的底层与HashMap相同。另外,LinkedHashMap在上面结构的基础上,增加了一条双向链表,使得上面的结构可以保持键值对的插入顺序。同时通过对链表进行相应的操作,实现了访问顺序相关逻辑;
  • HashTable:数组+链表组成,数组是hashMap的主体,链表则是为了解决哈希冲突而存在的;
  • TreeMap:红黑树。

 

10.如何选用集合?

主要根据集合的特点来选用,比如我们需要根据键值获取元素值时就选用Map接口下的集合;需要排序时就选择TreeMap,不需要排序就选择HashMap,需要保证线程安全就选用ConcurrentHashMap;

当我们只需要存放元素值时,就选择实现Collection接口的集合,需要保证元素唯一时选择实现Set接口的集合,比如TreeSet或HashSet;

不需要就选择实现List接口的集合,比如ArrayList或LinkedList。

 

 

Guess you like

Origin www.cnblogs.com/Rain1203/p/11245974.html