Chapter 23 - Sets and Maps Flashcards
What is a set?
A set is an efficient data structure for storing and processing nonduplicate elements.
What is a map?
The map data structure is like a dictionary that provides a quick lookup to retrieve a value using a key.
Describe the Set interface.
The Set interface extends the Collection interface. It does not introduce new methods or constants, but it stipulates that an instance of Set contains no duplicate elements. The concrete classes that implement Set must ensure that no duplicate elements can be added to the set.
Three concrete classes of Set are: HashSet, LinkedHashSet, and TreeSet.
What is a HashSet?
The concrete HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
What is the hashCode method?
For efficiency, objects added to a hash set need to implement the hashCode method in a manner that properly disperses the hash code. The hashCode is defined in the Object class. The hash codes of two objects must be the same if the two objects are equal. Two unequal objects may have the same hash code, but you should implement the hashCode method to avoid too many such cases.
How do you create a HashSet?
HashSet has 4 different constructors:
HashSet() Create an empty hash set.
HashSet(c: Collection[? extends E>) Create a hash set from an existing collection.
HashSet(initialCapacity: int) Create a hash set with a specific initial capacity (default = 16).
HashSet(initialCapacity: int, loadFactor: float) The load factor measures how full the set is allowed to be before its capacity is increased (default = 0.75).
What is a LinkedHashSet?
LinkedHashSet extends HashSet with a linked-list implementation that supports an ordering of the elements in the set. The elements in a HashSet are not ordered, bu the elements in a LinkedHashSet can be retrieved in the order in which they were inserted into the set.
Other than that, LinkedHashSet and HashSet are identical. They have identical constructors.
How do you decide whether to use LinkedHashSet or HashSet?
If you don’t need to maintain the order in which the elements are inserted, use HashSet, which is more efficient than LinkedHashSet.
Describe TreeSet.
SortedSet is a subinterface of Set, which guarantees that the elements in the set are sorted.
NavigableSet extends SortedSet to provide navigation methods.
TreeSet implements the NavigableSet interface, therefore inherits from SortedSet as well.
The elements in a TreeSet are always sorted. You can only add objects to a TreeSet as long as they can be compared with each other.
How do you create a TreeSet?
TreeSet() : Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
TreeSet(Collection[? extends E> c) : Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements.
TreeSet(Comparator[? super E> comparator) : Constructs a new, empty tree set, sorted according to the specified comparator.
TreeSet(SortedSet[E> s) : Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.
A tree set has the methods first() and last() available to it. What do they do?
first() returns the first element in the tree set.
last() returns the last element in the tree set.
A tree set has the methods headSet(element) and tailSet(element) available to it. What do they do?
headSet(element) returns the elements in the tree set before the specified element.
tailSet(element) returns the elements in the tree set after the specified element, including the element.
A tree set has the methods lower(e) and higher(e) available to it. What do they do?
lower(e) returns the largest element less than e in the tree set.
higher(e) returns the smallest element greater than e in the tree set.
A tree set has the methods floor(e) and ceiling(e) available to it. What do they do?
floor(e) returns the largest element less than or equal to e in the tree set.
ceiling(e) returns the smallest element greater than or equal to e in the tree list.
How do you decide whether to use a hash set or a tree set?
If you don’t need to maintain a sorted set when updating a set, you should use a hash set, because it takes less time to insert and remove elements in a hash set. When you need a sorted set, you can create a tree set from the hash set.
When should you use sets and when should you use lists?
Lists are useful for accessing elements through the index. Sets are much(!) more efficient than lists for testing whether an element is in a set or a list.
If you don’t want duplicate items but still need to keep the order of the elements, definitely use LinkedHashSet.