50 Frequently asked Java Collection Interview Questions

The most common module covered in the interview section is Java Collection. This article discusses the most frequently asked Java Collection Package interview questions. This will be useful for both Freshers and Professionals to understand the fundamentals.

1. What is Iterable Interface?

  The iterable interface is the root level interface for collections. The main purpose of this interface is to iterate the collection elements through the iterator method.

2. What is List Interface ?

  • Child Interface of Collection Interface
  • List is an ordered collection of objects.
  • List allows duplicates 
  • ArrayList, Stack, and Vector are classes available at the List Interface.

3. What is Array List ?

  • The size of an ArrayList is increased automatically if the collection grows or shrinks 
  • ArrayList is faster, because it’s not synchronized, hence the data operation will be faster 

4. What is LinkedList ?

  • LinkedList is fast in data manipulation because of its storing mechanism.
  • LinkedList uses a linear data structure where every element is stored in a separate object with a data part and an address part.

 5. What is Vector ?

  • vector is slower because it is synchronized.

6. What is Stack ?

  • Stack implements the stack data structure. Hence, the data operation follows LIFO. 
  • Push, pull, and pop are operations of the stack.
  • Stack is a thread-safe one,so the process would be slow.

7. What is Queue Interface ?

  • Queue maintains a FIFO method for operations, like a real-world queue.
  • PriorityQueue, Deque, and ArrayDeque are implemented classes in Queue

8. What is PriorityQueue ?

  • A PriorityQueue is used when the objects are supposed to be processed based on their priority.
  • Ascending and descending order using comparable elements, 
  • Poll, peek, and add are the methods available in PriorityQueue.
  • Poll will remove the top priority   

9. What is Comparator Interface ?

  • Order the objects into defined classes  
  • It used to compare two objects using the compare method   

10. What is Set ?

  • Set is a unordered collection of objects.
  • Set will not allow duplicates.
  • Implemented classes are HashSet, LinkedHashSet, and TreeSet.

11. What is Hashset ?

  • The HashSet class is an inherent implementation of the hash table data structure. The objects were inserted into the hashtable based on their hashcode.
  • HashSet is not an ordered 
  • Allows Null
  • Does not allow duplicates.
  • HashSet uses Hashmap internally to store and retrieve the data.

12. What is LinkedHashSet ?

  • It uses a doubly linked list to store the elements. 
  • It is an ordered one.
  • Does not allow duplicates

13. Difference Between Enumeration vs Iterator vs ListIterator ?

    Enumeration : 

  • Enumeration is an interface that involves iterating the elements singly.
  • Object can be created by invoking the elements() function.
  • Enumeration applies only to the legacy classes like Hashtable, Vector, and Stack.

   Iterator :

  • Iterator is a universal interface applicable to all the collections.
  • Single-way iteration, removing elements, can be done.
  • Uses iterate() method to create objects.

   ListIterator :

  • ListIterator applies only to implemented classes like ArrayList, LinkedList, Stack.
  • ListIterator provides the iterator in both directions.
  • ListIterator allows the elements to read, remove, replace.  

14. What is SortedSet Interface ?

  • Ordering of the elements
  • Does not allow duplicates.
  • Does not allow null
  • Uses TreeSet classes for Implementation

15. What is TreeSet ?

  • TreeSet uses Tree for storage.
  • Uses Comparator for Ordering

16. Difference Between Comparator Vs Comparable Interfaces ?

     Comparator :

  • It’s an Interface
  • Compare the object based on its attributes.
  • Compare() method to sort the elements.
  • Mainly used when multiple classes are involved, because it compares the class level attributes.

Comparable :

  • It’s an Interface
  • It is used to compare objects with current objects. Simply use this.
  • It uses the CompareTo() method to compare the objects.

17. What is Map ?

  • It is a data structure that follows a key-value pair.
  • Does not allow duplicate key
  • HashMap, TreeMap are the implemented classes of Map interface
  • Can be iterated through EntrySet()

18. What is HashMap ?

  • Using the hashing technique to store the elements.
  • Work basis on the key-value pair.
  • put, get operations will be used to retrieve the data
  • It is faster, not synchronized.
  • Extends AbstractMap

19. What is Hashing ?

  •  Hashing is a technique that converts large strings to small strings so that the indexing, search operations will be faster.

20. What is TreeMap ?

  •  Ordered Map, Comparator will be used to sort the elements.
  •  No Duplicates are allowed.
  •  Elements are sorted in TreeMap
  •  The treemap is faster since it is not synchronized.

21. What is Linkedhashmap ?

  • Extends Hashmap, hence holding all the properties of Hashmap
  • It is an ordered Map.
  • It does not contain duplicates.
  • It is not synchronized.

Related Posts

Leave a Reply

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