Q 1 What is Collection ? What is a Collections Framework ?
Collection : A collection (also called as container) is an object that groups multiple elements into a single unit.Collections Framework : Collections framework provides unified architecture for manipulating and representing collections.
Collection interface is a member of the Java Collections framework.
Q 3 What is the difference between Collection and Collections ?
Collection is an interface while Collections is a java class , both are present in java.util package and part of java collections framework.
Q 4 What is the Collection Hierarchy in Java ?
The list of important (core) collection interfaces are
Collection , Set , Queue , List , Map
Q 5 What is the difference between List and Set ?
List is ordered but contains duplicate elements.
Set contains only unique elements but it is unordered
List maintains the order in which the objects are added .
Q 6 What is the difference between Map and Set ?
Set contain only unique values.
Map object has unique keys each containing some value
Q 7 What are the classes implementing List and Set interface ?
Class implementing List interface : ArrayList , Vector , LinkedList ,
Class implementing Set interface : HashSet , TreeSet
Q 8 What is an iterator ?
Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.
Q 9 What is the difference between Iterator and Enumeration ?
The main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn't.
Hence , using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it .
Q 10 What is the difference between HashMap and Hashtable ?
It is one of the most popular collections interview question for java developer . Make sure you go through this once before appearing for the interview .
Main differences between HashMap and Hashtable are :
a. Hashtable does not allow null keys and null values. HashMap allows one null key and any number of null values.
b. Hashtable is synchronized or thread-safe .HashMap is not synchronized or thread-safe.
Q 14 Write the code for iterating the list in different ways in java ?
There are two ways to iterate over the list in java :
a. using Iterator
b. using for-each loop
Q 15 Explain the importance of hashCode() and equals() method ? Explain the contract also.
HashMap object uses Key object hashCode() method and equals() method to find out the index to put the key-value pair. If we want to get value from the HashMap same both methods are used .all the collection classes that does not allow the duplicate values use hashCode() and equals() method to find the duplicate elements.So it is very important to implement them correctly
Contract of hashCode() and equals() method
If object1.equals(object2) , thenobject1.hashCode() == object2.hashCode() should always be true.