Wednesday, March 6, 2013

Basic Java Generics

Java Generics allow you to write a class or method that are parametrized with a type. This requires a type argument be passed to a generic class when it is instantiated or passed when a generic method is called. Generics are a rather difficult subject to grasp as they are more complicated than the non-generic type specification. Generics were created with backward compatibility to interact with legacy Java code, so they were implemented using erasure instead of reification.

You just needs to remember that generic type checking is done at compile time when the type parameters are available. After compiling, the type parameters are erased and there are no run-time checks to insure types are compatible. If there are no errors or warnings at compile time, then you can be sure there will be no ClassCastExeception at run time.

Below is a simple example of using a generic class, called Commmand, that wraps an ArrayList. An ArrayList is a container that is part of the Java Collection Library and it is a generic class that is called with a type argument. In this example, I instantiate Command with a String type argument so it instantiates ArrayList<String> — making Command<ArrayList<String>> essentially behave like an ArrayList of ArrayList<String>, or in other words ArrayList<ArrayList<String>>.

The ArrayList is shuffled in random order using Collections.shuffle() and inserted into a new ArrayList which is then inserted into a Command object. This is done for as many times as there are elements in the ArrayList. Then all the elements of the Command object are printed to the console.

// Command Class:

import java.util.ArrayList;

public class Command<T> {
    
    ArrayList<T> cmdList;
    
    public Command() {
        cmdList = new ArrayList<>();
    }
    
    public void addElement(T e) {
        cmdList.add(e);
    }
    
    public void printElements() {
        for(T e: cmdList) {
            System.out.println(e);
        }
    }
}


// Client Class:

import java.util.ArrayList;
import java.util.Collections;

class Client {
    
    ArrayList<String> stringList = new ArrayList<>();
    
    public static void main(String[] args) {
        Client myClient = new Client();
        myClient.go();
    }
    
    void go() {
        stringList.add("Kowabunga!");
        stringList.add("Boulderdash!");
        stringList.add("Geronimo!");
        stringList.add("Hasta la vista!");
        stringList.add("Poppycock!");
        Command<ArrayList<String>> cmdObj = new Command<>();
        for(int i = 0; i < stringList.size(); i++) {
            Collections.shuffle(stringList);
            cmdObj.addElement(new ArrayList<>(stringList));
        }
        cmdObj.printElements(); 
    }
}


/* Output: 
 * [Boulderdash!, Poppycock!, Geronimo!, Kowabunga!, Hasta la vista!]
 * [Hasta la vista!, Boulderdash!, Poppycock!, Kowabunga!, Geronimo!]
 * [Hasta la vista!, Boulderdash!, Kowabunga!, Poppycock!, Geronimo!]
 * [Kowabunga!, Hasta la vista!, Geronimo!, Boulderdash!, Poppycock!]
 * [Hasta la vista!, Geronimo!, Boulderdash!, Kowabunga!, Poppycock!]
 */

No comments:

Post a Comment