Generics in java

It would be nice if we could write a single sort method that could sort the elements in an Integer array, a String array or an array of any type that supports ordering.

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively.

Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

Using Java Generic concept, we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements.

Generic Methods:

You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods:

  • All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).

  • Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.

  • The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

  • A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char).

Example:

Following example illustrates how we can print array of different type using a single Generic method:



 

    
package com.cv.java.generics;
/**
 @author Chandra Vardhan
 
 */
public class GenericMethodTest {
  // generic method printArray
  public static <E> void printArray(E[] inputArray) {
    // Display array elements
    for (E element : inputArray) {
      System.out.printf("%s ", element);
    }
  }
    

  public static void main(String args[]) {
    // Create arrays of Integer, Double and Character
    Integer[] intArray = 1234};
    Double[] doubleArray = 1.12.23.34.4 };
    Character[] charArray = 'H''E''L''L''O' };

    System.out.println("Array integerArray contains:");
    printArray(intArray)// pass an Integer array

    System.out.println("\nArray doubleArray contains:");
    printArray(doubleArray)// pass a Double array

    System.out.println("\nArray characterArray contains:");
    printArray(charArray)// pass a Character array
  }
}

This would produce the following result:

Array integerArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 

Array characterArray contains:
H E L L O

Bounded Type Parameters:

There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound.

Example:

Following example illustrates how extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). This example is Generic method to return the largest of three Comparable objects:



 

    
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 
 */

public class MaximumTest {
  // determines the largest of three Comparable objects
  public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
    T max = x; // assume x is initially the largest
    if (y.compareTo(max0) {
      max = y; // y is the largest so far
    }
    if (z.compareTo(max0) {
      max = z; // z is the largest now
    }
    return max; // returns the largest object
  }

  public static void main(String args[]) {
    System.out.printf("Max of %d, %d and %d is %d\n\n"345, maximum(345));

    System.out.printf("Maxm of %.1f,%.1f and %.1f is %.1f\n\n"6.68.87.7, maximum(6.68.87.7));

    System.out.printf("Max of %s, %s and %s is %s\n""pear""apple""orange",
        maximum("pear""apple""orange"));
  }
}

This would produce the following result:

aximum of 3, 4 and 5 is 5

aximum of 6.6, 8.8 and 7.7 is 8.8

aximum of pear, apple and orange is pear

Generic Classes:

A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section.

As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.

Example:

Following example illustrates how we can define a generic class:

 

    
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 
 */
public class Box<T> {

  private T t;

  public void add(T t) {
    this.t = t;
  }

  public T get() {
    return t;
  }

  public static void main(String[] args) {
    Box<Integer> integerBox = new Box<Integer>();
    Box<String> stringBox = new Box<String>();

    integerBox.add(new Integer(10));
    stringBox.add(new String("Hello World"));

    System.out.printf("Integer Value :%d\n\n", integerBox.get());
    System.out.printf("String Value :%s\n", stringBox.get());
  }

}

This would produce the following result:

Integer Value :10
String Value :Hello World


 

    
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 
 */
public class Generic<T> {
  private T object;

  public Generic(T object) {
    super();
    this.object = object;
  }

  public T getObject() {
    return object;
  }

  public void setObject(T object) {
    this.object = object;
  }

}




 

    
package com.cv.java.generics;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 @author Chandra Vardhan
 
 */
public class GenericsWithArrayListExample {

  public static void main(String[] args) {
    // This is without generics
    List list = new ArrayList();
    list.add("chandra");
    list.add(null);
    list.add(100);//There is no type safety
    list.add(true);//There is no type safety
    
    String s = (Stringlist.get(0);// type casting required
    System.out.println("First element is: " + s);

    Iterator itr = list.iterator();
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }

    List<String> stringList = new ArrayList<String>();
    stringList.add("chandra");
    stringList.add(null);
    stringList.add("100");
//    stringList.add(100);// type safety availablle here

    String s2 = stringList.get(0);// type casting is not required
    System.out.println("First element is: " + s2);

    Iterator<String> itr2 = stringList.iterator();
    while (itr2.hasNext()) {
      System.out.println(itr2.next());
    }
  }

}




 

    
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 
 */
public class GenericericTest {

  public static void main(String[] args) {
    Generic<String> g1 = new Generic<String>("chandra");
    System.out.println(g1.getObject());
    System.out.println(g1.getObject().getClass());
    
    Generic<Boolean> g2 = new Generic<Boolean>(true);
    System.out.println(g2.getObject());
    
    Generic<Double> g3 = new Generic<Double>(12.5);
    System.out.println(g3.getObject());

    Generic<Integer> g4 = new Generic<Integer>(100);
    System.out.println(g4.getObject());

    Generic<Object> g5 = new Generic<Object>(null);
    System.out.println(g5.getObject());

  }

}


 

    
/**
 
 */
package com.cv.java.generics;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 @author Chandra Vardhan
 *
 */
public class GenericsWithHashMapExample {
  public static void main(String[] args) {
    Map map = new HashMap();
    Key key = new Key("chandra");
    Key key2 = new Key("vardhan");
    Key key3 = new Key("kodam");

    Value v1 = new Value(100);
    Value v2 = new Value(200);
    Value v3 = new Value(300);

    map.put(key, v1);
    map.put(key2, v2);
    map.put(key3, v3);
    map.put(1"chandra");
    map.put("key"true);
    map.put(true, 100);

    Object object = map.get(1);

    String s1 = (Stringobject;
    System.out.println(s1);

    Object object1 = map.get(key);
    Value value = (Valueobject1;
    System.out.println(value);

    // Now use Map.Entry for Set and Iterator
    Set<Map.Entry> set = map.entrySet();

    Iterator<Map.Entry> itr = set.iterator();
    while (itr.hasNext()) {
      Map.Entry e = itr.next();// no need to typecast
      System.out.println(e.getKey() " " + e.getValue());
    }

    Map<Integer, String> genericMap = new HashMap<Integer, String>();
    genericMap.put(1"chandra");
    genericMap.put(2"vardhan");
    genericMap.put(3"kodam");

    // Now use Map.Entry for Set and Iterator
    Set<Map.Entry<Integer, String>> genericSet = genericMap.entrySet();

    Iterator<Map.Entry<Integer, String>> itr2 = genericSet.iterator();

    while (itr2.hasNext()) {
      Map.Entry e = itr2.next();// no need to typecast
      System.out.println(e.getKey() " " + e.getValue());
    }

  }
}


 

    
/**
 
 */
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 *
 */
public class Key {

  private String name;

  public Key(String name) {
    super();
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null: name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Key other = (Keyobj;
    if (name == null) {
      if (other.name != null)
        return false;
    else if (!name.equals(other.name))
      return false;
    return true;
  }

  @Override
  public String toString() {
    return "Key [name=" + name + "]";
  }

}


 

    
package com.cv.java.generics;
/**
 @author Chandra Vardhan
 *
 */
public class SimpleGeneric<T> {

  // declaration of object type T
  private T object = null;

  // constructor to accept type parameter T
  public SimpleGeneric(T param) {
    this.object = param;
  }

  public T getObjReff() {
    return this.object;
  }

  // this method prints the holding parameter type
  public void printType(){
        System.out.println("Type: "+object.getClass().getName());
    }
}


 

    
package com.cv.java.generics;
/**
 @author Chandra Vardhan
 *
 */
public class SimpleGenericsTest {

  public static void main(String[] args) {

    // we are going to create SimpleGeneric object with String as type
    // parameter
    SimpleGeneric<String> sgs = new SimpleGeneric<String>("Chandra");
    sgs.printType();
    // we are going to create SimpleGeneric object with Boolean as type
    // parameter
    SimpleGeneric<Boolean> sgb = new SimpleGeneric<Boolean>(true);
    sgb.printType();
  }
}


 

    
/**
 
 */
package com.cv.java.generics;

/**
 @author Chandra Vardhan
 *
 */
public class Value {

  private Integer id;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Value(Integer id) {
    super();
    this.id = id;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null: id.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Value other = (Valueobj;
    if (id == null) {
      if (other.id != null)
        return false;
    else if (!id.equals(other.id))
      return false;

    return true;
  }

  @Override
  public String toString() {
    return "Value [id=" + id + "]";
  }

}

No comments:

Post a Comment