1 / 19

Lecture 3 Set s

Lecture 3 Set s. Introduction to Collections A set ADT (abstract data type) : requirements, contract. Implementations of sets: using member arrays, linked lists. Sets in the Java class library. Example. Introduction to Collections .

affrica
Télécharger la présentation

Lecture 3 Set s

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3 Sets • Introduction to Collections • A set ADT (abstract data type): requirements, contract. • Implementations of sets: using member arrays, linked lists. • Sets in the Java class library. • Example

  2. Introduction to Collections • Simply an object that groups multiple elements into a single unit. • Collections are used to store, retrieve, manipulate, and communicate aggregate data. • Typically, they represent data items that form a natural group, e.g. • a poker hand (a collection of cards), • a mail folder (a collection of letters), • a telephone directory (a mapping of names to phone numbers).

  3. What Is a Collections Framework? • A unified architecture for representing and manipulating collections • Consists of • Interfaces • Implementations • Algorithms • polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.

  4. Benefits of the Java Collections Framework • Reduces programming effort • Increases program speed and quality • Allows interoperability among unrelated APIs • Reduces effort to learn and to use new APIs • Reduces effort to design new APIs • Fosters software reuse

  5. Interfaces • The core collection interfaces encapsulate different types of collections, • These interfaces allow collections to be manipulated independently of the details of their representation. • Core collection interfaces are the foundation of the Java Collections Framework.

  6. Observations • All the core collection interfaces are generic. • For example, this is the declaration of the Collection interface. public interface Collection<E>... • The <E> syntax tells you that the interface is generic. • When you declare a Collection instance you can and should specify the type of object contained in the collection. • Specifying the type allows the compiler to verify (at compile-time) that the type of object you put into the collection is correct, thus reducing errors at runtime

  7. Set • A collection that cannot contain duplicate elements. • This interface models the mathematical set abstraction • used to represent Sets, • the cards comprising a poker hand • the courses making up a student's schedule • the processes running on a machine

  8. 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.

  9. Set ADT: requirements • Requirements: • It must be possible to make a set empty. • It must be possible to test whether a set is empty. • It must be possible to obtain the cardinality of a set. • It must be possible to perform a membership test. • It must be possible to add or remove a member of a set. • It must be possible to test whether two sets are equal. • It must be possible to test whether one set subsumes another. • It must be possible to compute the union, intersection, or difference of two sets. • It must be possible to traverse a set (to go to every element).

  10. Set class Diagram We cannot complete this! Why?

  11. Set ADT: contract/specification(1) The Set interface // Each Set object is a set whose members are objects. //////////// Accessors //////////// publicboolean isEmpty (); // Return true if and only if this set is empty. publicint size (); // Return the cardinality of this set. publicboolean contains (Object obj); // Return true if and only if obj is a member of this set.

  12. Set ADT: contract (2) • Possible contract (continued): publicboolean equals (Set that); // Return true if and only if this set is equal to that. publicboolean containsAll (Set that); // Return true if and only if this set subsumes that.

  13. Set ADT: contract (3) • Possible contract (continued): //////////// Transformers //////////// publicvoid clear (); // Make this set empty. publicvoid add (Object obj); // Add obj as a member of this set. publicvoid remove (Object obj); // Remove obj from this set. publicvoid addAll (Set that); // Make this set the union of itself and that.

  14. Set ADT: contract (4) • Possible contract (continued): publicvoid removeAll (Set that); // Make this set the difference of itself and that. publicvoid 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.

  15. 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 or hash table (not considered in this module).

  16. 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.

  17. Example: (1) import java.util.TreeSet; class Colours { public static void main (String [] args) { TreeSet<String> rainbow=new TreeSet<String>(); TreeSet<String> primary=new TreeSet<String>(); TreeSet<String> flag=new TreeSet<String> (); // 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");

  18. Example: (2) primary.add("red"); primary.add(“yellow"); 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());

  19. Example: (3) System.out.println(); System.out.println("rainbow colours in “+ “the national flag"); rainbow.retainAll(flag); System.out.println(rainbow.toString()); System.out.println();   } } This prints out the set, how would you print out the individual colours? Go to the sun java site to find out

More Related