Download presentation
Presentation is loading. Please wait.
Published bySharon Harrison Modified over 9 years ago
1
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets: using member arrays, linked lists. Sets in the Java class library. Example © 2001, D.A. Watt and D.F. Brown
2
2-2 Set concepts (1) A set is a collection of distinct members (values or objects), whose order is insignificant. Notation for sets: {a, b, …, z}. The empty set is { }. Set notation is used here, but not supported by Java.
3
2-3 Set concepts (2) Examples of sets: evens= {0, 2, 4, 6, 8} punct= {‘.’, ‘!’, ‘?’, ‘:’, ‘;’, ‘,’} EU= {AT, BE, DE, DK, ES, FI, FR, GR, IE, IT, LU, NL, PT, SE, UK} NAFTA= {CA, MX, US} NATO= {BE, CA, CZ, DE, DK, ES, FR, GR, HU, IS, IT, LU, NL, NO, PL, PT, TR, UK, US} a set of integers a set of characters sets of countries
4
2-4 Set concepts (3) The cardinality of a set s is the number of members of s. This is written #s. E.g.: #EU = 15 #{red, white, red} = 2 Duplicate members are not counted. An empty set has cardinality zero. We can test whether x is a member of set s (i.e., s contains x). This is the membership test, written x s. E.g.: UK EU SE EU SE NATO SE is not a member of NATO.
5
2-5 Set concepts (4) Two sets are equal if they contain exactly the same members. E.g.: NAFTA = {US, CA, MX} NAFTA {CA, US} Order of members does not matter. These two sets are not equal. Set s 1 subsumes (is a superset of) set s 2 if every member of s 2 is also a member of s 1. This is written s 1 s 2. E.g.: NATO {CA, US} NATO EU NATO does not subsume EU.
6
2-6 Set concepts (5) The union of sets s 1 and s 2 is a set containing just those values that are members of s 1 or s 2 or both. This is written s 1 s 2. e.g.: {DK, NO, SE} {FI, IS} = {DK, FI, IS, NO, SE} {DK, NO, SE} {IS, NO} = {DK, IS, NO, SE}
7
2-7 Set concepts (6) The intersection of sets s 1 and s 2 is a set containing just those values that are members of both s 1 and s 2. This is written s 1 s 2. E.g.: NAFTA NATO= {CA, US} NAFTA EU= {} Two sets are disjoint if they have no common member, i.e., if their intersection is empty. E.g.: NAFTA and EU are disjoint NATO and EU are not disjoint.
8
2-8 Set concepts (7) The difference of sets s 1 and s 2 is a set containing just those values that are members of s 1 but not of s 2. This is written s 1 – s 2 (the symbol \ was used last year on SDM) E.g.: NATO – EU = {CA, CZ, HU, IS, NO, PL, TR, US} EU – NATO = {AT, FI, IE, SE}
9
2-9 Set applications Spelling checker: A spelling checker’s dictionary is a set of words. The spelling checker highlights any words in the document that are not in the dictionary. The spelling checker might allow the user to add words to the dictionary. Relational database system: A relation is essentially a set of tuples. Each tuple is distinct. The tuples are in no particular order.
10
2-10 Set ADT: requirements Requirements: 1)It must be possible to make a set empty. 2)It must be possible to test whether a set is empty. 3)It must be possible to obtain the cardinality of a set. 4)It must be possible to perform a membership test. 5)It must be possible to add or remove a member of a set. 6)It must be possible to test whether two sets are equal. 7)It must be possible to test whether one set subsumes another. 8)It must be possible to compute the union, intersection, or difference of two sets. 9)It must be possible to traverse a set.
11
2-11 Set class Diagram Set ? boolean isEmpty (); int size (); boolean contains (Object obj); Etc. We cannot complete this! Why?
12
2-12 Set ADT: contract/specification (1) public interface Set { // Each Set object is a set whose members are objects. //////////// Accessors //////////// public boolean isEmpty (); // Return true if and only if this set is empty. public int size (); // Return the cardinality of this set. public boolean contains (Object obj); // Return true if and only if obj is a member of this set.
13
2-13 Set ADT: contract (2) Possible contract (continued): public boolean equals (Set that); // Return true if and only if this set is equal to that. public boolean containsAll (Set that); // Return true if and only if this set subsumes that.
14
2-14 Set ADT: contract (3) Possible contract (continued): //////////// Transformers //////////// public void clear (); // Make this set empty. public void add (Object obj); // Add obj as a member of this set. public void remove (Object obj); // Remove obj from this set. public void addAll (Set that); // Make this set the union of itself and that.
15
2-15 Set ADT: contract (4) Possible contract (continued): public void removeAll (Set that); // Make this set the difference of itself and that. public void retainAll (Set that); // Make this set the intersection of itself and that. //////////// Iterator //////////// public Iterator iterator(); // Return an iterator that will visit all members of this set, in no // particular order. }
16
2-16 Summary of set implementations The array representation is suitable only for small or static sets. A static set is one in which members are never (or at least infrequently) added or removed. The SLL (singly link list, covered in a few weeks time) representation is suitable only for small sets. For general applications, we need a more efficient set representation: search tree (week 10) or hash table (not considered in this module).
17
2-17 Sets in the Java class library The java.util.Set interface is similar to the Set interface above. The java.util.TreeSet class implements the java.util.Set interface, representing each set by a search tree (covered later in the module). The java.util.HashSet class implements the java.util.Set interface, representing each set by a hash table (we will not consider this).
18
2-18 Example: (1) import java.util.TreeSet; class Colours { public static void main (String [] args) { TreeSet rainbow = new TreeSet(); TreeSet primary = new TreeSet(); TreeSet flag = new TreeSet(); // set up the colours of the rainbow rainbow.add("red"); rainbow.add("orange"); rainbow.add("yellow");rainbow.add("green"); rainbow.add("blue");rainbow.add("indigo"); rainbow.add("violet");
19
2-19 Example: (2) primary.add("red"); primary.add("green"); primary.add("blue"); flag.add("red"); flag.add("white"); flag.add("blue"); // printout the cardinality of the sets System.out.println( "the cardinality of set rainbow is “ +rainbow.size()); System.out.println( "the cardinality of set primary is “ +primary.size()); System.out.println( "the cardinality of set flag is “ +flag.size());
20
2-20 Example: (3) System.out.println(); System.out.println("rainbow colours in “ + “ the national flag"); rainbow.retainAll(flag); System.out.println(rainbow.toString()); } } This prints out the set, how would you print out the individual colours? Go to the sun java site to find out
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.