Java集合总结

前面已经把 Java Collections Framework整体过了一遍,动态数组ArrayList,树集合TreeSet,双向队列LinkedList,键值对集合HashMap,树集TreeMap。他们都各自有各自的优点,ArrayList动态扩容,数组实现查询非常快但要求连续内存空间,TreeSet可以实现根据自然顺序排序的无重复集合,其底层实现基于TreeMap,双向队列LinkedList不需要像ArrayList一样创建连续的内存空间,它以链表的形式连接各个节点,但是查询搜索效率极低。HashMap存放键值对,内部使用数组加链表实现,检索快但是由于键是按照Hash值存储的,所以无序,在某些情况下不合适。TreeMap使用优化了的排序二叉树(红黑树)作为逻辑实现,物理实现使用一个静态内部类Entry代表一个树节点,这是一个完全有序的结构,但是每个树节点都需要保存一个父节点引用,左右孩子节点引用,还有一个value值,虽然效率高但开销很大。

常见问题:

1.ArrayList和LinkedList的区别?

LikedList底层实现为元素列表,每个节点都有前驱元素和后驱元素连接,插入、删除和增加速度很快,时间复杂度为O(1),查找的话,最坏情况需要遍历列表,时间复杂度为O(n)
ArrayList底层实现为数组,可以通过索引取到对应位置的元素,查询时间复杂度为O(n),插入删除和增加元素需要移动索引,时间负责度为O(n)

2.ArrayList和Vector的区别?
vector保证同一时间只有一个线程可以访问它,但是会有一些额外的资源开销,一般情况推荐使用ArrayList,多线程情况下可以使用 Collections.synchronizedList来创建线程安全的ArrayList,效果等效于Vector。

As the documentation says, a Vector and an ArrayList are almost equivalent. The difference is that access to a Vector is synchronized, whereas access to an ArrayList is not. What this means is that only one thread can call methods on a Vector at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList, this isn't the case. Generally, you'll want to use an ArrayList; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList function to create a synchronized list, thus getting you the equivalent of a Vector

3.List和Set接口的区别?
List允许重复元素,可以插入一个或者多个null值。Set不允许插入重复元素(可用于去重),最多允许插入一个null值。
List是一个有序容器,Set是一个无序容器。

4.ListIterator和Iterator的区别?
Iterator只能向后遍历, ListIterator可以向前向后遍历,Iterator迭代时不能修改集合的元素, ListIterator可以修改集合中的元素.

5.LinkedList的getFirst()、getLast()和peekFirst()、peekLast()有什么区别?
get系列的方法(JDK 1.2),当list为空时会报NoSuchElementException,这会造成编程开发的不便(需要捕捉异常)。peek系列的方法(JDK 1.6)不会报这一错误。
参考:What is the difference between getFirst() and peekFirst() in Java's LinkedList?

6.为什么LinkedList的peek()和peekFirst()以及pool()和pollFirst()具有同样的功能?

LinkedList implements two interfaces - Queue and Deque. And Deque extends from Queue. Now, Deque has defined the method - Deque#pollFirst() and inherited the method - Queue#poll(). So, LinkedList has basically these two methods defined for the two interfaces it implements.

7.HashSet、LinkedHashSet和TreeSet的区别?

HashSet is Implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1).

HashSet的底层是哈希表,内部的元素是无序的,添加删除和查询操作的算法时间复杂度为O(1).

LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion. The time complexity of basic methods is O(1).

LinkedHashSet的底层也是哈希表,根据元素的hashCode决定它们的存储位置,但是它使用链表维护元素的存储位置,以元素的添加顺序访问集合的元素,基本的操作算法时间复杂度为O(1).

TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity of O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc.

树集合的底层是红黑树结构,元素顺序默认按照自然顺序排列,也可以自定义排序比较器,但是树集合的基本操作算法时间复杂度为O(log (n))。

8.HashMap、TreeMap 、 Hashtable和LinkedHashMap的区别?

HashMap is implemented as a hash table, and there is no ordering on keys or values.

HashMap按照key的hashCode存储数据,内部数据是无序的,并且是非线程安全的,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。

TreeMap is implemented based on red-black tree structure, and it is ordered by the key.

TreeMap的key是根据红黑树存储的,默认按照key的自然顺序排序。

LinkedHashMap preserves the insertion order

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

Hashtable is synchronized, in contrast to HashMap. It has an overhead for synchronization

HashTable继承了Java的Dictionary,基本功能和HashMap差不多,不过由于HashTable的所有方法都加了synchronized关键字,因此HashTable是线程安全的。

posted on 2018-11-21 17:49  ShoolyShooly  阅读(146)  评论(0编辑  收藏  举报

导航