140 likes | 498 Views
Week 7 Lectures. Set Abstract Data Type. Aims. Describe the Set Abstract Data Type Illustrate different implementations of the Set ADT Describe the Java Iterator concept. Learning Outcomes. specify the Set ADT write a Java program using one of the java.util set classes use an iterator
E N D
CS2005 Week 7 Lectures Set Abstract Data Type
CS2005 Aims • Describe the Set Abstract Data Type • Illustrate different implementations of the Set ADT • Describe the Java Iterator concept
CS2005 Learning Outcomes • specify the Set ADT • write a Java program using one of the java.util set classes • use an iterator • recall the main points of Watt & Brown chapter 9.
CS2005 The Set ADT • A set is a collection of elements, without any imposed order
CS2005 Set Concepts • Null set (empty set) • Universal set • Disjoint sets • Subset • Equality
CS2005 Set ADT • Essential Operations • create: Set • add: Element Set Set • contains: Element Set Boolean • remove: Element Set Set • isEmpty: Set Boolean
CS2005 Set ADT (ii) • Other Operations • size: Set Integer • equals: Set Set Boolean • copy: Set Set • areDisjoint: Set Set Boolean • isSubset: Set Set Boolean • union: Set Set Set • intersection : Set Set Set • difference : Set Set Set
CS2005 Set Implementations • Bitmap • e.g. to represent the set { ‘C’, ‘X’ } • see java.util.BitSet class
CS2005 Set Implementations (ii) • Array • e.g. to represent the set { “baker”, “butcher”, “grocer” }
CS2005 Set Implementations (iii) • Linked List • Binary Search Tree • Hash Table
CS2005 Sets in Java Collection Classes • HashSet • TreeSet • addAll, retainAll, removeAll operations
CS2005 Iterators • need for following concept for sets 1. for every member, m, of the set s 1.1 process m • Iterator used to solve this problem public interface Iterator { public abstract boolean hasNext(); public abstract Object next(); public abstract void remove(); }
CS2005 Using an Iterator java.util.Iterator i; for (i = s.iterator(); i.hasNext(); ) { Object obj = i.next(); // the following code processes obj … }
CS2005 Sample code • ArraySet • TestArraySet • HTML file