Java Collections Interview Questions and Answers

Java Collection Interview Questions

1) Is Collection is an Interface or Class?

A) A collection is an Interface.

2) What is the difference between collection and collections?

A) Collection: Collection is an interface in java.util package used to represent a group of as a single entity.

Collections: Collections is a class in java.util package. This class consists exclusively of static methods that operate on or return Collections.

3) What are the Collections API?

A) The Collections API is a set of classes and interfaces that support operations on the collection of objects.

4) What is a Cursor in Java?

A) If we want to receive objects one by one from the Collection then we should use Cursor in Java.

5) How many Cursors are there in Java?

A) There are three cursors available in Java. They are:
1) Enumeration.
2) Iterator.
3) List Iterator.

6) What is an Iterator?

A) The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a collection. Iterators are a generic way to go through all the elements of a collection no matter how it is organized. An iterator is an interface implemented a different way for every Collection.

7) How do you traverse through a collection using its Iterators?

A) To use an Iterator to traverse through the contents of a collection, follow this step:
a) Obtain an Iterator to the start of the collection by calling the collection iterator() method.
b) Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.
c) Within the loop, obtain each element by calling next().

8) How do you remove elements during Iterator?

A) Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.

9) What is the List Iterator?

A) List Iterator is just like Iterator, except that it allows us to access the collection in either the forward or backward direction and lets us modify an element.

10) What is an Enumeration?

A) The Enumeration interface defines the methods by which you can enumerate ( obtain one at a time ) the elements from legacy collection objects. We can create an Enumeration object by using the elements method.
Example: Enumeration e = v.elements();

11) What are the methods present in the Enumeration Interface?

A) There are three methods in Enumeration Interface. They are:
1) asIterator().
2) hasMoreElements().
3) nextElement().

12) What are the methods present in Iterator?

A) There are four methods present in Iterator. They are:
1) hasNext().
2) next().
3) remove().
4) forEachRemaining( Consumer action).

13) What are the methods present in List Iterator?

A) There are nine methods present in List Iterator. They are:
1) add( E e).
2) hasNext().
3) hasPrevious().
4) next().
5) nextIndex().
6) previous()
7) previousIndex().
8) remove().
9) set( E e).

14) How Enumeration can be converted into an Iterator?

A) An Enumeration can be converted into an Iterator by using the Enumeration.asIterator() method.

15) What is the Comparable Interface?

A) Comparable Interface is used to order the Objects of user-defined Class. It provides only a single sorting sequence.

16) What is the Comparator?

A) A comparator is an interface present in java. util package. The comparator object reference can be used to define our own customized sorting order.

17) What is Sorted Map?

A) Sorted Map is a subinterface of Map. If we want to represent a group of key-value pairs according to the sorting order of keys. In a Sorted Map we can perform sorting only based on the keys but not values.

18) Which interface provides the capability to store objects using a key-value pair?


19) What is the use of descending keyset()?

A) The descending keyset() method returns a NavigableSet in which the order of the elements is reversed compared to the original key set.

20) What is the use of descendingMap()?

A) The descendingMap() method returns a NavigableMap which is a view of the original Map. The order of the elements in this view map is reverse of the order of the original map.

21) What is the use of headMap()?

A) The headMap() method returns a view of the original NavigableMap which only contains elements that are less than the given element.

22) What is the difference between HashSet and LinkedHashSet?

A) HashSet does not maintain Insertion order of Objects. LinkedHashSet maintains Insertion order of objects.

23) What is the difference between the List and Set?

A) The list can contain duplicate elements whereas Set contains unique elements only.

24) What is a Stack?

A) A stack is a child class of vector. If we want to represent object according to last in first out order then we should go far, Stack.

25) What is ceiling key?

A) The ceilingKey() method returns the least(smallest) key in the Map that is greater than or equal to the element passed as a parameter to the ceilingKey() method.

26) What is PollFirstEntry?

A) The PollFirstEntry() method returns and removes the first entry (key + values) in the NavigableMap or null if the map is empty.

27) What is the use of PollLastEntry?

A) The PollLastEntry returns and removes the last element in the map or null if the map is empty.

28) What is the difference between HashMap and WeakHashMap?

A) HashMap: Object is not eligible for garbage collection if it is associated with Hashmap. Even though it does not have any external references.

WeakHashMap: If an Object is not having any external references then it is always eligible for garbage collector even though it is associated with WeakHashMap.

29) What is the List Interface?

A) The List interface provides support for ordered collections of objects. Lists may contain duplicate elements.

30) What is the main implementation of the List interface?

A) The main implementation of the List interface are as follows:
Array List: Resizable-array implementation of the List interface. The best all-around implementation of the List interface.
Vector: Synchronized resizable-array implementation of the List interface with additional “Legacy methods”.
Linked List: Doubly-linked list implementation of the List interface. May provide better performance than the Array list implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques ).

31) What is the advantage of Array List over arrays?

A) Some of the advantages Array List has over arrays are:
• It can grow dynamically.
• It provides more powerful insertion and search mechanisms than arrays.

32) Difference between Array List and Vector?
Array List:
1) Array List is Not synchronized by default.
2) Array List can use the only Iterator to access the elements.
3) The Array List increases its array size by 50 percent if it runs out of the room.
4) Array List has no default size.

1) Vector List is synchronized by default.
2) Vector list can use the Iterator and Enumeration Interface to access the elements.
3) A Vector defaults to doubling the size of its array if it runs out of the room.
4) While vector has a default size of 10.

33) How to obtain Array from an Array List?

A) An array can be obtained from an Array List using toArray() method on Array List.
List a = new ArrayList();
Object a[] = a.toArray();

34) How do you decide, when to use Array List and when to use Linked List?

A) If you need to support random access, without inserting or removing elements from any place other than the end, then Array List offers the optimal collection. If however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then the linked list offers better implementation.

35) What is the Set Interface?

A) The Set Interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. Contains no methods other than those inherited from the collection. It adds the restriction that duplicate elements are prohibited. Two Set objects are equal if they contain the same elements.

36) What are the main implementations of the Set Interface?

A) The main implementation of the Set interface is as follows:
• HashSet
• TreeSet
• Linked HashSet
• EnumSet

37) What is a HashSet?

A) A HashSet is an unsorted, unordered Set. It uses the hash code of the object being inserted (so the more efficient your hashcode() implementation, the better access performance you’ll get). Use this class when you want a collection with no duplicates and you don’t care about the order when you iterate through it.

38) What is a TreeSet?

A) TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time.

39) What is an EnumSet?

A) An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.

40) What is the difference between HashSet and TreeSet?
A) HashSet: HashSet is under Set interface i.e. it does not guarantee for either sorted order or sequence order.

TreeSet: TreeSet is under Set i.e. it provides elements in sorted order(acceding order).

41) What is a Map?

A) A map is an object that stores associations between keys and values (key/value pairs). Given a key, you can find its value. Both keys and values are objects. The key must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot.

42) What is the main Implementation of the Map Interface?

A) The main implementation of the List Interface is as follows:
• HashMap
• HashTable
• TreeMap
• EnumMap

43) What is the difference between HashMap and when to use TreeMap?
A) HashMap:
1) HashMap lets you have null values as well as one null key.
2) The iterator in the HashMap is fall-safe (If you change the map while iterating, you’ll know).
3) HashMap is unsynchronized.

1) Hashtable does not allow null values as key and value.
2) The enumerator for the HashTable is not fall-safe.
3) HashTable is synchronized.

44) How does a Hashtable internally maintain the key-value pairs?

A) TreeMap actually implements the SortedMap interface which extends the Map interface. In a Treemap, the data will be sorted in acceding order of keys according to the natural order for the key’s class, or by the comparator provided at creation time. Treemap is based on the Red-Black tree data structure.

45) What are the different collection views that Maps provide?

A) A map provides three Collection Views.
• Key Set – allow a map’s contents to be viewed as a set of keys.
• Values collection – allow a map’s contents to be viewed as a set of values.
• Entry Set – allow a map’s contents to be viewed as a set of key-value mappings.

46) What is a Keyset view?

A) The keyset is a set returned by the KeySet() method of the Map Interface, it is a set that contains all the keys present in the Map.

47) What is a values Collection Views?

A) Values Collection Views is a collection returned by the Values() method of the Map Interface, it contains all the objects present as values in the map.

48) What is an Entry Set View?

A) Entry Set View is a set that is returned by the entryset() method in the map and contains objects of type map. Entry, each of which has both Key and Value.

49) How do you sort an ArrayList (or any list) of user-defined objects?

A) Create an implementation of the java.lang.A comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, comparator).

Leave a comment

Your email address will not be published. Required fields are marked *