In this article series, we will discuss different types of questions that can be asked in a Java interview, in order for the employer to test your skills in Java and object-oriented programming. This article is dedicated to Java Collections.
- 1 Java Collections Interview Questions
- 1.0.1 What are the basic interfaces of Java Collections Framework?
- 1.0.2 Why doesn’t Collection extend Cloneable and Serializable interfaces?
- 1.0.3 What is an Iterator?
- 1.0.4 What differences exist between Iterator and ListIterator?
- 1.0.5 What is the difference between fail-fast and fail-safe?
- 1.0.6 How does HashMap work in Java?
- 1.0.7 What is the importance of hashCode() and equals() methods?
- 1.0.8 What differences exist between HashMap and Hashtable?
- 1.0.9 What is the difference between Array and ArrayList? When will you use Array over ArrayList?
- 1.0.10 What is the difference between ArrayList and LinkedList?
- 1.0.11 What is Comparable and Comparator interface?
- 1.0.12 What is Java Priority Queue?
- 1.0.13 What do you know about the big-O notation and can you give some examples with respect to different data structures?
- 1.0.14 What is the tradeoff between using an unordered array versus an ordered array?
- 1.0.15 What are some of the best practices relating to the Java Collection framework?
- 1.0.16 What’s the difference between Enumeration and Iterator interfaces?
- 1.0.17 What is the difference between HashSet and TreeSet?
- 1.1 Related
Java Collections Interview Questions
1What are the basic interfaces of Java Collections Framework?
Java Collections Framework provides a well-designed set of interfaces and classes that support operations on a collection of objects. The most basic interfaces that reside in the Java Collections Framework are:
2Why doesn’t Collection extend Cloneable and Serializable interfaces?
The Collection interface specifies groups of objects known as elements. Each concrete implementation of a Collection can choose its own way of how to maintain and order its elements. Some collections allow duplicate keys, while some other collections don’t. The semantics and the implications of either cloning or serialization come into play when dealing with actual implementations. Thus, the concrete implementations of collections should decide how they can be cloned or serialized.
3What is an Iterator?
The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the Iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.
4What differences exist between Iterator and ListIterator?
The differences between these elements are listed below:
- An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over List.
- The Iterator can traverse a collection only in forward direction, while the ListIterator can traverse a List in both directions.
- The ListIterator implements the Iterator interface and contains extra functionality, such as adding an element, replacing an element, getting the index position for previous and next elements, etc.
5What is the difference between fail-fast and fail-safe?
The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes in java.util package is fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such an exception.
6How does HashMap work in Java?
A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists, its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.
7What is the importance of hashCode() and equals() methods?
A HashMap in Java uses the hashCode and equals methods to determine the index of the key-value pair. These methods are also used when we request the value of a specific key. If these methods are not implemented correctly, two different keys might produce the same hash value and thus, will be considered as equal by the collection. Furthermore, these methods are also used to detect duplicates. Thus, the implementation of both methods is crucial to the accuracy and correctness of the HashMap.
8What differences exist between HashMap and Hashtable?
Both the HashMap and Hashtable classes implement the Map interface and thus, have very similar characteristics. However, they differ in the following features:
- A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow either null keys, nor null values.
- A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments.
- A HashMap provides its set of keys and a Java application can iterate over them. Thus, a HashMapis fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys.
- The Hashtable class is considered to be a legacy class.
9What is the difference between Array and ArrayList? When will you use Array over ArrayList?
The Array and ArrayList classes differ on the following features:
- Arrays can contain primitive or object, while an ArrayList can contain only objects.
- Arrays have fixed size, while an ArrayList is dynamic.
- An ArrayList provides more methods and features, such as addAll, removeAll, iterator, etc.
- For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this approach makes them slower when working on fixed size primitive data types.
10What is the difference between ArrayList and LinkedList?
Both the ArrayList and LinkedList classes implement the List interface, but they differ on the following features:
- An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a performance equal to O(1). On the other hand, a LinkedList stores its data as a list of elements and every element is linked to its previous and next element. In this case, the search operation for an element has execution time equal to O(n).
- The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the index when an element is added in some arbitrary position inside the collection.
- A LinkedList consumes more memory than an ArrayList, because of every node in a LinkedList stores two references, one for its previous element and one for its next element. Check also our article ArrayList vs. LinkedList.
11What is Comparable and Comparator interface?
List their differences. Java provides the Comparable interface, which contains only one method, called compareTo). This method compares two objects, in order to impose an order between them. Specifically, it returns a negative integer, zero, or a positive integer to indicate that the input object is less than, equal to or greater than the existing object. Java provides the Comparator interface, which contains two methods, called compare) and equals). The first method compares its two input arguments and imposes an order between them. It returns a negative integer, zero, or a positive integer to indicate that the first argument is less than, equal to, or greater than the second. The second method requires an object as a parameter and aims to decide whether the input object is equal to the comparator. The method returns true, only if the specified object is also a comparator and it imposes the same ordering as the comparator.
12What is Java Priority Queue?
The PriorityQueue is an unbounded queue, based on a priority heap and its elements are ordered in their natural order. At the time of its creation, we can provide a Comparator that is responsible for ordering the elements of the PriorityQueue. A PriorityQueue doesn’t allow null values, those objects that don’t provide natural ordering or those objects that don’t have any comparator associated with them. Finally, the Java PriorityQueue is not thread-safe and it requires O(log(n)) time for its enqueuing and dequeuing operations.
13What do you know about the big-O notation and can you give some examples with respect to different data structures?
The Big-O notation simply describes how well an algorithm scales or performs in the worst case scenario as the number of elements in a data structure increases. The Big-O notation can also be used to describe other behaviours such as memory consumption. Since the collection classes are actually data structures, we usually use the Big-O notation to chose the best implementation to use, based on time, memory and performance. Big-O notation can give a good indication of performance for large amounts of data.
14What is the tradeoff between using an unordered array versus an ordered array?
The major advantage of an ordered array is that the search times have a time complexity of O(log n), compared to that of an unordered array, which is O (n). The disadvantage of an ordered array is that the insertion operation has a time complexity of O(n) because the elements with higher values must be moved to make room for the new element. Instead, the insertion operation for an unordered array takes constant time of O(1).
15What are some of the best practices relating to the Java Collection framework?
- Choosing the right type of the collection to use, based on the application’s needs, is very crucial for its performance. For example, if the size of the elements is fixed and know a priori, we shall use an Array, instead of an ArrayList.
- Some collection classes allow us to specify their initial capacity. Thus, if we have an estimation of the number of elements that will be stored, we can use it to avoid rehashing or resizing.
- Always use Generics for type-safety, readability, and robustness. Also, by using Generics you avoid the ClassCastException during runtime.
- Use immutable classes provided by the Java Development Kit (JDK) as a key in a Map, in order to avoid the implementation of the hashCode and equals methods for our custom class.
- Program in terms of interface not implementation.
- Return zero-length collections or arrays as opposed to returning a null in case the underlying collection is actually empty.
16What’s the difference between Enumeration and Iterator interfaces?
Enumeration is twice as fast as compared to an Iterator and uses very less memory. However, the Iterator is much safer compared to Enumeration, because other threads are not able to modify the collection object that is currently traversed by the iterator. Also, Iterators allow the caller to remove elements from the underlying collection, something which is not possible with Enumeration.
17What is the difference between HashSet and TreeSet?
The HashSet is implemented using a hash table and thus, its elements are not ordered. The add, remove and contains methods of a HashSet have constant time complexity O(1). On the other hand, a TreeSet is implemented using a tree structure. The elements in a TreeSet are sorted, and thus, the add, remove and contains methods have a time complexity of O(logn).