Sets
A Set is a collection of unique elements. Sets have no ordering guarantees, so you cannot obtain an item by its position. There are several implementations of the Set interface in the Java standard library. Probably the most used one is the HashSet class. It can add, remove and check if an item is contained in a constant time. The LinkedHashSet class has roughly the same performance, but internally it uses a LinkedList for storing items. This comes with the added memory overhead of an additional Node object for each item it contains, but has the benefit of predictable iteration order. When the HashSet class is iterated, it is in an unspecified order, in other words, not in the order the items were inserted. There is also the TreeSet ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access