2. [Mandatory] Do not add elements to collection objects returned by keySet()
/values()
/entrySet()
, otherwise UnsupportedOperationException
will be thrown.
3. [Mandatory] Do not add nor remove to/from immutable objects returned by methods in Collections
, e.g. emptyList()
/singletonList()
.
Counter example: Adding elements to
Collections.emptyList()
will throwUnsupportedOperationException
.
4. [Mandatory] Do not cast subList in class ArrayList
, otherwise ClassCastException
will be thrown: java.util.RandomAccessSubList
cannot be cast to java.util.ArrayList
.
Note:
subList
ofArrayList
is an inner class, which is a view ofArrayList
. All operations on theSublist
will affect the original list.
5. [Mandatory] When using subList, be careful when modifying the size of original list. It might cause when performing traversing, adding or deleting on the subList.
6. [Mandatory] Use toArray(T[] array)
to convert a list to an array. The input array type should be the same as the list whose size is list.size()
.
Counter example: Do not use
toArray
method without arguments. Since the return type isObject[]
,ClassCastException
will be thrown when casting it to a different array type.Positive example:
7. [Mandatory] Do not use methods which will modify the list after using Arrays.asList
to convert array to list, otherwise methods like add/remove/clear will throw UnsupportedOperationException
.
8. [Mandatory] Method add
cannot be used for generic wildcard with <? Extends T>
, method get
cannot be used with <? super T>
, which probably goes wrong.
Note: About PECS (Producer Extends Consumer Super) principle:
1)Extends
is suitable for frequently reading scenarios.
2)Super
is suitable for frequently inserting scenarios.
9. [Mandatory] Do not remove or add elements to a collection in a foreach loop. Please use Iterator
to remove an item. object should be synchronized when executing concurrent operations.
Counter example:
Note: If you try to replace “1” with “2”, you will get an unexpected result.
Positive example:
10. [Mandatory] In JDK 7 and above version, Comparator
should meet the three requirements listed below, otherwise Arrays.sort
and Collections.sort
will throw IllegalArgumentException
.
Counter example: The program below cannot handle the case if o1 equals to o2, which might cause an exception in a real case:
11. [Recommended] Set a size when initializing a collection if possible.
12. [Recommended] Use entrySet
instead of keySet
to traverse KV maps.
Note: Actually,
keySet
iterates through the map twice, firstly convert toIterator
object, then get the value from theHashMap
by key.EntrySet
iterates only once and puts keys and values in the entry which is more efficient. UseMap.foreach
method in JDK8.Positive example:
values()
returns a list including all values,keySet()
returns a set including all values,entrySet()
returns a k-v combined object.
13. [Recommended] Carefully check whether a k/v collection can store null value, refer to the table below:
Counter example: Confused by
HashMap
, lots of people think null is allowed inConcurrentHashMap
. Actually,NullPointerException
will be thrown when putting in null value.
14. [For Reference] Properly use sort and order of a collection to avoid negative influence of unsorted and unordered one.
Note: Sorted means that its iteration follows specific sorting rule. Ordered means the order of elements in each traverse is stable. e.g.
ArrayList
is ordered and unsorted,HashMap
is unordered and unsorted,TreeSet
is ordered and sorted.