Java Collections Interview Questions
Java Collections Interview Questions
The Iterator is an interface, used to traverse through the elements of a Collection. It is not advisable to
modify the collection itself while traversing an Iterator.
What is the Collections API?
The Collections API is a set of classes and interfaces that support operations on collections of objects.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
What is the List interface?
The List interface provides support for ordered collections of objects.
How can we access elements of a collection?
We can access the elements of a collection using the following ways:
1.Every collection object has get(index) method to get the element of the object. This method will return
Object.
2.Collection provide Enumeration or Iterator object so that we can get the objects of a collection one by
one.
What is the Set interface?
The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not
allow duplicate elements.
Whats the difference between a queue and a stack?
Stack is a data structure that is based on last-in-first-out rule (LIFO), while queues are based on First-infirst-out (FIFO) rule.
What is the Map interface?
The Map interface is used associate keys with values.
What is the Properties class?
The properties class is a subclass of Hashtable that can be read from or written to a stream. It also provides
the capability to specify a set of default values to be used.
Which implementation of the List interface provides for the fastest insertion of a new element into
the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above
ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the
middle of the list the elements that follow the insertion point must be shifted to make room for the new
element. The LinkedList is implemented using a doubly linked list; an insertion requires only the updating
of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions.
The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove()
method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods
only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like
adding and removing the objects.
So Enumeration is used when ever we want to make Collection objects as Read-only.
In object-oriented design, the Singleton design pattern is used to restrict instantiation of a class to one
object. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes
it is generalized to systems that operate more efficiently when only one or a few objects exist.
Implementation Insight
The Singleton pattern is implemented by creating a class with a method that creates a new instance of the
object if one does not exist. If an instance already exists, it simply returns a reference to that object. To
make sure that the object cannot be instantiated any other way, the constructor is made either private or
protected. Note the distinction between a simple static instance of a class and a singleton. Although a
Singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or
resources until needed.
}
}
Notes:
- No lazy instantiation
- Sufficient for allmost all the cases
}
returnuniqueInstance;
}
}