Top Collection Java Interview Questions

Q) What data structure is used to implement HashSet?

Ans) HashSet implements Hashmap internally to store the data. The data passed to hashset is stored as a key in hashmap with null as value. This can be summarized as HashMap with keys and null value is a hashset.

Q) What is a ConcurrentHashMap?

Ans) A concurrentHashMap is a thread-safe implementation of Map interface. In this class put and remove methods are synchronized but not get method. This class is different from Hashtable in terms of locking; it means that hashtable use object level lock but this class uses bucket level lock thus having better performance. The allowed concurrency among update operations is guided by the optional concurrencyLevel constructor argument (default 16), which is used as a hint for internal sizing. The table is internally partitioned to try to permit the indicated number of concurrent updates without contention. Because placement in hash tables is essentially random, the actual concurrency will vary. Ideally, you should choose a value to accommodate as many threads as will ever concurrently modify the table. Using a significantly higher value than you need can waste space and time, and a significantly lower value can lead to thread contention. But overestimates and underestimates within an order of magnitude do not usually have much noticeable impact. A value of one is appropriate when it is known that only one thread will modify and all others will only read. Also, resizing this or any other kind of hash table is a relatively slow operation, so, when possible, it is a good idea to provide estimates of expected table sizes in constructors.

Q) What is the difference between iterator access and index access?

Ans) Index based access allows access of the element directly on the basis of index. The cursor of the data structure can directly goto the 'nth' location and get the element. It does not have to traverse through n-1 elements.

In Iterator based access, the cursor has to traverse through each element to get the desired element. So to reach the 'nth' element it needs to traverse through n-1 elements.

Insertion, updation or deletion will be faster for iterator based access if the operations are performed on elements present in middle of the data structure.

Insertion, updation or deletion will be faster for index based access if the operations are performed on elements present at the end of the data structure.

Traversal or search in index based data structure is faster.

ArrayList is index access and LinkedList is iterator access.

Q) How to sort a list in reverse order?

Ans) To sort the elements in the reverse natural order of the strings, get a reverse Comparator from the Collections class with reverseOrder(). Then, pass the reverse Comparator to the sort() method.

List list = new ArrayList();
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp)

Q) Can a null element be added to a Treeset or HashSet?

Ans) A null element can be added only if the set is of size 1 because when a second element is added then as per set definition a check is made to check duplicate value and comparison with null element will throw NullPointerException.
HashSet is based on hashMap and can contain null element.

Q) What are advantages of iterating a collection using iterator?

Ans) For loop does not allow updating the colection(add or remove) whereas Iterator does allow modifying the collection. Also Iterator can be used where there is no clue what type of collections will be used because all collections implement Iterator interface.

Q) How to sort a list of strings - case insensitive ?

Ans) Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

Q) How to make a List (ArrayList,Vector,LinkedList) read only ?

Ans) A list implemenation can be made read only using Collections.unmodifiableList(list). This method returns a new list. If a user tries to perform add operation on the new list; UnSupportedOperationException is thrown.

Recommend Reading

  1. How java manages Memory?
  2. Why is it advised to use hashcode and equals together?
  3. Comparable and Comparator in java
  4. How to create Singleton class in Java?
  5. Difference between equals and ==?
  6. When to use abstract class over interface?
  7. Difference between final, finally and finalize
  8. Why is it recommended to use Immutable objects in Java