Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.

Similar presentations


Presentation on theme: "Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections."— Presentation transcript:

1 Chapter 3 Collections

2 Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections API  Discuss the abstract design of collections  Define a set collection  Use a set collection to solve a problem  Examine an array implementation of a set

3 Introduction  A collection is an object that gathers and organizes other objects.  The collection defines the specific ways in which the elements of the collection can be accessed and managed.  Collections can be separated into two broad categories: linear and non- linear.

4

5 Order of the Collection  The organization of the elements in a collection, relative to each other, is usually determined by one of two things: 1.The order in which they were added 2.Some inherent relationship among the elements themselves

6 Abstract Data Types  An abstraction hides or ignores certain details at certain times.  The interface is an abstraction that allows us to control the object.  A collection is an abstraction.  The user interacts with the collection through the interface.  The details of how the collection is implemented is hidden from the user.

7 Example of an Interface

8 Collections Concerns  How does the collection operate, conceptually?  How do we formally define the interface to the collection?  What kinds of problems does the collection help us solve?  In which various ways might we implement the collection?  What are the benefits and costs of each implementation?

9 Related Terms  Data type is a group of values and operations defined on those values.  The primitive types in Java are great examples.  An abstract data type (ADT) is a data type whose values and operations are not inherently defined within a programming language.  A data structure is the collection of programming constructs used to implement a collection.

10 Java Collections API  Why should we learn to program collections, when Java already gives us a set of collections?  This set of collections is only a subset of the collections you may want to use.  The classes may not be implemented the way you desire.  The study of software development requires a deep understanding of the issues involved in the design of collections and the data structures used to implement them.

11 Set Collection  Set is a collection of element with no duplicates.  We can assume that there is no particular positional relationship among the element of the set.  A set is a non-linear collection.

12 Conceptual View of a Set

13 Common Operations  Every collection has operations that allow the user to add and remove  They usually will vary in the details.  Additional operations such as isEmpty and size are common.

14 Interfaces  An interface allows us to specify the method signatures.  The interface name can be used as the type of the object reference, which can be assigned any object of any class that implements the interface.  Interfaces can also be generic

15 SetADT<T> package jss2; import java.util.Iterator; public interface SetADT public interface SetADT { /** Adds one element to this set, ignoring duplicates. */ /** Adds one element to this set, ignoring duplicates. */ public void add (T element); public void add (T element); /** Removes and returns a random element from this set. */ /** Removes and returns a random element from this set. */ public T removeRandom (); public T removeRandom (); /** Removes and returns the specified element from this set. */ /** Removes and returns the specified element from this set. */ public T remove (T element); public T remove (T element); /** Returns the union of this set and the parameter */ /** Returns the union of this set and the parameter */ public SetADT union (SetADT set); public SetADT union (SetADT set); /** Returns true if this set contains the parameter */ /** Returns true if this set contains the parameter */ public boolean contains (T target); public boolean contains (T target);

16 SetADT<T> /** Returns true if this set and the parameter contain exactly /** Returns true if this set and the parameter contain exactly the same elements */ the same elements */ public boolean equals (SetADT set); public boolean equals (SetADT set); /** Returns true if this set contains no elements */ /** Returns true if this set contains no elements */ public boolean isEmpty(); public boolean isEmpty(); /** Returns the number of elements in this set */ /** Returns the number of elements in this set */ public int size(); public int size(); /** Returns an iterator for the elements in this set */ /** Returns an iterator for the elements in this set */ public Iterator iterator(); public Iterator iterator(); /** Returns a string representation of this set */ /** Returns a string representation of this set */ public String toString(); public String toString();}

17 UML for SetADT

18 Iterators  An iterator is an object that provides the means to iterate over a collection.  The iterator interface is defined in the Java standard class library with two primary abstract methods: –hasNext – returns true if the collection has more elements. –next – returns the next element in the iteration.

19 Iterator Issues  What happens if the collection is modified while the iterator is in use?  Most of the collections in Java are fail-fast.  Meaning they should throw an exception if the collection is modified while the iterator is in use.  However the documentation regarding this behavior explicitly states that this is not guarnteed.

20 Iterator Issues  How do you handle these modification issues?  Can make iterators that allow concurrent modificaion and reflect the changes in the collection.  Make iterators that iterate over a snapshot of the collection so modification makes no changes to the iterators.

21 Implementing a Set with Arrays  Design Questions: –How do you implement a non-linear collection with a linear data structure? –How do you manage the fact that the size of an array is fixed?

22 Managing Capacity  What do we do when the array is full? –We could throw an exception. –We could return a success indication from the add method that the use can check. –We could increase the capacity when it is full.

23 Array implementation of a set

24 ArraySet - Constructors

25 ArraySet - size and isEmpty

26 ArraySet - add

27 ArraySet - expandCapacity

28 ArraySet - addAll

29 ArraySet - removeRandom

30 ArraySet - remove

31 ArraySet - union

32 ArraySet - contains

33 ArraySet - equals

34 ArraySet - equals (continued)

35 ArraySet - iterator

36 Listing 3.4

37 Listing 3.4 (cont.)

38

39 Analysis of ArraySet  Adding an element to the set is O(n) because of the need to check to see if the element is already in the set  Expanding the capacity is also O(n)  Removing a particular element, because it must be found, is O(n)  Removing a random element is O(1)  Adding all elements of another set is O(n)  The union of two sets is O(n+m), where m is the size of the second set


Download ppt "Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections."

Similar presentations


Ads by Google