collectionhomepage.cs.uiowa.edu/~ghosh/collections.pdfthe set interface is in the java.utilpackage....

61

Upload: others

Post on 03-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and
Page 2: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Collection

ArrayList<T>

Collection

Collection

Adapted from material by Walter Savitch

Page 3: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 4: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

public void method(String arg1, ArrayList<?> arg2)

String

ArrayList<T>

Adapted from material by Walter Savitch

Page 5: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

<? extends String>

String

<? super String>

String

Adapted from material by Walter Savitch

Page 6: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Collection<T>

Collection<T>

Adapted from material by Walter Savitch

Page 7: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 1 of 10)

Adapted from material by Walter Savitch

Page 8: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 2 of 10)

Adapted from material by Walter Savitch

Page 9: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 3 of 10)

Adapted from material by Walter Savitch

Page 10: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 4 of 10)

Adapted from material by Walter Savitch

Page 11: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 5 of 10)

Adapted from material by Walter Savitch

Page 12: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 6 of 10)

Adapted from material by Walter Savitch

Page 13: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 7 of 10)

Adapted from material by Walter Savitch

Page 14: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 8 of 10)

Adapted from material by Walter Savitch

Page 15: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 9 of 10)

Adapted from material by Walter Savitch

Page 16: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Method Headings in the Collection<T> Interface (Part 10 of 10)

Adapted from material by Walter Savitch

Page 17: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Collection<T>

Collection<T>

Collection<T>

Set<T> List<T>

Adapted from material by Walter Savitch

Page 18: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Set<T>

Set<T>

Collection<T>

Collection<T>

Set<T>

Adapted from material by Walter Savitch

Page 19: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

List<T>

List<T>

List<T> Collection<T>

Collection<T>

List<T>

ArrayList<T> List<T>

Adapted from material by Walter Savitch

Page 20: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Set<T>

The Set<T> interface is in the java.util package.

The Set<T> interface extends the Collection<T> interface and has all the same method headings. However, the

semantics of the add methods vary as described below.

public boolean add(T element) (Optional)

If element is not already in the calling object, element is added to the calling object and true is returned. If element is in the calling object, the calling object is unchanged and false is returned.

public boolean addAll(Collection<? extends T> collectionToAdd) (Optional)

Ensures that the calling object contains all the elements in collectionToAdd. Returns true if the calling object changed as a result of the call; returns false otherwise. Thus, if collectionToAdd is a Set<T>, then the calling object is changed to the union of itself with collectionToAdd.

Adapted from material by Walter Savitch

Page 21: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

The List<T> interface has more method headings than the Collection<T> interface.

Adapted from material by Walter Savitch

Page 22: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 23: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 24: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 25: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 26: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 27: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 28: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 29: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 30: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 31: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 32: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 33: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 34: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 35: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 36: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 37: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

UnsupportedOperationException

Adapted from material by Walter Savitch

Page 38: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 39: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

HashSet<T>Set<T>

HashSet<T>Set<T>

HashSet<T>

ArrayList<T> Vector<T>List<T>

ArrayList<T> Vector<T>List<T>

List<T>

Adapted from material by Walter Savitch

Page 40: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

LinkedList<T>

AbstractSequentialList<T>

LinkedList<T>

SortedSet<T> TreeSet<T>

Set<T>

Adapted from material by Walter Savitch

Page 41: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 42: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 43: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 44: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 45: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 46: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Contents of set round:

grapes pie ball peas

Contents of set green:

grass garden hose grapes peas

ball in set round? true

ball in set green? false

ball and peas in same set? true

pie and grass in same set? false

Union of green and round:

garden hose grass peas ball pie grapes

Intersection of green and round:

peas grapes

Adapted from material by Walter Savitch

Page 47: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

HashSet<T>

Adapted from material by Walter Savitch

Page 48: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

<T>

<T> <ClassName>

Adapted from material by Walter Savitch

Page 49: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Map<T> AbstractMap<T>

AbstractMap<T>

Adapted from material by Walter Savitch

Page 50: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Map<K,V>

Interface

SortedMap<K,V> AbstractMap<K,V>

Implem

ents

TreeMap<K,V> HashMap<K,V>

Imp

lem

ents

Abstract Class

Concrete Class

A single line between two boxes means

the lower class or interface is derived

from (extends) the higher one.

K and V are type parametersfor the type of

the keys and elements stored in the map.

Page 51: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 52: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 53: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 54: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

HashMap<K,V>

TreeMap<K,V>LinkedHashMap<K,V>

Adapted from material by Walter Savitch

Page 55: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 56: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 57: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

All of the Map Interface methods are supported, such as getand put

Adapted from material by Walter Savitch

Page 58: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 59: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch

Page 60: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

SAMPLE OUTPUT

Added Joe, Andy, Greg, Kiki, and Antoinette to the map.

Enter a name to look up in the map. Press enter to quit.

Joe

Name found: Joe September 15, 1970

Enter a name to look up in the map. Press enter to quit.

Andy

Name found: Andy August 22, 1971

Enter a name to look up in the map. Press enter to quit.

Kiki

Name found: Kiki October 8, 1970

Enter a name to look up in the map. Press enter to quit.

Myla

Name not found.

Adapted from material by Walter Savitch

Page 61: Collectionhomepage.cs.uiowa.edu/~ghosh/Collections.pdfThe Set interface is in the java.utilpackage. The Set interface extends the Collectioninterface and

Adapted from material by Walter Savitch