Welcome!

Java Authors: Maureen O'Gara, Liz McMillan, Walter H. Pinson, III, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

Java Collections Framework & Managing Data

A supporting structure for data manipulation

Every application has to consider how its data is stored, manipulated, and accessed. Fortunately for Java developers Sun provides commonly employed data structures as part of the Java platform in the Java Collections Framework (JCF). A framework is a set of well-defined interfaces that, if used properly, can benefit productivity tremendously, increase reusability, reduce software costs, and improve quality.

The JCF acts as a supporting structure for data manipulation. Well-thought-out choices in this arena make the difference between superior-performing software modules that are easy to build and maintain and those that have performance problems and are difficult to build and modify.

Interfaces and abstract classes are at the heart of the JCF. These describe the data structures and the operations supported by the data structure. A good understanding of the JCF can make for easy-to-maintain, easy-to-write modules based on a well-engineered framework.

In terms of data structures, a collection is a group of elements that can be treated as one unit. Consider why the following independent data elements can be stored in one entity called a collection:

  • Employees belonging to one department
  • Students in a grade
  • Hourly temperature readings over a 24-hour period
Grouping these elements into a collection permits the easy management of these otherwise independent data elements.

Decisions related to how data is accessed dictate what data structure will be used to house the data. Consider how you organize everyday items. Your address book is in alphabetical order because you look up people by name, while a bag of M&Ms stores the M&Ms in no order at all because they're arbitrarily accessed in no certain order (or are you one of those people that eats M&Ms by color?).

Java's Historical or Legacy Collections
The JCF was introduced as part of the JDK1.2. Before the JCF, Java supported three collections, each of which had its own syntax for accessing elements. An array is fixed in size, can store primitives or objects, and references elements with square brackets ([]). Arrays house homogeneous data elements. That is, all elements in an array must be the same type. Arrays contain a data element named length that represents the number of elements comprising the array.

A variable can be used to specify the size of an array, but once declared, an array can't change size. This can result in specifying too large an array or, at runtime one array can be copied into a larger array. The System class provides an arraycopy() method that copies elements from one array into another.

If we need to house more than 24 ints in our temperatureReadings int array, the arraycopy() method from the System class can be used as shown below:

int temperatureReadings[] = new int[24];
// iarray can only house ints or primitives compatible with int temperatureReadings [0] = 72;
temperatureReadings [0] = 98.6;
// will cause compile error-possible loss of precision
temperatureReadings [0] = new MyType();
// will cause compile error-incompatible types
int largerArray[] = new int[48];
System.arraycopy(temperatureReadings, 0, largerArray, temperatureReadings.length);

Hashtable and Vector collections are part of the java.util package. A Hashtable implements a hash table data structure (also known as an associative array) by storing objects accessible by an arbitrary key. Objects in a Hashtable are referenced with the get() and put() methods. The put() method maps a key to a value. If the key exists in the Hashtable, the previous value is returned and the new value is mapped to the key.

A Hashtable can be used to store the number of members in an organization. Say we need to keep track of how many "Charter Members," "Gold Members," and "Silver Members" there are. Membership type is the key and the number of members in the membership type is the value. Both arguments to the get() method are objects. If the value returned from get() isn't null, a value for the key already exists and is returned from get():

Hashtable members = new Hashtable();
Integer oldValue;
oldValue = (Integer)members.put("charter", new Integer(123));
if (oldValue == null) {
   System.out.println("key charter did not exist in Hashtable");
} else {
   System.out.println("key charter contained value " + oldValue);
}
oldValue = (Integer)members.put("gold", new Integer(456));
if (oldValue == null) {
   System.out.println("gold did not exist in Hashtable");
} else {
   System.out.println("key gold contained value " + oldValue);
}
   System.out.println(members.get("gold"));
// prints 456

All keys existing in the Hashtable can be retrieved into an Enumeration by executing the keys() method:

members.put("charter", new Integer(90));
members.put("gold", new Integer(87));
members.put("silver", new Integer(154));
Enumeration enumeration = members.keys();
while (enumeration.hasMoreElements()) {
   String key = (String)enumeration.nextElement();
   Integer value = (Integer)members.get(key);
   System.out.println(key + " " + value);
}

A Vector stores objects and can change in size as objects are added or removed. There are many ways to add and remove objects from a Vector including Methods add(), set(), setElementAt(), remove(), and removeAll(). Vectors grow as needed. They are often used as "adjustable-sized" arrays, when the number of elements to be stored is unknown:


Vector vector = new Vector();
vector.add(new Integer(90));
vector.add(new Integer(83));
vector.add(new Integer(193));
Enumeration enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
   Integer value = (Integer)enumeration.nextElement();
   System.out.println(value);
}

The ArrayList, discussed below, is like the Vector without the overhead of synchronization. Because the ArrayList isn't synchronized, it delivers four or more times the performance of a Vector. Of course using an array delivers the best performance.

The Java Collections Framework
Each historical collection had its unique syntax for managing elements. The JCF introduced the Collection interface - which the List and Set interfaces derive from - and the Map interface - which HashMap, Hashtable, and TreeMap derive from. In JDK 1.2, the Hashtable and Vector classes were updated to implement the Map and List interfaces, respectively.

More Stories By Rolf F. Kamp

Rolf is a Sun Certified Java Developer and has been worked with Java in varied environments. In addition to his development work he is an adjunct faculty member at Brookdale College, NJ.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
JDJ News Desk 07/18/05 12:24:10 PM EDT

Java Collections Framework & Managing Data
Every application has to consider how its data is stored, manipulated, and accessed. Fortunately for Java developers Sun provides commonly employed data structures as part of the Java platform in the Java Collections Framework (JCF). A framework is a set of well-defined interfaces that, if used properly, can benefit productivity tremendously, increase reusability, reduce software costs, and improve quality.