CSC153, Class 51: Dictionaries, Continued Overview: * Review * The Dictionary ADT: Central Methods * Dictionaries vs. Databases * Implementing Dictionaries with Lists * Better Strategies? Notes: * It's leftover day. * Extra credit for attending cool talk today at 4:15/4:30. * Some of you have asked for averages, minima, maxima, and all that crap for exam 3. I refuse to report any of those numbers. Since I have an absolute scale, you know what your number means. Why should you care what your colleagues got? * I expect to have prospective grades for you on Friday of 14th week. * Are there questions on exam 4? Pick four questions. If you do five, I'll pick four for you. I'll pick the four that are easiest for me to grade. Note that "This is garbage" is pretty easy to write. ---------------------------------------- What is a dictionary? It's like Scheme's map? * A set of "keyed elements" * You can use the keys to get values and change values. What operations should we provide? See Dictionary.java for more info What do you think about java.util.Dictionary and java.util.Map * Brian likes put, David doesn't + replaces, returns replaced item + David just doesn't like it. + Provides a nice compromise between set and replace. * "They're just lazy with remove" + Throws an exception only when key is null + Returns null if the key is not in the dictionary otherwise + Why? java.util.Dictionary vs. java.util.Map * Map has more annoying documentation + More methods * Map is a less clear term than Dictionary " Map maps this map" is confusing You would never write "Dictionary dictionaries this dictionary" * But Map is an interface and Dictionary is an abstract Interfaces vs. Abstract Classes * Both declare lots of methods. * Neither needs to define any methods. * Abstract classes can define some methods, interfaces cannot * Abstract classes can have fields, interfaces cannot * You can implement many interfaces, you can only subclass one abstract class. Suppose you want a real implementation of dictionaries. Easy implementation is with lists of key/value pairs. Some temptation to write public class AssociationList extends LinkedList implements Dictionary { } We can write public class Entry { Object key; Object value; public Entry(key, value) { this.key = key; this.value = value; } } public class AssociationList implements Dictionary { List contents; public AssociationList() { this.contents = new LinkedList(); } public void add(Object key, Object value) { this.contents.add(Entry(key, value)); } // add(Object key, Object value) public Object get(Object key) { Cursor current = this.contents.getCursor(); while (!current.atEnd()) { if this.key.equals(((Entry) current.getValue()).key) return ((Entry) current.getValue()).value; else current.advance(); } // while // Deal with end of list specially if this.key.equals(((Entry) current.getValue()).key) return ((Entry) current.getValue()).value; else throw new Exception("Not there!"); } // get(Object) } // class AssociationList