lecture 8 - home | computer science and...
TRANSCRIPT
Page 1 of 36
Collections API
CSE 100, UCSD: LEC 8
Lecture 8
✔ Data structure and algorithm libraries
✔ A tour of the Java Collections Framework
✔ Iterators
✔ Iterators for lists and trees
Reading: Weiss Ch. 1, online Javadoc documentation for the
Page 2 of 36
Learning and using data structures
data structures are ng else you have to write
++, Java 2, very nice
: they are well-designed, ard environment for the
and and the Collections
CSE 100, UCSD: LEC 8
✔ Learning data structures has two parts:
✗ learning how to design and implement data structures
✗ learning how to use data structures
✔ In language environments like ANSI C, Pascal, etc., very fewavailable by default (maybe just arrays and records); everythiyourself
✔ However in other language environments such as Smalltalk, Cstandard data structure libraries are available
✔ For many applications, it makes sense to use these if possiblewell-written, well-tested, and are available wherever the standlanguage is available
✔ We will look at the Standard Template Library for C++ (later)framework for Java 5 (now)
Page 3 of 36
Generic data structures in Java version 1.2 (aka “Java 2”)
d enhancements over
ava Collections
, and in particular,
the next pages; for the
ctures Application
CSE 100, UCSD: LEC 8
✔ Java versions 1.2 and later provide a number of extensions anversion 1.1
✔ One of these is a high-level data structure library called the JFramework
✔ Then in version 1.5, generics were added to the Java languageincorporated into the Collections Framework
✔ The main aspects of the Collections Framework are shown onmost part these things live in the java.util package
✔ The Collections Framework provides a well-designed datastruProgramming Interface (API) that is worth studying!
Page 4 of 36
Toward Java generics
as just one that was
powerful and general
ect in it...e.g. Integers or
d to downcast in order to
f you do not correctly
or
CSE 100, UCSD: LEC 8
✔ In Java prior to 1.5 (a.k.a. Java 5), a “generic” data structure wdesigned to hold Objects
✔ Since in Java every object is-a Object by inheritance, this is aapproach
✔ For example, you can create a Vector, and put any kind of objStrings (though not primitive types);Vector v = new Vector();
v.add(new Integer(3));
v.add(new Integer(333));
v.add(new String("xyz"));
v.add(777); // compile time error
✔ Of course, when you take an object out of the Vector, you neeaccess the specific aspects of its true type:int i = ( (Integer) v.get(0) ) . intValue();
✔ The downcast can fail at runtime with a ClassCastException ikeep track of the type of object stored:String s = (String) v.get(1); // run time err
Page 5 of 36
Java and type checking
ent with its type are
ithout having to run and
may not catch all of them
errors to be detected at
o use an object in a way other
CSE 100, UCSD: LEC 8
✔ Java is typesafe: attempts to use an object in a way inconsistdetected as errors
✔ These errors can be detected at compile time, or at run time
✔ It is better to detect them at compile time!
✗ The compiler detects and localizes errors automatically, wtest your code
✗ Run-time errors require thorough testing to detect and you before your code ships
✔ The main advantage of Java generics is that they permit morecompile-time
✗ Though not all of them! Some can still occur at run time
✔ But in any case, Java with generics is still typsafe: attempts tinconsistent with its type are detected as errors, one way or an
Page 6 of 36
Using Java generics
eated specifying the type
use an object taken from or or warning
old only Integers:
or
nd unwrapping) of
CSE 100, UCSD: LEC 8
✔ In Java 1.5 (a.k.a. Java 5), a “generic” data structure can be crof object it is to hold
✔ Attempts to store another type of object in the structure, or to the structure as another type, will generate a compile-time err
✔ For example, you can create a Vector and specify that it can hVector<Integer> v = new Vector<Integer>();
v.add(new Integer(3));
v.add(new Integer(333));
v.add(new String("xyz")); // compile time err
✔ Now taking an object out of the Vector does not need a cast:Integer myInt = v.get(0);
✔ And in fact automatic boxing and unboxing (a.k.a. wrapping aprimitive types is now done:v.add(44);
int i = v.get(0) ;
Page 7 of 36
Defining generic classes
meters in the class header
generic class
lly 1 or 2 is enough
talized names, but they
ss definition, and can be
error
[100] // error
T // error
eate instances of it, ed for the formal type
,String,Object>();
CSE 100, UCSD: LEC 8
✔ To define a generic class or interface, specify formal type para
✔ For example, to begin a definition declaring MyClass to be a parameterized with 3 types:public class MyClass<T,E,F>
✔ There can be any number of type parameters; in practice, usua
✔ It is conventional for the type parameters to be one-letter capican be any Java identifiers
✔ The formal type parameters have scope over the rest of the claused there in most ways that type names can be used
✗ Some places a formal type parameter can not be used:
• In creating an object of that type: new T() //
• In creating an array with elements of that type: new T
• As an argument to instanceof: someref instanceof
✔ Then when the generic class MyClass is compiled, you can crsupplying actual type parameters which are in effect substitutparameters throughout the class definition:
MyClass<Byte,String,Object> x= new MyClass<Byte
Page 8 of 36
The Java Collections Framework includes...
ollection interfaces nd maps), plus sorted
s of the core collection
e collection interfaces, to
tion Hierarchy, notably ring.
ions polymorphically on e.g., sorting a list).
ns API, but this s on the same
CSE 100, UCSD: LEC 8
✔ Basic hierarchy of generic collections interfaces - Four core crepresenting different types of collections (such as sets, lists, aversions of these
✔ Concrete implementations - General-purpose implementationinterfaces.
✔ Abstract implementations - Partial implementations of the corfacilitate custom implementations.
✔ Infrastructure - Several interfaces used in defining the Collectwo new iterators, and two interfaces that define element orde
✔ Algorithms - Several static methods that perform useful functan object that implements an appropriate collection interface (
✔ Array sorting and searching - Not really a part of the Collectiofunctionality was added to the JDK at the same time, and relieinfrastructure.
Page 9 of 36
Collections framework interface and class hierarchy
Map
AbstractMap
HashMapHashtable
p
TreeMap
class:
abstract class:
interface:
NavigableMapt
CSE 100, UCSD: LEC 8
Collection
SortedSet
List SetAbstractCollection
AbstractList AbstractSet
HashSet TreeSet
ArrayListVector
AbstractSequentialList
SortedMa
LinkedList
NavigableSe
Page 10 of 36
Collection interfaces
ons are made about the licate elements.
permitted. May or may
E; i.e. a sequence. using an index. Extends
V; i.e. a table or es not extend Collection.)
matically sorted, either in a Comparator object Set interface.
type K to values of type V ordering or by a ed. Extends the Map
CSE 100, UCSD: LEC 8
✔ Collection<E> - A group of objects of type E. No assumptiorder of the collection (if any), or whether it may contain dup
✔ Set<E> - The familiar set abstraction. No duplicate elements not be ordered. Extends the Collection interface.
✔ List<E> - A structurally ordered collection of objects of typeDuplicates are generally permitted. Allows positional access, the Collection interface.
✔ Map<K,V> - A mapping from keys of type K to values of typedictionary. Each key can map to at most one value. (Note: do
✔ In addition to these, there are sorted versions of Set and Map:
✗ SortedSet<E> - A set whose elements, of type E, are autotheir natural ordering (see the Comparable interface), or byprovided when a SortedSet instance is created. Extends the
✗ SortedMap<K,V> - A map whose mappings from keys of are automatically sorted by key, either in the keys’ naturalComparator provided when a SortedMap instance is creatinterface.
Page 11 of 36
Collection operations
E> {
c);
ny class that implements
oc comments attached to
CSE 100, UCSD: LEC 8
✔ The generic Collection interface specifies these methods:
public interface Collection<E> extends Iterable<
public boolean add(E o);
public boolean addAll(Collection<? extends E>
public void clear();
public boolean contains(Object o);
public boolean containsAll(Collection<?> c);
public boolean isEmpty();
public Iterator<E> iterator();
public boolean remove(Object o);
public boolean removeAll(Collection<?> c);
public boolean retainAll(Collection<?> c);
public int size();
public Object[] toArray();
}
✔ These are the signatures of methods that must be defined by athis interface....
✔ The required semantics of these methods is specified in Javadthis interface definition.
Page 12 of 36
Map operations
ny class that implements
oc comments attached to
CSE 100, UCSD: LEC 8
The Map interface specifies these methods:
public interface Map<K,V> {
void clear();
boolean containsKey(Object key);
boolean containsValue(Object value);
Set<Map.Entry<K,V>> entrySet();
V get(Object key);
boolean isEmpty();
Set<K> keySet();
V put(K key, V value);
void putAll((Map<? extends K,? extends V> m);
boolean remove(Object key);
int size();
Collection<V> values();
}
✔ These are the signatures of methods that must be defined by athis interface....
✔ The required semantics of these methods is specified in Javadthis interface definition.
Page 13 of 36
List operations
ny class that implements
oc comments attached to
CSE 100, UCSD: LEC 8
✔ The List interface adds these methods to Collection:
public interface List<E> extends Collection<E> {
void add(int index, E o);
E get(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator<E> listIterator();
E set(int index, E element);
List<E> subList(int fromIndex, int toIndex);
}
✔ These are the signatures of methods that must be defined by athis interface....
✔ The required semantics of these methods is specified in Javadthis interface definition.
Page 14 of 36
Set operations
not add any methods not
}
mantics of the Collection
er contain duplicate
CSE 100, UCSD: LEC 8
✔ The Set interface extends the Collection interface, but it does required by Collection:
public interface Set<E> extends Collection<E> {
✔ However, it does place slightly different stipulations on the semethods...
✔ ...these differences are all related to the fact that a Set can nevelements, whereas a Collection in general can
Page 15 of 36
SortedSet operations
Element);
forced just by specifying
CSE 100, UCSD: LEC 8
✔ The SortedSet interface adds these methods to Set:
public interface SortedSet<E> extends Set<E> {
public Comparator<? super E> comparator();
public E first();
public SortedSet<E> headSet(E toElement);
public E last();
public SortedSet<E> subSet(E fromElement, E to
public SortedSet<E> tailSet(E fromElement);
}
✔ ... and it adds some other stipulations as well, that cannot be enmethod signatures in the interface definition...
Page 16 of 36
SortedSet requirements
a constructor that takes ould have two more: one that takes a Collection or SortedSet.)
tructor, all contained face, and all comparisons his is called the "natural
es a Comparator ompare() method
ust iterate through the natural ordering" or the
CSE 100, UCSD: LEC 8
✔ A SortedSet<E> class must have a default constructor, andone argument of type Comparator<? super E>. (It also shthat takes a Collection<? extends E> argument, and oneSortedSet<E> argument, for making a “copy” of an existing
✔ If the SortedSet<E> object is created using the default consobjects must implement the Comparable<? super E> interare done using the contained objects’ compareTo() methods (tordering" on the contained objects)
✔ If the SortedSet object is created using the constructor that takargument, all comparisons are done using that Comparator’s c
✔ The Iterator returned by the iterator() method of a SortedSet mSortedSet in order from smallest to largest, according to the "Comparator’s ordering, as appropriate
Page 17 of 36
Collection implementations
e instantiated directly
interfaces:
ted using a hash table
ed using a red-black tree
)
implement Map, and
es if you want
CSE 100, UCSD: LEC 8
✔ The Collection and Map interfaces, being interfaces, cannot b
✔ But Java 2 also provides generic classes that implement these
✗ HashSet<E> implements Set<E>
✗ TreeSet<E> implements SortedSet<E>
✗ HashMap<K,V> implements Map<K,V>
✗ TreeMap<K,V> implements SortedMap<K,V>
✗ ArrayList<E>, LinkedList<E> implement List<E>
✔ HashSet is implemented using a HashMap which is implemen
✔ TreeSet is implemented using a TreeMap which is implement
✔ ArrayList uses an array; LinkedList uses a linked list (surprise
✔ Also, in Java 2, the old java.util.Hashtable is now declared to java.util.Vector is declared to implement List
✔ You can also define your own implementations of the interfac
Page 18 of 36
Comparable and Comparator
ackage):
ct for order.
a class that
ive integer as
er than the
:
ive integer as
greater than the
CSE 100, UCSD: LEC 8
✔ Java 2 introduced the Comparable interface (in the java.lang ppublic interface Comparable<T> {
/** Compare this object with the specified obje
* This is the "natural ordering" on objects of
* implement this interface.
* @return a negative integer, zero, or a posit
* this object is less than, equal to, or great
* specified object.
*/
int compareTo(T o);
}
✔ ... and also the Comparator interface (in the java.util package)public interface Comparator<T> {
/** Compare the two arguments for order.
* @return a negative integer, zero, or a posit
* the first object is less than, equal to, or
* second object.
*/
int compare(T o1, T o2);
}
Page 19 of 36
Iterator and ListIterator
d Enumeration interface d names):
er objects that implement
r<E> {
CSE 100, UCSD: LEC 8
✔ Java 2 introduces the Iterator interface, in preference to the ol(Iterator permits removing elements, and it has shorter metho
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
✔ ... and ListIterator, useful for iterating forward or backward ovList; also permits inserting and setting values of elements:
public interface ListIterator<E> extends Iterato
void add(E o);
boolean hasPrevious();
int nextIndex();
E previous();
int previousIndex();
void set(E o);
}
Page 20 of 36
Collections algorithms
can be used to perform ions for some of them:
Comparator c);
);
);
CSE 100, UCSD: LEC 8
✔ Static methods of the Collections class (note: not Collection)useful operations on Collection objects. Here are the declarat
public class Collections {
static int binarySearch(List list, Object key);
static int binarySearch(List list, Object key,
static void copy(List dest, List src);
static Object max(Collection coll);
static Object max(Collection coll, Comparator c
static Object min(Collection coll);
static Object min(Collection coll, Comparator c
static List reverse(List list);
static void shuffle(List list);
static void sort(List list);
static void sort(List list, Comparator c);
}
Page 21 of 36
Iterators
erface:
s) must define an instance that instance
g idea, implemented in
o access and traverse all epresentation of that
e, one element at a time
CSE 100, UCSD: LEC 8
✔ The Collection<E> interface extends the Iterable<E> int
public interface Iterable<E> {
public Iterator<E> iterator();
}
✔ So class that implements Collection<E> (or its subinterfacemethod iterator() that returns an Iterator<E> object for
✔ Iterators are a basic and powerful object-oriented programmindifferent ways in different OO systems
✔ In general: an iterator is an object that provides an interface tthe data in a data structure, without exposing the underlying rstructure
✔ Think of an iterator as a convenient way to traverse a structur
Page 22 of 36
Using iterators
ould be in a context like:Next(); ) {
item in c
something with it
re conveniently write an on) that implements the
CSE 100, UCSD: LEC 8
✔ A typical use of an Iterator for a Collection<MyObjs> c wfor ( Iterator<MyObjs> i = c.iterator(); i.has
MyObjs x = i.next(); // get the next data
if ( passesTest(x) ) process(x); // and do
}
✔ (Here, c is called the “backing Collection” of the Iterator i)
✔ In Java 5, a new ‘foreach’ style syntax is available to even moiteration over the elements of any object (such as a CollectiIterable interface.
Again, suppose that c is a Collection<MyObjs>:
for( MyObjs x: c ) {
if ( passesTest(x) ) process(x);
}
Page 23 of 36
Implementing iterator() and an Iterator class as a seperate class
not inner) class
llection interface
r Foo class might be:il.Iterator<T> {
init’lzation */ }
ts remain */}
ow if none*/}
lt returned */}
per methods...
only to the public (and ds of the backing
ds to do, it’s a problem!
can now be quite simple:
CSE 100, UCSD: LEC 8
✔ It may be possible to define an Iterator class as a standalone (
✔ Suppose you define a generic class Foo that implements a Co
✔ In outline, the class definition for an Iterator to work with youpublic class FooIterator<T> implements java.ut
private Foo<T> c; // the backing Collection
FooIterator(Foo<T> x) { c = x; /* and other
public boolean hasNext() { /* see if any el
public T next() {/* return next elt, or thr
public void remove() { /* remove the last e
// any other needed instance variables, hel
}
✔ The instance methods of a separate class like this have accesspackage, if in the same package) instance variables and methoCollection object; if this isn’t enough access to do what it nee
✔ But with access, the iterator() method of the backing Foo classpublic java.util.Iterator<E> iterator()
{ return new FooIterator<E>(this); }
Page 24 of 36
Implementing iterator() and an Iterator class as an inner class
class for a class that tatic) private inner class, Collection<E>
erator<E> {
}
ts remain */}
ow if none*/}
lt returned */}
or the Iterator
access to all the instance n easily do everything it
ery simple: new Iterator(); }
CSE 100, UCSD: LEC 8
✔ In Java, an easier and more flexible way to define an Iterator implements Collection<E> is to define an instance (i.e., not swithin the body of the definition of the class that implements
✔ In outline, the inner class definition would look like:private class Iterator implements java.util.It
Iterator() { /* initialize the Iterator */
public boolean hasNext() { /* see if any el
public E next() {/* return next elt, or thr
public void remove() { /* remove the last e
// any needed separate instance variables f
}
✔ The instance methods of a (nonstatic) inner class like this havevariables and methods of its enclosing class, so the Iterator caneeds to do
✔ Then the iterator() method of the backing Collection can be vpublic java.util.Iterator<E> iterator() { return
Page 25 of 36
Implementing an Iterator class
e the Iterator
backing structure, and
en visited
structure
t backing structure
CSE 100, UCSD: LEC 8
✔ The Iterator needs to do the following things efficiently:
✗ find the “first” element in the backing structure, to initializ
✗ find the “next” element that has not yet been visited in thevisit it (i.e., return a pointer to the data stored there)
✗ tell whether or not there is a “next” element that has not be
✗ remove the most-recently visited element from the backing
✔ Let’s look at how these can be realized for a simple linked-lis
Page 26 of 36
A linked list class
ic inner class
l header node
the sentinel node
CSE 100, UCSD: LEC 8
✔ Suppose a linked list class is defined (in part) like this:
public class LinkedList<E> {
private static class Node<T> { // private stat
Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}
T data;
Node<T> next;
}
private Node<E> header; // pointer to sentine
public LinkedList() {
header = new Node<E>(null,null); // create
}
// etc...
Page 27 of 36
A linked list and its iterator
, it might look like this:
es:
500
CSE 100, UCSD: LEC 8
✔ After some operations on a LinkedList<Integer> object x
✔ Now what should happen with these operations:
Iterator<Integer> i = x.iterator();
System.out.print(i.next());
System.out.print(i.next());
i.remove();
System.out.print(i.next());
System.out.print(i.hasNext());
✔ The backing collection is changed by the remove()! It becom
12 25 122x: header: null
12 122 500x: header: null
Page 28 of 36
An iterator for a singly-linked list: ideas
at header.next is
pointer from the last
eed to keep a pointer to
nner class, so its instance t instance
the Iterator, and defining
instance of this inner
CSE 100, UCSD: LEC 8
✔ Finding the first element in a singly-linked list is easy: it’s whpointing to
✔ Finding the next element to visit is easy: just follow the next element visited (if it’s null, there is no next element)
✔ Deleting the last element visited requires more thought: you nthe predecessor of the last element visited
✔ We will implement the Iterator as an instance (i.e. nonstatic) imethods can access everything about the enclosing LinkedLis
✔ Then all this can be accomplished with 3 instance variables inthe Iterator’s instance methods accordingly... example next
✔ LinkedList’s iterator() instance method just needs to return anIterator class
Page 29 of 36
An inner Iterator class for a singly-linked list
ator<E> {
visited
visited
astReturned
; }
ception();
der : prev.next;
tateException();
round lastReturned
CSE 100, UCSD: LEC 8
private class Iterator implements java.util.Iter
private Node<E> next; // the Node next to be
private Node<E> lastReturned; // the Node last
private Node<E> prev; // the predecessor of l
Iterator() { next = header.next; }
public boolean hasNext() { return next != null
public E next() {
if (next == null) throw new NoSuchElementEx
lastReturned = next;
next = next.next; prev = prev == null ? hea
return lastReturned.data;
}
public void remove() {
if(lastReturned == null) throw new IllegalS
prev.next = lastReturned.next; // splice a
lastReturned = null;
}
Page 30 of 36
A general form for an Iterator
tor, along these lines:ator<E> {
e visited
st visited
terator
rstElt(); }
; }
eption();
ssor(state);
tateException();
CSE 100, UCSD: LEC 8
✔ Using these ideas, you can sketch the general form of an Iteraprivate class Iterator implements java.util.Iter
private Elt<E> next; // the Element next to b
private Elt<E> lastReturned; // the Element la
private State<E> state; // the state of the I
Iterator() { state = initialState(); next = fi
public boolean hasNext() { return next != null
public E next() {
if(next == null) throw new NoSuchElementExc
lastReturned = next;
next = successor(next,state); state = succe
return lastReturned.data();
}
public void remove() {
if(lastReturned == null) throw new IllegalS
delete(lastReturned,state);
lastReturned = null;
}
Page 31 of 36
An Iterator for a skip list backed Collection
ngly-linked list
just a matter of following
e a little more
sed in the skip list delete
CSE 100, UCSD: LEC 8
✔ An Iterator for a skip list would be very similar to one for a si
✔ Finding the first element and finding the successor element is “level-1” pointers
✔ However, the state required to implement remove() needs to bcomplicated...
✔ ... basically, it needs to be equivalent to the “update” vector ualgorithm
Page 32 of 36
An Iterator for a binary search tree backed Collection
issues:
than the key in X, but its
er than the key in X
CSE 100, UCSD: LEC 8
✔ An Iterator for a binary search tree needs to address the same
✗ how to find the “first” element of a BST
✗ how to find the “next” element to visit
✗ how to remove the last-visited element
✔ In a BST, these are somewhat different than in a linked list
✔ Pay attention to the BST ordering property! For any node X,
✗ any keys in X’s left subtree are smaller than the key in X
✗ any keys in X’s right subtree are larger than the key in X
✗ If X is the left child of its parent, its parent’s key is larger parent’s key is also larger than any key in X’s right subtree
✗ If X is the right child of its parent, its parent’s key is small
✔ Everything follows from these facts...
Page 33 of 36
Finding the “first” element in a BST
st key
end
CSE 100, UCSD: LEC 8
✔ The “first” element in a BST is the node containing the smalle
✔ Q: How to find this?
✔ A: start at the root, and follow the left spine of the tree to the
6
3
0
8
95 7
Page 34 of 36
Finding the “next” element in a BST
ng the smallest key in the
ining the smallest key in the left spine downward
essor. Go to the parent
ine upward to the left ’s parent is the successor
CSE 100, UCSD: LEC 8
✔ For a node X in a BST, the successor of X is the node containiBST that is larger than the key in X
✔ Q: How to find this?
✔ A: There are cases to consider:
✗ If X has a right child, then X’s successor is the node contathe right subtree of X. Go to the right child, and then followto the end
✗ Else, if X is the left child of its parent, its parent is its succ
✗ Else, if X is the right child of its parent, follow the right spuntil reaching a node Y that is the left child of its parent; Yof X. If there is no such node Y, X has no successor
✗ Else, X has no successor
6
3
0
8
95 7
Page 35 of 36
Iterator-removing an element from a BST
lla BST, a treap, an AVL arent of X
a way to access quickly
red for finding the
ight child pointers. The te these pointers correctly
ee. When descending to a parent, pop the stack to
ntains the tree and the
CSE 100, UCSD: LEC 8
✔ In general, deleting a node X from a BST (whether it is a vanitree, a red-black tree, etc.) requires being able to modify the p
✔ So, an Iterator implementing remove() efficiently should havethe parent of the last node visited
✔ (Note that quickly accessing the parent of a node is also requisuccessor of a node!)
✔ Two ways to go about doing this:
✗ Have each tree node store a parent pointer, as well as left/rtree’s insert and delete methods need to be written to upda
✗ Use a stack. Simulate recursive in-order traversal of the trchild, push parent node onto the stack; when returning to afind the parent. (Be careful that a remove() operation maistack in states consistent with each other!)