YOUR FEEDBACK
udaykiran wrote: Really Excellent Information. But i have some doubts. initially i have some aver...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


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.

About 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.

YOUR FEEDBACK
Sean wrote: Adam: Very valid points. But I think I should clarify the point and intent of this blog post, because it's not entirely evident (even to me) after reading it again. First, however, I do have to disagree that "RIA" and "Web 2.0" are "tech terms." SQL is a tech term. HTTP is a tech term. AJAX is a tech term. RIA is clearly a marketing term (disputably) coined by some clever marketing folk at Macromedia. Web 2.0 was supposedly coined by O'Reilly. When I build a web app (I am a Rails and Flex developer), neither of these terms helps me clarify what the heck I am intending to build. Only drawings do that. Or mock-ups. Or focused discussions of particular features. Throwing in a term like RIA is just verbal hand-waving. I say all this, but these are unimportant arguments. RIA and Web 2.0 aren't going anywhere at this point, so it would be silly of me to call for their permanent banish...
Adam wrote: These ARE valid terms because definitions such as RIA or Web 2.0 help people who build these things to communicate what's required. These are not "public" words. They are tech terms that overarch a wide variety of solutions. Broad terms yes. Whether your grandparents understand what these phrases mean or not is a moot point. Do your grandparents know what a SQL database is? Should we rename it to "software" too? As far as marketing goes, if the term RIA or Web 2.0 is used when pitching to a client then it _is_ useful because of how the approaches that underpin what these phrases stand for differ from traditional web sites. So you want Web 2.0? If you mean more user interaction, comments, gradings etc... then beware that users can comment negatively as well as positively - what will that mean to your brand if this occurs? Plus it'll cost you more because of x, y, and z. Architecting an...
LATEST JAVA STORIES & POSTS
What could be a problem with logging in SOA in the presence of such wonderful tools like log4j, Java’s logging library and similar? Why might we need something special for SOA and why aren’t existing techniques enough? The answer is simple and complex simultaneously – in SO...
Aonix released PERC Ultra 5.1 cross development and target support on Sysgo's PikeOS 2.2 real-time operating system. PERC Ultra support of the PikeOS POSIX PSE52 profile provides a solution for the increasing need for portability across multiple operating systems as industries su...
What's the key to team and individual developer productivity in maintaining and extending a large application? Let’s start by making the following assertions: A developer's knowledge of an application code base is likely the single biggest factor of individual productivity. Cor...
An applet, a Java program that runs in a browser, often has to access the client resources. However, the security manager prevents an applet from accessing client resources. To access client resources, the applet has to have the proper permission. With this permission the applet ...
Three-letter acronyms (TLAs) are hardly new in Information Technology: EAI, ESB, SOA, BPM, BAM, ETL, MDM; the list goes on and on. This article is about yet another three-letter acronym, EDA, which stands for Event-Driven Architecture. EDA is not a brand new technology, but rathe...
Furthering its dedication to providing Java developers productivity with choice, Oracle announced the Oracle Enterprise Pack for Eclipse, a new component of Oracle Fusion Middleware. This release marks the first free Eclipse 3.4 environment to support Oracle WebLogic Server 10g R...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
In every field of design one of the first things students do is learn from the work of others. They ...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE
BREAKING JAVA NEWS
Ricoh Americas Corporation, a leading provider of digital office equipment, today announced the avai...