next up previous
Next: More on Java- pass Up: Part 1: Crash Course Previous: Comments on java

Sets vs. Maps

For your work in this class Maps will generally be preferred over Sets. Maps are like sets, except that each key entry is associated with some value which can be anything. So, for instance, if you are using a class with a name like our "Dot" class, you can use the String name as a key and the whole Dot as the value. Maps are good because you need only know the key to get back the value object you want. (ie., read in a string name from the user, and use it to look up the whole corresponding dot). In a Set in java there is no easy way to pull out an object with a specific key other than to iterate over all the elements of the set. There is an add(Object) method which lets you add to the set, but there is no corresponding get(Object x) method. The assumption is that if you already have x, why do you need the set to return you another copy? They're the same thing! Sets in java instead have a contains(Object) method, which won't be of much use as a dictionary.
next up previous
Next: More on Java- pass Up: Part 1: Crash Course Previous: Comments on java
MM Hugue 2004-07-15