Java Reference
In-Depth Information
The other classes in the Java collections framework are similar to the
Vector and Hashtable class in terms of construction and adding, deleting,
and accessing elements. If you are interested in the other list, set and map
classes, browse the Java documentation in the java.util package, which is
where the collections framework classes are found.
Classroom Q & A
Q: Why have all these different classes in the collections framework?
Why not just use a class like Vector all the time?
A: The Vector class is a list data structure, like an array or a linked list.
The Hashtable class is a map data structure and is entirely differ-
ent internally from a Vector, even though the two classes are simi-
lar to use. Your question really should be: Why not just use a list all
the time?
Q: Sounds good. Why not just use a list all the time?
A: Good question! Well, with a list, all the elements are accessed by
using an index, which means you can iterate (traverse) a list
quickly. However, insertions and deletions all take linear time,
which can really add up if you are performing lots of these types
of operations or working with a large list.
Q: Why not use a map all the time then?
A: A map is very efficient when adding and removing elements, but
iterating a map is time-consuming. So there is the trade-off
between lists and maps.
Q: So which is better to use?
A: You can answer that question only on a case-by-case basis because
it depends entirely on your situation. If you are looking for a good
data structure to use, my advice is to browse the java.util package
in the Java documentation and read up on some of the different
types of classes provided in the collections framework.
Search WWH ::




Custom Search