1 cs2136: paradigms of computation class 17: java: containers enumerations & iterators copyright...

31
1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

Upload: joan-reynolds

Post on 08-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

3 But Second… zA little more on polymorphism, overloading, and overriding. zRemember: yOverloading means same operation name. xSame or different classes. yOverriding means same operation signature. xDifferent classes. xPolymorphism only applies to overriding.

TRANSCRIPT

Page 1: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

1

CS2136:Paradigms of Computation

Class 17:Java:

ContainersEnumerations & Iterators

Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

Page 2: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

2

But First…Remember that objects are active.Generally…

You don’t do things to them. You ask them to do things to

themselves.

Page 3: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

3

But Second…A little more on polymorphism,

overloading, and overriding.Remember:

Overloading means same operation name.Same or different classes.

Overriding means same operation signature.Different classes.Polymorphism only applies to overriding.

Page 4: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

4

PolyOver2.javaArt

method1(aa : Art) : voidmethod1(dd : Drawing) : voidmethod2(aa : Art) : voidmethod2(dd : Drawing) : voidmethod3(aa : Art) : voidmethod4(dd : Drawing) : void

Drawing

method1(aa : Art) : voidmethod1(dd : Drawing) : voidmethod3(dd : Drawing) : voidmethod4(aa : Art) : void

Cartoon

Page 5: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

5

Containers

Page 6: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

6

ContainersObjects which hold objects

What they hold are of class Object. Not any specific subclass.

Categories Collection

SetList

Map

Page 7: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

7

ContainersIn Java 1.3, Collections and Maps are

Interfaces. Need to actually use concrete classes

which implement these interfaces. Or create your own.

Containers were completely revamped in Java 2 (as implemented in JDK 1.2). Old ways, e.g. Vector, are de-

emphasized.

Page 8: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

8

Container Taxonomy (Eckel, pg. 502)

Page 9: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

9

Collections vs. MapsCollections hold individual elements.Maps hold pairs.

Key and value.

Page 10: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

10

CollectionsHold individual elementsTypes (subinterfaces):

A Set cannot have duplicate elements A List holds elements in a particular

sequence.Prints in square brackets, separated

by commas.

Page 11: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

11

MapsA Map holds a group of pairs of

objects.Each pair is a key and a value.a.k.a. associative array.Acts like a simple database.No duplicate keys.Prints in curly braces, with pairs

shown as key=value.

Page 12: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

12

CollectionsMethods common to all:

boolean add(Object o)Returns true if collection has changed.

boolean remove(Object o)Returns true if collection has changed.

void clear()

Page 13: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

13

ListHolds elements in a particular

sequence.Automatically expandsYou should use one of the concrete

classes which implement this interface: ArrayList LinkedList

Page 14: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

14

ArrayListCreating:

new ArrayList() new ArrayList(int initialCapacity)

Measuring: int size()

Page 15: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

15

ArrayListStoring:

boolean add(Object o) boolean add(int index, Object element) Object set(int index, Object element)

Retrieving: Object get(int index) Object remove(int index)

Page 16: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

16

ArrayListTesting:

boolean isEmpty() boolean contains(Object elem)

Finding (failure = -1): int indexOf(Object elem) int lastIndexOf(Object elem)

Page 17: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

17

SetHolds elements in no particular

sequence.No duplicates allowed.Automatically expands.You should use one of the concrete

classes which implement this interface: HashSet TreeSet

Page 18: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

18

MapStores values and keys.Duplicate keys not allowed.Can be tricky to set up.Concrete implementations:

Attributes HashMap Hashtable

Page 19: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

19

MapStoring:

Object put(Object key, Object value)Note: Both must be objects, not primitives.

Retrieving: Object get(Object key) Object remove(Object key)

Page 20: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

20

MapTesting:

boolean containsKey(Object key) boolean containsValue(Object value)

Page 21: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

21

Problems With MapMatching keysOverriding equals() and hashCode().

Page 22: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

22

HashMapImplements a hash table.Efficient, constant-time access.Two functions must be properly

implemented for the class used as the key: boolean equals() int hashCode()

Page 23: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

23

Overriding equals()The default method compares object

addresses: no good because you want to compare contents.

Return true if contents are equal, by whatever criteria are important in your case.

Page 24: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

24

Overriding hashCode()The hashCode() function must

generate an int derived from the key.The default method uses object

address: no good.Two objects with the same contents

must return the same hash. Vice versa not required.

Page 25: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

25

Enumerations & Iterators:A Brief Look

Page 26: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

26

Enumerations & IteratorsObjects used to step through a container.

Available for some standard container classes.

Work properly even if the container changes. Order might or might not be significant.

Java has two variations: Enumeration (old: since JDK 1.0) Iterator (new: since JDK 1.2)

Page 27: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

27

EnumerationsTo get an Enumeration e for container v:

Enumeration e = v.elements(); This is initialized at the start of the list.

To get the first and subsequent elements: someObject = e.nextElement()

To test if all have been accessed: e.hasMoreElements()

Page 28: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

28

Enumeration Examplefor (Enumeration e = v.elements(); e.hasMoreElements(); ) { System.out.println( e.nextElement());}

Page 29: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

29

IteratorsTo get an Iterator i for container v:

Iterator i = v.iterator(); This is initialized at the start of the list.

To get the first and subsequent elements: someObject = i.next()

To test if all have been accessed: i.hasNext()

Page 30: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

30

Iterator Examplefor (Iterator i = v.iterator(); i.hasNext(); ) { System.out.println(i.next());}

Page 31: 1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel

31

Next TimeExceptionsNumbers