|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java Collections Framework & Managing Data
A supporting structure for data manipulation
By: Rolf F. Kamp
Jul. 18, 2005 12:45 PM
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.
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:
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 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]; 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(); All keys existing in the Hashtable can be retrieved into an Enumeration by executing the keys() method:
members.put("charter", new Integer(90)); 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:
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 YOUR FEEDBACK
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||