Frequently asked best and advanced serialization and Java collections interview questions and answers for freshers, 2-6 year experienced Java Developers with examples.
1) Explain Java Collections framework and its interfaces in hierarchy ?
The Collection is an object that contain group of objects called elements. Collection is the main interface and it is the root interface in the collection hierarchy. Collections Classes are categorized into three based on the interface implemented by them. They are List, Set and Map.
List : The List is an ordered collection which allows duplicates. We can insert values at the end of a list or at a specified index. Main implementations of List interface are
|Implemented as a resizable array.||Synchronized ArrayList||Implemented as a double Linked list.|
|Fast Iteration and Random Access||Fast Random Access.||Fast Insertion and deletion.|
|Grow 50% of its size each time||Each time doubles its array size.||Add and Remove both End.|
Set : The set interface allows only unique values. It won't contain duplicates. Major implementations of set interface are
|It's backed by HashMap.||It's backed by Hashtable.|
|It's not ordered.||Sorted order.|
|It allows null object.||Not allows null object.|
|Comparing objects using equals method.||uses compareTo() method.|
Map : Maps provide a key value pair, where keys are unique. Values are mapped to keys. Maps and Keys are objects. Main implementations of Map interface are
|Not synchronized||Synchronized||Not Synchronized|
|Better performance.||Comparatively less performance because of synchronization.||Comparatively less performance because of sorting.|
|Not sorted||Not sorted||Sorted|
|One null key and multiple null values allowed.||Won't allow null object.|
2) How hashmap work in Java ?
It works on the principle of Hashing. Hashmap have put(key,value) and get(key) methods for storing and retrieving data. Key value pairs are stored in "Buckets". Each bucket has a unique number allocated to it. When we pass a key-value pair, the internal implementation of hashmap calls hashcode method which returns a hashcode corresponding to the key. Then hashmap look at the buckets whose unique identifier matches with the hashcode of the key value pair. When it finds a match, it stores the data in that bucket. A bucket can have multiple Key value pairs. When we want to retrieve data from the map by passing the key, it will have a look at the hash code of the key to identify the bucket. Once the bucket is identified, it uses equals() method to retrieve the appropriate key in the bucket.
3) Explain in detail the difference between HashMap, HashTable and Synchronized HashMap ?
|Better performance.||Comparatively less performance because of synchronization.||Same ad HashTable, less performance due to synchronization.|
4) Difference between Set and List in Java ?
|Order||Ordered||Depends on implementation|
5) How can Arraylist be synchronized without using Vector ?
Arraylist can be synchronized using
Other collections can be synchronized:
Collection.synchronizedMap (Map map) and Collection.synchronizedCollection (Collection c)
6) Can you explain about the priority Queue in Java ?Priority queue uses an unbounded Queue (Which makes sure that no tasks are missed) based on priority heap. Sometimes, it uses large bounded queues to hold the huge no of tasks that an application encounters during runtime. Depending on the type of constructor called at runtime, priority queue elements are ordered based on the natural order or by a Comparator. Null's are not allowed in the priority queue.