Java Data StructuresLesson 65Free preview
Java Set
This lesson is part of Java · All levels
Summary
The set is a collection interface that only stores unique elements, without duplicates at all. The set has no index because the sequence is not guaranteed, so access is done via an iterator or for-each command. The uniqueness of an element is determined by the methods equals() and hashCode().
Key points
- Add() returns false when the same element is already in it
- No get(i); search using for-each or Iterator
- Key implementations: HashSet (fastest), TreeSet (following), LinkedHashSet (following)
- Comparison of elements uses equals() and hashCode(), not object references
- Some implementations allow exactly one element.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.