| By Rolf F. Kamp | Article Rating: |
|
| July 18, 2005 12:45 PM EDT | Reads: |
28,547 |
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
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.
Published July 18, 2005 Reads 28,547
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
JDJ News Desk 07/18/05 12:24:10 PM EDT | |||
Java Collections Framework & Managing Data |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?




































