Most asked Collection framework Interview questions
What is the Collection framework in Java?Â
Collection of the objects is nothing but the Group of any individual objects which are represented as a single unit is collection.In java, collection represents a group of objects as a single unit.
Explain the hierarchy of the Collection framework in Java.
What are the advantages of the Collection framework?
-
Reduces programming effort
-
Increases program speed and quality
-
Allows interoperability among unrelated APIs
-
Reduces effort to learn and to use new APIs
-
Reduces effort to design new APIs
-
Fosters software reuse
Explain the various interfaces used in the Collection framework.
-
CollectionÂ
-
SetÂ
-
List
-
Queue
-
Deque
-
Map
-
SortedSetÂ
-
SortedMap
Â
What are the main differences between array and collection?
Array |
Collection |
Array can only store same type of objects(i.e. Homogeneous objects) |
Collection can store different types of objects |
Arrays donât have any inbuilt methods to be used for different operations like searching, sorting etc |
Collection framework has different inbuilt methods which can be used for several operations. |
Array is static |
Collection is dynamic |
ArrayList vs LinkedList vs Vector
Array List |
LinkedList |
Vector |
The ArrayList class is a resizable array |
The LinkedList class is a collection which can contain many objects of the same type |
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. |
ArrayList is not synchronized. |
LinkedList is not synchronized. |
Vector is synchronized. |
Arraylist is not thread-safe |
Linkedlist is not thread-safe |
Vector list is thread-safe |
ArrayList is not a legacy class. |
LinkedList is not a legacy class. |
Vector is a legacy class. |
What is the difference between List and Set?
List |
Set |
The List interface in Java provides a way to store the ordered collection which maintains the insertion order. It is a child interface of Collection. |
Set is an unordered collection which does not preserve the insertion order. |
The List can contain duplicate elements |
Set includes unique items. |
What is the difference between HashSet and TreeSet?
Â
HashSet |
TreeSet |
HashSet is backed by HashMap |
TreeSet is backed by TreeMap |
HashSet performs faster than TreeSet. |
TreeSet performs slower than HashSet. |
HashSet maintains no order |
TreeSet maintains ascending order. |
What is the difference between Set and Map?
Set |
Map |
Set contains only values |
Map contains key and values both. |
Set contains only unique values |
Maps can contain unique Keys with duplicate values. |
Set holds a single number of null value |
Map can include a single null key with n number of null values |
What is the difference between HashSet and HashMap?
HashSet |
HashMap |
HashSet cannot have any duplicate value |
HashMap can contain duplicate values with unique keys. |
HashSet contains the only single number of null value |
HashMap can hold a single null key with n number of null values. |
HashSet implements Set interface |
HashMap implements the Map interface |
 What is the difference between HashMap and TreeMap?
Â
HashMap |
TreeMap |
HashMap maintains no order |
TreeMap maintains ascending order. |
HashMap can be sorted by Key or value |
TreeMap can be sorted by Key. |
HashMap is implemented by hash table |
TreeMap is implemented by a Tree structure. |
HashMap may contain a null key with multiple null values |
TreeMap cannot hold a null key but can have multiple null values. |
What is BlockingQueue?
java.util.concurrent.BlockingQueue is a java Queue. It provides concurrency in the operations like retrieval, insertion, deletion. It supports operations that wait for the queue to become non-empty when retrieving and removing an element, and wait for space to become available in the queue when adding an element.
What is the advantage of the Properties file?
Java Properties files are useful resources to add information in Java. If any information is changed from the properties file, you don't need to recompile the java class. It is used to store information which is to be changed frequently.
What is the hashCode() method?
The hashCode() method is used to generate the hash values of objects.Using these hash values, these objects are stored in Java collections such as HashMap, HashSet and HashTable.
What is the Dictionary class?
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value. Given a Dictionary and a key, the associated element can be looked up. Any non-null object can be used as a key and as a value.
These are some most important questions asked from the topic collection framework in java. These will give you a short revision of collection framework.