【Note】Java Collections Study

HashMap vs TreeMap vs LinkedHashMap

1) HashMap is a map based on hashing of the keys. It supports O(1) get/put operations. Keys must have consistent implementations of hashCode() and equals() for this to work.

2) LinkedHashMap is very similar to HashMap, but it adds awareness to the order at which items are added (or accessed), so theiteration order is the same as insertion order (or access order, depending on construction parameters).

3) TreeMap is a tree based mapping. Its put/get operations take O(log n) time. It requires items to have some comparison mechanism, either with Comparable or Comparator. The iteration order is determined by this mechanism.


HashSet vs TreeSet vs LinkedHashSet

Because of sorting order provided by TreeSet, use TreeSet when you need a collection where elements are sorted without duplicates. HashSet are rather general purpose Set implementation, Use it as default Set implementation if you need a fast, duplicate free collection. LinkedHashSet is extension of HashSet and its more suitable where you need tomaintain insertion order of elements.


ArrayList vs CopyOnWriteArrayList

This class implements a variant of java.util.ArrayList in which allmutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. This is ordinarily too costly, but it becomes attractive whentraversal operations vastly overwhelm mutations, and, especially, when you cannot ordon't want to synchronize traversals, yet need to preclude interference among concurrent threads.


ArrayBlockingQueue vs LinkedBlockingQueue

The ArrayBlockingQueue is bounded with the capacity argument supplied when constructing it, whereas in the LinkedBlockingQueue, bounding is optional and the highest capacity is Integer.MAX_VALUE and it can grow up to that value if no lower bound is specified.

If you can estimate with reasonable accuracy the size of your queue then may be ArrayBlockingQueue is what you need. Othewise LinkedBlockingQueue may have to be it.


Fail-safe vs Fail-fast Iterator

As name suggest fail-fast Iterators fail as soon as they realized that structure of Collection has been changed since iteration has begun. Structural changes means adding, removing or updating any element from collection while one thread is Iterating over that collection. fail-fast behavior is implemented by keeping a modification count and if iteration thread realizes the change in modification count it throws ConcurrentModificationException.

Contrary to fail-fast Iterator, fail-safe iterator doesn't throw any Exception if Collection is modified structurally while one thread is Iterating over it because they work on clone of Collection instead of original collection and that’s why they are called as fail-safe iterator. Iterator of CopyOnWriteArrayList is an example of fail-safe Iterator also iterator written by ConcurrentHashMap keySet is also fail-safe iterator and never throw ConcurrentModificationException in Java.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值