- What is a collection?
A collection is a group of objects, in an orderer or unordered way. Collection in java is the basic interface and extends Iterable interface.
- Which interfaces extend Collection interface?
- List
- Set
- Queue
- Explain List interface.
List is an ordered collection of elements which extends Collection interface.
Following are the features of list -
- List maintains the insertion order of elements.
- List allows duplicate elements to be inserted.
- Which classes extend List interface?
- ArrayList
- LinkedList
- Vector
- Explain ArrayList.
ArrayList is a collection which extends AbstractList class and implements List, RandomAccess, Cloneable and Serializable interfaces.
Following are the features of ArrayList -
- ArrayList maintains insertion order of elements.
- ArrayList allows insertion of duplicate elements.
- ArrayList allows insertion of null values.
- ArrayList is not synchronized, which means, it is not thread safe.
- ArrayList is used when we need faster access to iterate elements.
- Explain LinkedList.
LinkedList is a collection which extends AbstractSequentialList and implements List, Deque, Cloneable and Serializable interfaces. Linked List contains elements, each having a node which contains the reference of next element.
Following are the features of LinkedList -
- LinkedList maintains insertion order of elements.
- LinkedList allows insertion of duplicate elements.
- LinkedList allows insertion of null values.
- LinkedList is not synchronized, which means, it is not thread safe.
- Linked List is used for faster addition and removal of elements.
- Explain Vector.
Vector is a collection which extends AbstractList and implements List, RandomAccess, Cloneable and Serializable interfaces.
Following are the features of Vector -
- Vector maintains insertion order of elements.
- Vector allows insertion of duplicate elements.
- Vector allows insertion of null values.
- Vector is synchronized, which means, it is thread safe.
- Vector increases its size as per the number of elements being added.
- Explain Set interface.
Set is an unordered collection of elements which extends Collection interface.
Following are the features of set -
- Set does not maintains the insertion order of elements.
- Set does not allows duplicate elements to be inserted.
- Which classes extend Set interface?
- HashSet
- LinkedHashSet
- TreeSet
- Explain HashSet.
HashSet is a collection which extends AbstractSet class and implements Set interface along with Cloneable and Serializable interfaces.
Following are the features of HashSet -
- HashSet does not maintains insertion order.
- HashSet does not allows insertion of duplicate elements.
- HashSet allows insertion of only one null value.
- HashSet is not synchronized, which means, it is not thread safe.
- HashSet is used when we need faster access to add and remove elements. Iteration over the elements is slow in hashset.
- Explain LinkedHashSet.
LinkedHashSet is a collection which extends HashSet class and implements Set, Cloneable and Serializable interfaces.
Following are the features of LinkedHashSet -
- LinkedHashSet does not maintains insertion order.
- LinkedHashSet does not allows insertion of duplicate elements.
- LinkedHashSet allows insertion of null values.
- LinkedHashSet is not synchronized, which means, it is not thread safe.
- LinkedHashSet is slow.
- Explain TreeSet.
TreeSet is a collection which extends AbstractSet class and implements NavigableSet, Cloneable and Serializable interfaces.
Following are the features of TreeSet -
- TreeSet inserts the elements is ascending order.
- TreeSet does not allows insertion of duplicate elements.
- TreeSet does not allows insertion of null values.
- TreeSet is not synchronized, which means, it is not thread safe.
- TreeSet is used when we need faster access to add and remove elements. Iteration over the elements is slow in hashset.
- Explain Queue.
Queue works on FIFO(First In First Out) pattern.
- Explain Map.
Map works on key-value pair. Each key is mapped to one value.
Following are the features of Map -
- Map cannot have duplicate keys but can have duplicate values.
- Which classes extend Map interface?
- HashMap
- LinkedHashMap
- TreeMap
- Explain HashMap.
HashMap follows the approach of HashTable, it extends AbstractMap and implements Map, Cloneable and Serializable interface.
Following are the features of HashMap -
- HashMap does not maintains the insertion order.
- HashMap allows insertion of null values.
- HashMap is not synchronized, which means its not thread safe.
- How is the performance of HashMap?
The performance of HashMap mainly depends on two parameters -
- Initial Capacity
- Load Factor
The capacity is the number of buckets in the hash table and the initial capacity is the capacity at the time hash table is created.
Load Factor is the measure of how full the hash table is allowed to get before its capacity automatically gets increased.
The hash tables is rehashed to approx twice the number of buckets when the number of entries in it exceed the product of load factor and current capacity. The default load factor (.75) manages good time and space costs. Higher values solve the space issue but lookup time increases which impacts the performance.
- Explain LinkedHashMap.
LinkedHashMap follows the approach of HashTable and LinkedList, it extends HashMap and implements Map interface.
Following are the features of LinkedHashMap -
- LinkedHashMap maintains the insertion order.
- LinkedHashMap allows insertion of null values.
- LinkedHashMap is not synchronized, which means its not thread safe.
- How is the performance of LinkedHashMap?
The performance of LinkedHashMap is slightly below than that of HashMap because it maintains the linked list. However, iteration of elements in LinkedHashMap is faster than that of HashMap.
- Explain TreeMap.
TreeMap is a sorted map that extends AbstractMap and implements NavigableMap, Cloneable and Serializable interface.
Following are the features of TreeMap -
- TreeMap inserts the elements in ascending order.
- TreeMap allows insertion of only one null value.
- TreeMap is not synchronized, which means its not thread safe.