Chapter 14
Introduction to Collections
So far you’ve been introduced to only one way of storing a collection of objects — with Java arrays, which are good for storage, but fall short when you need to dynamically add or remove data, or to sort or traverse your collection. There are a number of classes and interfaces in the package java.util that are quite handy when multiple instances of some objects have to be co-located in memory. This lesson will introduce you to several of them.
You can find more collections in the java.util.concurrent package, but those will be reviewed in Lesson 21 after you become familiar with the concept of multi-threading. Together, the collection classes and interfaces located in java.util and java.util.concurrent are often called the Java Collection Framework.
Arrays Revisited
Java collection classes enable the storing of handles of related data in one place. Here a handle is a reference to the location of an object in memory. You were introduced to arrays in Lesson 5: Arrays let you store and access a group of variables of the same type. Let’s go over the steps you follow to declare and populate an array.
First, declare a variable of the type that matches the types of the objects that will be stored in the array, and reserve enough memory to accommodate all objects. For example, to reserve memory enough for storing 10 instances of class Customer you can declare a variable cust, as follows:
Customer cust[] = new Customers[10];
At this point ...