4. Sets and Maps
4.1 - Sets
- Set extends Collection
- An set can contain NO duplicates.
- Hashset
- Capacity is doubled when the (capacity * load factor) is reached
- Load Factor is number between 0.00 and 1.00
- The bigger the load factor decreases space costs, but increases search time. (0.75 is a good middle)
- Used to store duplicate-free items.
- Objects added to a hash-set needs to implement the hashCode.
- No order is imposed.
- Use for-each to traverse all elements in the list.
- All methods from Collection can be used.
- LinkedHashSet
- Hashset with an linked list implementation to support ordering of items.
- Remembers the order in which te items are inserted, does not store duplicates.
- TreeSet
- SortedSet is a subinterface of set, which guarentees that the items are sorted.
- NavigableSet implements SortedSet to provide navigation methods.
- TreeSet implements SortedSet,
- If an TreeSet is create with the no-arg constructor the compareTo from the object is used (which implement Comparable)
4.2 - Maps
- Map = Container object athat stores a collection of key/value pairs.
- Fast retrieval, deletion, updating of the pair through the key.
- Map methods:
- Clear
- ContainsKey/Value - True if this map contains an entry for the specified key.
- EntrySet - Set consisting of all entries in the map.
- Get
- IsEmpty
- KeySet- Returns a set containing all keys.
- Put(all)
- Remove
- Size
- Values - Returns a collection consisting of the values in the map.
- Entry methods:
- Concrete Implementations
- HashMap (no duplicates)
- Efficient for locating/inserting/deleting entry.
- LinkedHashMap (no duplicates, ordering)
- Sorted in insertion order, or access order (least recent to most recent)
- TreeMap (no duplicates, sorted)
- Efficient for traversing the keys in a sorted order.
- SortedMap
- NavigableMap