Way2Java

Data Structures JDK 1.5 Features

Data Structures JDK 1.5 Features

Summary: This tutorial "Data Structures JDK 1.5 Features", narrates the new features added to DS with JDK 1.5

JDK 1.5 brought many changes in data structures. It not only added a new DS Queue but also added extra features to the existing collections framework.

Evolution of DS with JDK Versions
Data Structures JDK 1.5 Features

JDK 1.5 brought metamorphic changes in DS manipulation with which usage of DS became very easy compared to JDK 1.4 and earlier. The features are autoboxing, generics and enhanced for loop. Queue is added in JDK 1.5. Let us discuss one by one.

1. Autoboxing

Earlier to JDK 1.5, that is upto JDK1.4, the DS of Java do not accept data types. Following statements raise compilation error.

Vector vect = new Vector();
int x = 10;
vect.addElement(x);
double y = 10.5;
vect.addElement(y);

Then what the programmer followed to store x and y values? Convert them into object and then store. That is, upto JDK 1.4, the collections classes (all DS) stores only objects and returns objects. Following code works.

Vector vect = new Vector();
int x = 10;
Integer i1 = new Integer(x);
vect.addElement(i1);
double y = 10.5;
Double d1 = new Double(y);
vect.addElement(d1);

This is extra work for the programmer; converting data types into objects just for storage. While retrieving also, the same laborious work is to done. Observe.

Object obj = vect.elementAt(0);
Integer i2 = (Integer) obj;
int k = i2.intValue();

Similarly is with double also.

Object obj = vect.elementAt(1);
Double d2 = (Double) obj;
double m = d2.intValue();

Now you can use k and m in your code straightaway as int and double values. From JDK 1.5, the scenario completely changed. Following code works.

Vector vect = new Vector();
int x = 10;
vect.addElement(x);
double y = 10.5;
vect.addElement(y);

What is happening internally? Who has converted int x into Integer i1? Did the designers changed complete coding of their DS? No, the designers did not do much work over this. What you have done manually (converting data type into a wrapper object) earlier is done now by the designers internally. This feature is known as autoboxing. To put it more clearly, converting data type into object is known as autoboxing and converting object back into data type is known as unboxing. Autoboxing introduced with JDK 1.5 makes programmer job easy to use DS in Java.

2. Generics

A general data structure created gives a compilation warning with JDK 1.5

Vector vect = new Vector();

To the above vect object any Java data can be added as follows.

vect.add(10);
vect.add(10.5);

vect.add(true);
vect.add("helloo");

The vect object stores int, double, boolean and string values. This is fine, no problem if the programmer would like to add any data type or object. If the programmer would like to add only integers or strings, he must check explicitly with some validation code. To avoid this extra validation code, the JDK 1.5 introduced a concept called generics. In generics, the validation code is written by the data structure itself.

Vector vect1 = new Vector();

The above Vector object vect1 stores only integer values. If other added, it is compilation error.

vect1.add(10); // works fine
vect1.add(10.5); // compilation error
vect1.add("hello"”"); // compilation error

Let us see another data structure ArrayList that stores only strings.

ArrayList al = new ArrayList();

al.add(“hello”); // works fine
al.add(10); // compilation error
al.add(10.5); // compilation error

Creating a data structure to accept only one type data is known as generics. Generics give type-safe operation. If the programmer designs a non-generic data structure, the compiler displays a warning "unchecked and unsafe operation". Finally to say, generics allow storing only one type of data and this avoids validation code. Generics make implicitly checked and safe operation.

3. Enhanced for loop

Earlier before JDK 1.5, programmer write a for loop to print the elements of a data structure.

       for(int i = 0; i < vect.size(); i++)
       {
         System.out.println(vect.elementAt(i));
       } 

The above for loop known as basic for loop includes incrementing and condition checking. To avoid this, to print the values of a data structure or array, JDK 1.5 comes with a simple for loop known as enhanced for loop or foreach loop. This avoids incrementing and condition checking.

       for(int k : vect)
       {
          System.out.println(k);
       }

For every iteration, one element of vect is copied into k and k is printed.

To discuss very clearly about this new loop, two programs are given using with arrays and data structures.

Arrays Enhanced for loop (foreach)
Collections Enhanced for loop (foreach)

Pass your suggestions to imporve the quality of this tutorial "Data Structures JDK 1.5 Features".