Array vs Collection
- Array is best if we know the size of an array.
- Collection provides growable nature with performance implications.
Difference Between List & Set:
List Set
- Duplicates Allowed - Not Allowed
- Insertion order Preserved - Not Preserved
Difference Between ArrayList & LinkedList:
ArrayList LinkedList
1. Best for retrieval operation 1. Best for frequent insert/Delete operation
(random element access is fast)
2. Underlying data structure uses Array 2. Underlying data structure uses Double LL
Vector:
1. Underlying data structure uses Array.
2. Duplicates allowed.
3. Insertion order preserved
4. NULL object allowed.
5. Heterogeneous objects allowed.
6. Serializable and Clonable.
7. Implements RandomAccess Interface.
8. Thread safe.
- Only difference between ArrayList & Vector is ArrayList is not thread safe.
- Vector introduced in 1.0 version, before collection framework. So it contains legacy methods.
Note: How to get synchronized ArrayList object?
ArrayList list1 = new ArrayList();
List list2 = Collections.SynchronizedList(list1);
list2 object is synchronized Array List.
HashSet:
1. Underlying data structure uses HashTable.
2. Duplicates Not allowed.
3. Insertion order Not preserved (inserted based on hashcode of object)
4. NULL object allowed.
5. Heterogeneous objects allowed.
6. Serializable and Clonable.
7. Thread safe.
8. Best for Search operations (because of hashcode implementation)
LinkedHashSet:
- Exactly same as HashSet except the object insertion order is preserved.
- Underlying data structure uses HashTable + Linked List
SortedSet:
- Exactly same as Hashset except the object maintained in order.
- Default order 1. Ascending for integers 2. Alphabetical for String
- Can define custom comparator to change the default sorting method.
- Null object allowed only once. NPE will happen when objects compared to sort.
- Heterogeneous objects not allowed and all object should implement comparable interface.
TreeSet:
TreeSet same as SortedSet except
- Underlying data structure uses balance Tree.
HashMap vs HashTable
HashMap HashTable
Thread Safe non synchronized and thread safe and not thread safe synchronized Null keys and one null key and any do not allow null keys null values number of null values and null values Iterating the values are iterated by uses enumerator to iterate values using iterator
Note: Hashtable is a subclass of Dictionary class which is not used anymore. It is better off
externally synchronizing a HashMap or using a ConcurrentMap implementation
- Array is best if we know the size of an array.
- Collection provides growable nature with performance implications.
Difference Between List & Set:
List Set
- Duplicates Allowed - Not Allowed
- Insertion order Preserved - Not Preserved
Difference Between ArrayList & LinkedList:
ArrayList LinkedList
1. Best for retrieval operation 1. Best for frequent insert/Delete operation
(random element access is fast)
2. Underlying data structure uses Array 2. Underlying data structure uses Double LL
Vector:
1. Underlying data structure uses Array.
2. Duplicates allowed.
3. Insertion order preserved
4. NULL object allowed.
5. Heterogeneous objects allowed.
6. Serializable and Clonable.
7. Implements RandomAccess Interface.
8. Thread safe.
- Only difference between ArrayList & Vector is ArrayList is not thread safe.
- Vector introduced in 1.0 version, before collection framework. So it contains legacy methods.
Note: How to get synchronized ArrayList object?
ArrayList list1 = new ArrayList();
List list2 = Collections.SynchronizedList(list1);
list2 object is synchronized Array List.
HashSet:
1. Underlying data structure uses HashTable.
2. Duplicates Not allowed.
3. Insertion order Not preserved (inserted based on hashcode of object)
4. NULL object allowed.
5. Heterogeneous objects allowed.
6. Serializable and Clonable.
7. Thread safe.
8. Best for Search operations (because of hashcode implementation)
LinkedHashSet:
- Exactly same as HashSet except the object insertion order is preserved.
- Underlying data structure uses HashTable + Linked List
SortedSet:
- Exactly same as Hashset except the object maintained in order.
- Default order 1. Ascending for integers 2. Alphabetical for String
- Can define custom comparator to change the default sorting method.
- Null object allowed only once. NPE will happen when objects compared to sort.
- Heterogeneous objects not allowed and all object should implement comparable interface.
TreeSet:
TreeSet same as SortedSet except
- Underlying data structure uses balance Tree.
HashMap vs HashTable
HashMap HashTable
Thread Safe non synchronized and thread safe and not thread safe synchronized Null keys and one null key and any do not allow null keys null values number of null values and null values Iterating the values are iterated by uses enumerator to iterate values using iterator
Note: Hashtable is a subclass of Dictionary class which is not used anymore. It is better off
externally synchronizing a HashMap or using a ConcurrentMap implementation
0 comments:
Post a Comment