
Static method “sort(List, Comparator)” of Collections class is used to sort collection items/elementsSignature: Collections.sort(List list, Comparator CMP) Static method “sort(List)” of Collections class is used to sort collection items/elementsSignature: Collections.sort(List list)
The original class needs to implement interfaces affecting original classĪ separate new class implements interfaces don’t affect original class This is used for multiple-sorting over the collection items/elements This is used for single-sorting over the collection items/elements Provides compare() method to sort items/elementsSignature: public int compare(T o1, T o2) Provides compareTo() method to sort items/elementsSignature: public int compareTo(To) Those areĬomparable belongs to the java.lang package We should always check 2 important interfaces while dealing with the collection framework in Java. The only Iterator is allowed to iterate over HashMap items/elements HashMap allows only one null key (although it allows multiple null values corresponding to a key)īoth Iterator & Enumeration can be used to iterate over HashTable items/elements HashTable neither allows null key or null values This is comparatively faster, as it is non-synchronized Performance-wise HashTable is slower comparing with HashMap due to synchronization HashMap is not synchronized (need to make sure while working in a multi-threaded environment) HashTable is legacy (including other 4 viz., Vector, Stack, Dictionary, Properties)Īll legacy collection classes are synchronized. Doesn’t maintain order (while iterating).HashTable is synchronized, i.e., thread-safe.The null key is not allowed, and also null value is not allowed.Note: very similar to LinkedHashMap except maintaining ascending order.maintains ascending order (based on Key).The null key is not allowed (but it can have any number of null values corresponding to the key).
Note: very similar to HashMap except maintaining insertion order. maintains insertion order (based on Key). But it can be easily synchronized using Collections class, i.e., Collections.synchronizedMap(hashMap). maximum of only-one null key is allowed (but it can have any number of null values corresponding to a key). Important methods of PriorityQueue is poll(), peek(), remove(), head(). Maintains FIFO order, i.e., orders in the PriorityQueue are in FIFO fashion. The set allows only unique items/elements (if the same element inserted again, still it will contain just one element with that value). It sorts the elements and maintains ascending order. insertion order is maintained (in the order they are inserted). allows null element (if more than one is inserted, still it will contain only one null element). HashSet is fail-fast –> throws ConcurrentModificationException is modified by any other means than HashSet’s own remove() method. But it can be easily synchronized using Collections class, i.e., Collections.synchronizedSet(HashSet). insertion order is not maintained, i.e., items/elements order is random while iterating. allows only unique elements (doesn’t allow duplicates, if any duplicate is encountered, then it is overridden). uses the hashtable to store elements inside HashSet. The only Iterator is allowed to iterate items/elements inside ArrayList Vector increases its size by 100% of the current array when its capacity exceedsĪrrayList increases its size by 50% of the current array when its capacity exceedsīoth Iterator & Enumeration can be used to iterate items/elements inside Vector This is comparatively faster as it is non-synchronized Performance-wise vector is slower comparing with ArrayList due to synchronization Thus Vector is synchronizedĪrrayList is not synchronized (need to make sure while working in a multi-threaded environment) Vector is legacy (including other 4 viz., HashTable, Stack, Dictionary, Properties)Īll legacy collection classes are synchronized. Vector is legacy, and it is synchronizedĭifference Between Vector vs. size increases by 100% of the current array size when Vector exceeds its capacity. uses a grow-able array to store elements. manipulation, i.e., delete is very faster as no shifting is required (the links break, and a new link is formed while deleting any element from LinkedList). uses a dynamic array to store elements inside LinkedList. manipulation, i.e., delete is very slow due to the shifting of elements. accessing elements is faster (as we can use the index to access any elements). allows inserting/add duplicate elements. size increases by 50% of the current array size when ArrayList exceeds its capacity. uses the dynamic array to store elements inside ArrayList. String Interview Questions Java Collection Classes Framework ArrayList ( )