Collection Interview Questions
Collection Interview Questions
Collection Collections
It's used to represent a collection of It defines a number of useful methods for
separate objects as a single entity. working with collections.
It is an interface. It is a utility class.
Since Java 8, the Collection is an interface
with a static function.
It only has static methods in it.
Abstract and default methods can also be
found in the Interface.
Q - 3) Why Map is not inherited from Collection
interface although it is a part of Java collection
framework?
Set List
It is an unordered sequence. It is an ordered sequence.
Access to items from a certain position is Elements can be accessed based on their
not permitted. position.
Iterator ListIterator
Only has the ability to traverse In both forward and backward
components in a Collection in a forward orientations, can traverse components in
direction. a Collection.
It offers methods to get element indexes
Iterators cannot be used to obtain
at any time while traversing List, such as
indexes.
next Index() and previous Index().
It aids in the traversal of Maps, Lists, and Only List may be traversed, not the other
Sets. two.
It throws a Concurrent Modification At any time, you can quickly add elements
Exception since it can't add elements. to a collection.
next(), previous(), has Next(), has
next(), remove(), and has Next are some
Previous(), and add() are some of the List
of the Iterator's functions ().
Iterator's methods
Q - 7) What is the advantage of the generic collection?
HashMap Hashtable
HashMap can contain one null key and Hashtable cannot contain any null key or
multiple null values. null value.
HashMap inherits the AbstractMap class Hashtable inherits the Dictionary class.
Q - 9) What do you understand by fail-fast?
Array ArrayList
The Array is of fixed size, means ArrayList is not of the fixed size
we cannot resize the array as we can change the size
per need. dynamically.
Arrays are of the static type. ArrayList is of dynamic size.
ArrayList Vector
Set Map
Belongs to java.util package Belongs to java.util package
Extends the Collection interface Doesn’t extend the Collection interface
Duplicate keys are not allowed but
Duplicate values are not allowed
duplicate values are
Only one null key can be stored but
Only one null values can be stored
multiple null values are allowed
Doesn’t maintain any insertion order Doesn’t maintain any insertion order
Q - 18) Differentiate between HashSet and HashMap.
HashSet HasMap
Based on Set implementation Based on Map implementation
Doesn’t allow any duplicate keys but
Doesn’t allow any duplicate elements
duplicate values are allowed
Allows only one null key but any number
Allows only a single null value
of null values
Has slower processing time Has faster processing time
Uses HashMap as an underlying data Uses various hashing techniques for data
structure manipulation
Q - 19) Differentiate between HashSet and TreeSet.
HashSet TreeSet
Uses HasMap to store elements Uses Treemap to store elements
By default, it stores elements in their
It is unordered in nature
natural ordering
Has faster processing time Has slower processing time
Uses hasCode() and equals() for Uses compare() and compareTo() for
comparing comparing
Allows only one null element Doesn’t allow any null element
Takes up less memory space Takes up more memory space
Q - 20) Write a program to sort ArrayList in
descending order?
• To sort the ArrayList in descending order we will use
two methods Collections.reverseOrder() method and
Collections.sort() method.
Q - 21) Write a program to convert HashSet to
Array?
Q - 22) Explain various interfaces used in Collection framework?
Iterator Enumeration
The Iterator can traverse legacy and Enumeration can traverse only legacy
non-legacy elements. elements.
The Iterator can perform remove The Enumeration can perform only
operation while traversing the traverse operation on the collection.
collection.
Q - 24) What does the hashCode() method?
HashMap TreeMap
HashMap does not keep track of the TreeMap preserves insertion order.
order of insertions.
The Null key can be used once in TreeMap does not let the use of a null
HashMap, and the Null value can be key, but it does permit the use of a null
used any number of times. value any number of times.
Q - 26) What is the difference between ArrayList
and LinkedList?
ArrayList LinkedList
The elements of this class are stored in a The elements of this class are stored in a
dynamic array. doubly-linked list.
The List and Deque interfaces are both
The List interface is implemented by this
implemented by this class. As a result, it
class. As a result, this serves as a list.
can be used as both a list and a deque.
Because there is no concept of changing
Because of the internal implementation, memory bits in a doubly-linked list,
manipulating an ArrayList takes longer. manipulating it takes less time than
manipulating an ArrayList
This class is more useful when the
This class is more useful when the
application requires data storage and
application requires data manipulation.
access.
Q - 27) When would you prefer TreeSet to HashSet?
Comparable Comparator
Multiple sorting sequences are available
A single sorting sequence is provided by
in the Comparator. To put it another way,
Comparable. To put it another way, we
we can sort the collection based on
can sort the collection by a single
different criteria such as id, name, and
attribute such as id, name, or price.
price.
To sort elements, Comparable provides To order elements, the Comparator
the compareTo() method. provides the compare() method.
It is present in the java.lang package. It is present in the java.util package.
The original class is unaffected by the
The original class is affected by
comparator, i.e. the real class is
Comparable, i.e. the real class is changed.
unaffected.
The Collections.sort(List) method can be The Collections.sort(List, Comparator)
used to sort Comparable type list method can be used to sort the list
members. components of the Comparator type.
Q - 31) Explain fail-fast and fail-safe iterators.
Fail-Fast Fail-Safe
These types of iterators do not allow
These types of iterators allow modifying
modifying the collection while iterating
the collection while iterating over it.
over it.
It throws
ConcurrentModificationException if the No exception is thrown if the collection is
collection is modified while iterating over modified while iterating over it.
it.
It uses the original collection while It uses a copy of the original collection
traversing the elements. while traversing over it.
No extra memory is required in this case. Extra memory is required in this case.
Q - 33) Why does HashMap allow null whereas HashTable does not allow null?
Arrays Collection
Arrays are fixed in size that is once we The collection is growable in nature and is
create an array we can not increase or based on our requirements. We can
decrease based on our requirements. increase or decrease of size.
With respect to memory, Arrays are not With respect to memory, collections are
recommended for use. recommended for use.
Arrays can hold only homogeneous data Collection can hold both homogeneous
types elements. and heterogeneous elements.
Q- 40) Why iterator in hashmap is considered fail-fast?
Stack Queue
Stacks works on the LIFO principle, which Queues work on the FIFO principle, which
means that the element inserted at the last will means that the element inserted first will be
be the first element that will be taken out. the first element that will be taken out.
Insert operation is called push operation. Insert operation is called enqueue operation.
Delete operation is called pop operation. Delete operation is called dequeue operation.