HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"J2SE 1.5"
Vol. 9, Issue 3, p. 46


Listing 1: Generics usage in parameterized types, classes, interfaces, and methods

Parameterized Type Example

List<Integer> integerList = new LinkedList<Integer>();
Map<String, Integer> integerMap = new HashMap<String,Integer>();
Stack<Integer> integerStack = new Stack<Integer>();

Generic Class Example

public class C<T1, T2> {
private T1 type1;
private T2 type2;
public C(T1 type1, T2 type2) {
this.type1 = type1;
this.type2 = type2;
public T1 getType1() {
return this.type1;
public T2 getType2() {
return this.type2;
public static void main(String args[]) {
C<String, Integer> cSI = new C<String, Integer>("one", 1);
String type1SI = cSI.getType1();
int type2SI = cSI.getType2().intValue();

C<Integer, Boolean> cIB = new C<Integer, Boolean>(1, true);
int type1IS = cIB.getType1().intValue();
boolean type2IS = cIB.getType2().booleanValue();

Generic Interface Example

public interface I<T> {
public T getConnectionPool();
public void releaseConnectionPool(T connectionPool);

Generic Method Example

public class M {
public static <T extends Comparable> T minimum(T a, T b) {
if(a.compareTo(b) <= 0) return a;
else return b;
public static void main(String[] args) {
Integer b1 = new Integer(2);
Integer b2 = new Integer(5);
Integer min = minimum(b1, b2);
System.out.println("Minimum of (2,5) : " + min.intValue());

Listing 2: Collection filtering with generics

import java.util.*;
public class CollectionFiltering {

* Traditionally, collections are allowed to hold heterogeneous 
* elements. This causes run-time error due to poor type 
* checking. These errors are uncovered only during testing or 
* deployment time, which are very crucial to the success of the 
* product.
static void purgeFromCollection(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); ) {
String s = (String) i.next();
if(s.length() == 4)
* Generics restricts the collection to hold homogeneous 
* elements. When an element other than String is added to this 
* collection, this causes compile-time error. Generics helps us 
* in preventing the run-time exceptions.
static void purgeFromGenerics(Collection<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); ) {
if(i.next().length() == 4)
public static void main(String args[]) {
//switch between approaches for behaviour.
//List<String> arrayList = new 
ArrayList<String>();//generics approach
List arrayList = new ArrayList();//traditional approach
arrayList.add(new Integer(0));
arrayList.add(new String("1"));
arrayList.add(new String("2"));

Listing 3: The typesafe enum pattern

public class Continent {
private final String name;

private Continent(String name) { this.name = name; }

public String toString() { return name; }

public static final Continent AFRICA =
new Continent ("africa ");
public static final Continent ASIA =
new Continent ("asia");
public static final Continent EUROPE =
new Continent ("europe");
public static final Continent NORTH_AMERICA =
new Continent ("north america");
public static final Continent OCEANIA =
new Continent ("oceania");
public static final Continent SOUTH_AMERICA =
new Continent ("south america");

Listing 4: Proposed typesafe enum facility

public class EnumFacility {
enum Continent { africa , asia, europe, north_america, oceania, south_america };
public static void main(String args[]) {
System.out.println("Continents are : " + Continent.VALUES);
for ( Continent c : Continent.VALUES ) {
// switch on enum
switch(c) {
case Continent.africa:
System.out.println("in africa.");
case Continent.asia:
System.out.println("in asia.");
case Continent.europe:
System.out.println("in europe.");
case Continent.north_america:
System.out.println("in north_america.");
case Continent.oceania:
System.out.println("in oceania.");
case Continent.south_america:
System.out.println("in south_america.");

Listing 5: Autoboxing in a Collection

import java.util.*;

public class AutoBoxing {
static List<Integer> testAutoBoxing(int ii) {
int i = 0;
List<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(new Integer(11));
return arrayList;
public static void main(String args[]) {
List<Integer> arrayList = testAutoBoxing(110);
for (Integer i : arrayList)

Listing 6: "enhanced for" statements usage in a collection

import java.util.*;

public class TestForStatements {

static StringBuffer buffer = null;
public static void testCurrentForStatements(Collection c) {
buffer = new StringBuffer();
for (Iterator i = c.iterator(); i.hasNext(); ) {
String s = (String) i.next();
System.out.println("Collection elements from traditional for statement :
\n" + buffer.toString());
public static void testEnhancedForStatements(Collection<String> c) {
buffer = new StringBuffer();
for (String s : c) {
System.out.println("Collection elements from enhanced for statement : \n" +
public static void main(String args[]) {
List featureList = new ArrayList();
featureList.add("Java java = new Java();");
featureList.add("java.addFeature(\"enum facility\");");
featureList.add("java.addFeature(\"enhanced for\");");
featureList.add("java.addFeature(\"auto boxing\");");
featureList.add("java.addFeature(\"static import\");");


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.