Java Collections Interview Questions with Answers - Part 3
Q1. What is difference between HashSet and TreeSet?
HashSet | TreeSet | |
---|---|---|
1 | The underlying data structure is Hashtable | The underlying data structure is balanced tree |
2 | Heterogeneous objects are allowed | Heterogeneous objects are not allowed by defalut. |
3 | Insertion order is not preserved and it is based on hashcode of the objects | Insertion order is not preserved and all the objects are inserted according to some sorting order. |
4 | null insertion is possible. | As the first element only null insertion is possible and in all other cases we will get NullPointerException |
Q2. What is Entry interface?
It is inner interface of Map.
In the Map each key value pair is considered as Entry object.
interface Map{ //more code here interface Entry{ Object getKey() Object getValue() Object setValue(Object new) } }
Q3. Explain about HashMap?
It is a Map Object which can be used used to represent a group of objects as key-value pairs.
- The underlying data structure is Hashtable
- Duplicaes keys are not allowed duplicate values are allowed
- Insertion order is not preserved because insertion is based on hashcode of keys.
- Heterogeneous objects are allowed for both keys and values
- null key is allowed only once
- null values are allowed multiple times
- Introduced in 1.2 version
Q4. Explain about LinkedHashMap?
It is child class of HashMap. It is exactly same as HashMap except the following difference.
In the case of HashMap the insertion order is not preserved but in the case of LinkedHashMap insertion order is preserved. Introduced in 1.4 version
Q5. Differences between
HashMap and LinkedHashMap ?
HashMap | LinkedHashMap | |
---|---|---|
1 | The underlying data structure is Hashtable | The underlying data structure is a combination of Hashtable and linkedlist |
2 | Insertion order is not preserved and it is based on hashcode of keys | Insertion order is preserved |
3 | Introduced in 1.2 version | Introduced in 1.4 version. |
Q6. What is IdentityHashMap?
It is exactly same as HashMap except the following difference.
In the HashMap JVM uses equals() method to identify duplicate keys but in the case of IdentityHashMap JVM uses == operator for this.
Q7. Differences between HashMap and Hashtable?
HashMap | Hashtable | |
---|---|---|
1 | The underlying data structure is Hashtable | The underlying data structure of Hashtable |
2 | No method is synchronized and hence HashMap object is not thread safe | All methods are synchronized and hence it is thread safe |
3 | Performance is high | Performance is low |
4 | null insertion is possible for both keys and values | null insertion is not possible for both key and value violation leads to NullPointerException |
5 | Introduced in 1.2 version and it is non legacy | Introduced in 1.0 version and it is legacy |
Q8. What is WeakHashMap?
It is exactly same as HashMap except the following difference.
In case of HashMap an Object is not eligible for garbage collection if it is associated with HashMap even though it dosent have any external references. ie HashMap dominates garbage collector.
But in case of WeakHashMap , if an Object is not having any external references then it is always eligible for garabage collectoion even though it is associated with
weakHashMap. ie garbage collector dominates WeakHashMap
Q9. What is TreeMap?
TreeMap can be used to store a group of objects as key-value pairs where all the entries are arranged according to some sorting order of keys.
- The underlying data structure is RED-BLACK Tree
- Duplicates keys are not allowed but values can be duplicated.
- Insertion order is not preserved because insertion is based on some sorting order
- If we are depending on Natural sorting order then keys should be homogeneous(violation leads to ClassCastException) but values need not homogeneous
- In case of customized sorting order we can insert heterogeneous keys and values
- For empty TreeMap as first entry with null values are allowed but after inserting that entry if we are trying to insert any other entry we will get NullPointerException
- For non empty TreeMap if we are trying to insert null keys we will get NullPointerException
- There are no restrictions for null values.
Q10. What is Hashtable
Hashtable is a legacy Map and can be used to store objects as key value pairs.
- The underlying data sturucture is Hashtabe
- Duplicates keys are not allowed but duplicate values are allowed
- null insertion is not possible for both keys and values
- all methods are synchronized
- insertion order is not preserved because it is based on hashcode of keys
- heterogeneous Objects are allowed for both keys and values
- introduced in 1.0 version it is legacy class
Q11. What is PriorityQueue?
It represents a data structure to hold group of individual objects prior to processing based on some priority .it can be natural sorting order and it can be customized sorting order described by Comparator.
It is the implementation class of Queue interface.
- Insertion order is not preserved because here insertion is done based on some sorting order
- Duplicates are not allowed
- null insertion is not possible even as first element also
- If we are depending on natural sorting order Objects should be homogeneous violation leads to ClassCastException
- If we are depending on customized sorting order Objects can be heterogeneous also.
Q12. What is Arrays class?
- It is utility class for arrays.
- It defines several utility methods for arrays like sorting an array or searching an element in array
- present in java.util package
Q13. Why ArrayList is faster than Vector?
All methods present in the Vector are synchronized and hence any method can be executed by only one thread at a time. It slows down the execution.
But in ArrayList, no method is synchronized and hence multiple thread are allowed execute simultaneously which speed up the execution.
Q14. We are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList?
ArrayList, It is because in LinkedList methods are synchronized and hence any method can be executed by only one thread at a time. It slows down the execution.
But in ArrayList, no method is synchronized and hence multiple thread are allowed execute simultaneously which speed up the execution.
Q15. How to synchronize ArrayList?
We can synchronize ArrayList in two ways.
Using Collections.synchronizedList() method
Using CopyOnWriteArrayList<T>
import java.util.*; public class SyncronizeArrayList { public static void main(String args[]) { // Non Synchronized ArrayList List<String> fruitList = new ArrayList<String>(); fruitList.add("Apple"); fruitList.add("Banana"); fruitList.add("Cherries"); fruitList.add("Dragonfruit"); fruitList.add("Elderberry"); // Synchronizing ArrayList in Java furitList = Collections.synchronizedList(fruitList); // we must use synchronize block to avoid non-deterministic behavior synchronized (fruitList) { Iterator<String> itr = fruitList.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } } }
Output :
Apple
Banana
Cherries
Dragonfruit
Elderfruit
Recommended Posts
Java Collections Interview Questions Part 1
Java Collections Interview Questions Part 2
Core Java Important Questions Part 1
Core Java Important Questions Part 2
Core Java Important Questions Part 3
Core Java Important Questions Part 4
Core Java Important Questions Part 5
Core Java Important Questions Part 6
Core Java Important Questions Part 7
Core Java Important Questions Part 8
Core Java Important Questions Part 9
Core Java Important Questions Part 10
Java MCQs with Answers Set III
If you have any doubts join the discussion below ,our Moderator will reply to your Queries