- The underlying data structure is Hashtable
- null values are accepted
- duplicates are not allowed
- insertion order is based on hashcode of the object hence insertion order is not preserved
- best suitable if frequent operation is search operations
- HashSet class implements Serializable and Cloneable
- it is implementation class for Set interface
- heterogeneous objects are allowed
- it is introduced in 1.2 version
Q32. If we are trying to insert duplicate values in Set what will happen?
If we are trying to insert duplicate objects to the HashSet , we wont get any compile time or run time errors just the add(Object o) returns false and it doesn’t add that object.
Q33. What is LinkedHashSet?
It is the child class of HashSet. The main difference between HashSet and LinkedHashSet is:
In the case of HashSet insertion order is not preserved , but in the case of LinkedHashSet insertion will be preserved.
Q34. Differences between HashSet and LinkedHashSet?
|1The Underlying datastructure is Hashtable||1The underlying datastructure is combination of LinkedList and Hashtable|
|2Insertion Order is not preserved||2 Insertion order is preserved.|
|3Introduced in 1.2 version||3 Introduced in 1.4 version|
Q35. What are major enhancements in 1.4 version of collection frame work?
Q36. Explain about TreeSet?
It is Collection object which can be used to represent a group of objects according to some sorting order.
- The underlying datastructure is Balanced tree
- Duplicates are not allowed
- All objects are stored according to some sorting order hence insertion order is not preserved
- Heterogeneous objects are not allowed violation leads to ClassCastException
- For an Empty TreeSet as firs element null value can be inserted but after inserting that first value if we are trying to insert any other objects then we will get NullPointerException
- For an non empty TreeSet if we are trying to inser null value at run time u will get NullPointerException
Q37. What are differences between List and Set interfaces?
|1Insertion Order is preserved||1Insertion Order is not preserved|
|2Duplicate Objects are allowed||2 Duplicate Objects are not allowed|
|3The implemented classes are ArrayList,LinkedList , Vector and Stack classes||3 The implemented classes are HashSet, LinkedHashSet and Tree|
Q38. What is Comparable interface?
- This interface can be used for defining natural sorting order of the objects.
- It is present in java.lang package
- It contains a method public int compareTo(Object obj1)
Q39. What is Comparator interface?
- This interface can be used for implementing customized sorting order.
- It is present in java.util package
- It contains two methods
- public int compare(Object ,Object)
- public boolean equals(Object)
Q40. What are differences between Comparable and Comparator?
|1 This can be used for natural sorting order||1This can be used for implementing customized sorting|
|2 This interface present in java.lang package||2 This is present in java.util package|
|3 Contains only one method: public int compareTo(Object obj1)||3 It contains two methods. |
public int compare(Object ,Object)
public Boolean equals(Object)
|4 It is marker interface||4 It is not a marker interface.|