Unit08A - Sets Sets Maps The Set and Map Interfaces 8A A...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The Set and Map Interfaces 8A 2 Sets A set is a collection of objects that contains no duplicates. The index of each element in the set is not important. The presence of the element is important. Set operations: Union A B Intersection A B Difference A - B Subset A B 3 Sets A = { 1, 2, 4, 8, 16, 32, 64 } B = { 1, 4, 9, 16, 25, 36, 49, 64 } (note: elements in the set are not necessarily stored in sorted order) A B = { 1, 2, 4, 8, 9, 16, 25, 32, 36, 49, 64 } A B = { 1, 4, 16, 64 } A - B = { 2, 8, 32 } A B = false 4 Sets in the Java API Collection AbstractCollection Set AbstractSet SortedSet HashSet TreeSet extends implements A A A Interface Abstract Class Concrete Class 5 Set<E> Interface boolean add(E item) Adds the specified item to this set if it is not already present. Returns true if the set has been changed, false otherwise. boolean addAll(Collection<E> coll) Adds all of the elements in the specified collection to this set without adding duplicates. Returns true if the set has been changed, false otherwise. boolean contains(Object obj) Returns true if this set contains the specified object, false otherwise. boolean containsAll(Collection <E> coll) Returns true if this set contains all of the objects in the specified collection, false otherwise. 6 Set<E> Interface boolean remove(Object obj) Removes the elements equal to the specified object if it is present. Returns true if the set has changed, false otherwise. boolean removeAll(Collection<E> coll) Removes all of the elements from this set that match elements of the specified collection. Returns true if the set has changed, false otherwise. boolean retainAll(Collection<E> coll) Retains only those elements in this set that are contained in the specified collection. Returns true if the set has changed, false otherwise. boolean isEmpty()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 4

Unit08A - Sets Sets Maps The Set and Map Interfaces 8A A...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online