Title: Set Basics
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER > Section: Learning Units - Bb

Presentations and Videos

- Set Basics lecture video (Time: 36:50)
- Set Basics document

- The Set interface extends the Collection interface but doesn't add any new methods. However, a Set must guarantee uniquess of all of its elements based on the "equals" method. Sets are not ordered and they do allow one null entry.
- All classes which implement the Set interface should be fast at identifing whether an object is already present within the Set. The class HashSet implements the Set interface and uses a "hashing" technique for finding elements in the Set. This implies that each of the elements in the set has an "equals" method and overrides the method "hashCode" (defined in the Object class).
- When you iterate through a HashSet, the elements often come out in a somewhat random order. The LinkedHashSet provides an improvement to the HashSet by remembering the order that the elements were added. So iterating through a LinkedHashSet, you get the elements coming out in the order they were inserted.
- The SortedSet interface extends the Set interface. It adds the notion that the elements are maintained in a sorted order. So if you iterate through a SortedSet interface, you will get the elements listed in their natural (increasing) order.
- Additional methods of the SortedSet allow you to grab subsets of the SortedSet with the methods: first, headSet, last, subSet, tailSet.
- TreeSet is a class that implements SortedSet and it uses a binary tree to store the data.
- Because there needs to be a way to compare each element to other elements in the SortedSet, each element in the SortedSet must implement the Comparable interface.