InterviewSolution
Saved Bookmarks
| 1. |
What is a HashSet? How is it implemented? |
|
Answer» A SET is a collection of UNORDERED and UNIQUE elements. While adding an element a HashSet uses the hashCode() and equals() method to determine if the element already exists in the Set. If it doesn’t exist, it adds the element into the set. A HashSet uses a HashMap internally which uses the element as the key and keeps a fixed item as the VALUE. If the element exists in the HashMap as a key, it simply returns with a boolean value false denoting it already exists in the set, otherwise puts the element in the map and returns TRUE. |
|