Building the Right Environment to Support AI, Machine Learning and Deep Learning
|Bruce Eckel's Thinking in Java||Contents | Prev | Next|
To review the collections provided in the standard Java (1.0 and 1.1) library ( BitSet is not included here since it’s more of a special-purpose class):
- An array associates numerical indices to objects. It holds objects of a known type so you don’t have to cast the result when you’re looking up an object. It can be multidimensional, and it can hold primitives. However, its size cannot be changed once you create it.
- A Vector also associates numerical indices to objects – you can think of arrays and Vectors as random-access collections. The Vector automatically resizes itself as you add more elements. But a Vector can hold only Object handles, so it won’t hold primitives and you must always cast the result when you pull an Object handle out of a collection.
- A Hashtable is a type of Dictionary, which is a way to associate, not numbers, but objects with other objects. A Hashtable also supports random access to objects, in fact, its whole design is focused around rapid access.
- A Stack is a last-in, first-out (LIFO) queue.
If you’re familiar with data structures, you might wonder why there’s not a larger set of collections. From a functionality standpoint, do you really need a larger set of collections? With a Hashtable, you can put things in and find them quickly, and with an Enumeration, you can iterate through the sequence and perform an operation on every element in the sequence. That’s a powerful tool, and maybe it should be enough.
But a Hashtable has no concept of order. Vectors and arrays give you a linear order, but it’s expensive to insert an element into the middle of either one. In addition, queues, dequeues, priority queues, and trees are about ordering the elements, not just putting them in and later finding them or moving through them linearly. These data structures are also useful, and that’s why they were included in Standard C++. For this reason, you should consider the collections in the standard Java library only as a starting point, and, if you must use Java 1.0 or 1.1, use the JGL when your needs go beyond that.
If you can use Java 1.2 you should use only the new collections, which are likely to satisfy all your needs. Note that the bulk of this book was created using Java 1.1, so you’ll see that the collections used through the rest of the book are the ones that are available only in Java 1.1: Vector and Hashtable. This is a somewhat painful restriction at times, but it provides better backward compatibility with older Java code. If you’re writing new code in Java 1.2, the new collections will serve you much better.