Here I am going to sharing list of Java Collection Framework Interview Questions and Answers. Which are asking in the interview frequently.
Java Collection Framework Interview Questions and Answers
A collection framework is a class, library to handle group of objects. Collection framework is implemented in java.util package.
A collection object stores references of other objects.
No. Collections store only objects.
Both are useful to retrieve elements from a collection.
Iterator
- Iterator can retrieve the only in forward direction.
- Iterator can be used in List, Set and Queue.
ListIterator
- ListIterator can retrieve the elements in forward and backward direction also. So ListIterator is preferred to Iterator.
- ListIterator can be used in List only.
Both are useful to retrieve elements from a collection.
Iterator
1. Iterator has methods whose names are easy to follow.
2. Iteration has an option to remove element from the collection
Enumeration
1. Enumeration has method whose names are difficult to follow and to remember
2. Enumeration has an no option to remove element from the collection
So Iterator is preferred to Enumeration .
Converting a primitive data type into a object form automatically is called Auto Boxing.
Auto Boxing is done in generic types.
ArrayList
1. ArrayList object is not synchronized by default.
2. In case of a single thread, using ArrayList is faster than Vector.
3. ArrayList increases its size every time by 50 percent (Half)
Vector
1. Vector object is synchronized by default.
2. In case of a multiple threads, using Vector advisable. With a single thread, Vector becomes slow
3. Vector increases its size every time by doubling it.
Yes. We can use synchronizedList() method to synchronize the ArrayList like as follows.
Collections.synchronizedList(new ArrayList());
HashMap
- HashMap object is not synchronized by default.
- In case of single thread, using HashMap is faster than Hashtable
- HashMap allows null keys and null values to be stored. ( one null key and more null values allowed)
- Iterator in the HashMap is full-fast. This means Iterator will produce exception if concurrent updates are made to the HashMap.
Hashtable
- Hashtable object is synchronized by default.
- In case of multiple threads, using Hashtable is advisable. With a single thread, Hashtable becomes slow.
- Hashtable does not allow null keys and null values to be stored.
- Enumeration for the Hashtable is not full-fast. This means if concurrent updates are done to the Hashtable. There will not be any incorrect results produced by the Enumeration.
Load Factor is 0.75
The default initial capacity of the HashMap will be taken as 16 and the load factor as 0.75. Load factor represents at what level HashMap capacity should be doubled.
Example
The product of capacity and load factor = 16 * 0.75 = 12
This represents that after storing the 12th key-value pair into HashMap, its capacity will become 32.
Yes.
We can make HashMap object synchronized using synchronizedMap() method as shown here.
Collections.synchronizedMap(new HashMap)
Set
- A Set represents a collection of elements. Order of the elements may change in the set.
- Set will not allow duplicate values to be stored.
- Accessing elements by their index (position number) is not possible in case of sets.
- Sets will not allow null elements.
List
- A List represents ordered collection of elements. List preserves the order of the elements in which they are entered.
- List will allow duplicate values to be stored.
- Accessing elements by their index (position number) is possible in case of List.
- List will allow null elements to be stored.
Set
Set contains values only.
Map
Map contains key and values both.
HashMap
It does not follow order to store elements
TreeMap
It follow ascending order by default to store elements.
Collection
- Collection is an interface whereas
- Collection interface provides normal functionality of data structure to List, Set and Queue
Collections
- Collections is a class. .
- Collections class is to sort and synchronize collection elements.
Two different keys with the same hash value is known as hash-collision. Two different entries will be kept in a single hash bucket to avoid the collision.
Collections class provides methods to make List, Set or Map elements as synchronized.
- public static List synchronizedList(List l){}
- public static Set synchronizedSet(Set s){}
- public static SortedSet synchronizedSortedSet(SortedSet s){}
- public static Map synchronizedMap(Map m){}
- public static SortedMap synchronizedSortedMap(SortedMap m){}
Comparable
- Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
- Class whose objects to be sorted must implement this interface.
- It provides one method named compareTo().
- It is found in java.lang package. i.e java.lang.Comparable
- Collections.sort(List). Here objects will be sorted on the basis of CompareTo method
Comparator
- Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc.
- Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface.
- It provides one method named compare().
- It is found in java.util package. i.e java.util.Comparator
- Collections.sort(List, Comparator). Here objects will be sorted on the basis of Compare method in Comparator
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.