Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS2136: Paradigms of Computation Class 17: Java: Containers Enumerations & Iterators Copyright 2001, 2002, 2003, Michael J. Ciaraldi and David Finkel."— Presentation transcript:

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

2 2 But First… zRemember that objects are active. zGenerally… yYou don’t do things to them. yYou ask them to do things to themselves.

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

4 4 PolyOver2.java

5 5 Containers

6 6 zObjects which hold objects yWhat they hold are of class Object. yNot any specific subclass. zCategories yCollection xSet xList yMap

7 7 Containers zIn Java 1.3, Collections and Maps are Interfaces. yNeed to actually use concrete classes which implement these interfaces. yOr create your own. zContainers were completely revamped in Java 2 (as implemented in JDK 1.2). yOld ways, e.g. Vector, are de-emphasized.

8 8 Container Taxonomy (Eckel, pg. 502)

9 9 Collections vs. Maps zCollections hold individual elements. zMaps hold pairs. yKey and value.

10 10 Collections zHold individual elements zTypes (subinterfaces): yA Set cannot have duplicate elements yA List holds elements in a particular sequence. zPrints in square brackets, separated by commas.

11 11 Maps zA Map holds a group of pairs of objects. zEach pair is a key and a value. za.k.a. associative array. zActs like a simple database. zNo duplicate keys. zPrints in curly braces, with pairs shown as key=value.

12 12 Collections zMethods common to all: yboolean add(Object o) xReturns true if collection has changed. yboolean remove(Object o) xReturns true if collection has changed. yvoid clear()

13 13 List zHolds elements in a particular sequence. zAutomatically expands zYou should use one of the concrete classes which implement this interface: yArrayList yLinkedList

14 14 ArrayList zCreating: ynew ArrayList() ynew ArrayList(int initialCapacity) zMeasuring: yint size()

15 15 ArrayList zStoring: yboolean add(Object o) yboolean add(int index, Object element) yObject set(int index, Object element) zRetrieving: yObject get(int index) yObject remove(int index)

16 16 ArrayList zTesting: yboolean isEmpty() yboolean contains(Object elem) zFinding (failure = -1): yint indexOf(Object elem) yint lastIndexOf(Object elem)

17 17 Set zHolds elements in no particular sequence. zNo duplicates allowed. zAutomatically expands. zYou should use one of the concrete classes which implement this interface: yHashSet yTreeSet

18 18 Map zStores values and keys. zDuplicate keys not allowed. zCan be tricky to set up. zConcrete implementations: yAttributes yHashMap yHashtable

19 19 Map zStoring: yObject put(Object key, Object value) xNote: Both must be objects, not primitives. zRetrieving: y Object get(Object key) y Object remove(Object key)

20 20 Map zTesting: yboolean containsKey(Object key) yboolean containsValue(Object value)

21 21 Problems With Map zMatching keys zOverriding equals() and hashCode().

22 22 HashMap zImplements a hash table. zEfficient, constant-time access. zTwo functions must be properly implemented for the class used as the key: yboolean equals() yint hashCode()

23 23 Overriding equals() zThe default method compares object addresses: no good because you want to compare contents. zReturn true if contents are equal, by whatever criteria are important in your case.

24 24 Overriding hashCode() zThe hashCode() function must generate an int derived from the key. zThe default method uses object address: no good. zTwo objects with the same contents must return the same hash. Vice versa not required.

25 25 Enumerations & Iterators: A Brief Look

26 26 Enumerations & Iterators zObjects used to step through a container. yAvailable for some standard container classes. yWork properly even if the container changes. yOrder might or might not be significant. zJava has two variations: yEnumeration (old: since JDK 1.0) yIterator (new: since JDK 1.2)

27 27 Enumerations zTo get an Enumeration e for container v: yEnumeration e = v.elements(); yThis is initialized at the start of the list. zTo get the first and subsequent elements: ysomeObject = e.nextElement() zTo test if all have been accessed: ye.hasMoreElements()

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

29 29 Iterators zTo get an Iterator i for container v: yIterator i = v.iterator(); yThis is initialized at the start of the list. zTo get the first and subsequent elements: ysomeObject = i.next() zTo test if all have been accessed: yi.hasNext()

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

31 31 Next Time zExceptions zNumbers


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

Similar presentations


Ads by Google