Q41. What is difference between HashSet and TreeSet?
HashSet | TreeSet |
1The underlying data structure is Hashtable | 1The underlying data structure is balanced tree |
2Heterogeneous objects are allowed | 2 Heterogeneous objects are not allowed bydefalut |
3Insertion order is not preserved and it is based on hashcode of the objects | 3 Insertion order is not preserved and all the objects are inserted according to some sorting order. |
4null insertion is possible | 4 As the first element only null insertion is possible and in all other cases we will get NullPointerException |
Q42. What is Entry interface?
It is inner interface of Map.
In the Map each key value pair is considered as Entry object.
interface Map
In the Map each key value pair is considered as Entry object.
interface Map
{
//more code here
interface Entry
//more code here
interface Entry
{
Object getKey()
Object getValue()
Object setValue(Object new)
}
}
Object getKey()
Object getValue()
Object setValue(Object new)
}
}
Q43. 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
Q44. 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
Q45. Differences between HashMap and LinkedHashMap ?
HashMap | LinkedHashMap |
1.The underlying data structure is Hashtable | 1.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 | 2 Insertion order is preserved |
3.Introduced in 1.2 version | 3 Introduced in 1.4 version. |
Q46. Differences between HashMap and Hashtable?
HashMap | Hashtable |
1.The underlying data structure is Hashtable | 1.The underlying data structure of Hashtable |
2.No method is synchronized and hence HashMap object is not thread safe | 2 .All methods are synchronized and hence it is thread safe |
3.Performance is high | 3. Performance is low |
4.null insertion is possible for both keys and values | 4. 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 | 5. Introduced in 1.0 version and it is legacy |
Q47. 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.
Q48. What is difference between HashMap and IdentityHashMap?
Refer Q47 for the answer.
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
Q50. What is difference between HashMap and WeakHashMap?
Refer Q49 for the answer.
0 comments:
Post a Comment