Set Flashcards
What is a set
set in Java is an unordered collection of elements that cannot contain duplicate values.
Mention methods in Set interface
add(E element): Adds the specified element to the set if it is not already present.
remove(Object o): Removes the specified element from the set if it is present.
contains(Object o): Returns true if the set contains the specified element.
size(): Returns the number of elements in the set.
isEmpty(): Returns true if the set contains no elements.
clear(): Removes all elements from the set.
iterator(): Returns an iterator over the elements in the set.
Definition of hash set
HashSet: Use when you need a fast, unordered set and the order of elements is not important.
Definition of linked hash set
LinkedHashSet: Use when you need to maintain the order of insertion, and you also want fast access times.
Definition of Tree set
TreeSet: Use when you need elements to be sorted and require additional navigational methods, such as finding the smallest or largest element.