Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: LetterCount
|
Posted: Jan 13, 2003 6:35 PM
|
|
That is a good question. After years of programming, the choice seems "natural," but of course, it really isn't. There is a tutorial on collections at Sun's site: http://developer.java.sun.com/developer/onlineTraining/collections/, but it looks a bit heavy and theoretical (and pretty long). I don't know of a practical tutorial that outlines how to choose a collection type for particular tasks, but I think that would be a good article. Maybe someone else on the list has seen such an article on JavaWorld, or elsewhere.
Generally when you have a set of unique keys that each have an associated value (as in this case), you are looking at a Map. Lists (Vector is a List, by the way) are more common when you have a simple collection of values where there may be duplicates and order is maintained. A List often just functions as a more flexible array, since it grows and shrinks dynamically. Sets are used when you have a set of values, each of which is unique and usually the order is unimportant. There are also different implementations of these interfaces; choosing these is usually dependent on what you are doing with the data and whether performance or memory constraints are more important. For instance, you'd probably want to implement a queue with a LinkedList instead of an ArrayList, because adding and removing items from either end of a LinkedList is of equivalent (and fairly reasonable) speed, whereas adding to and removing from the beginning of an ArrayList is costly.
|
|