Creating a Generic Method

  • We can create a Generic method same as Generic class which operates on different type of data
  • we can create a generic method in a non-generic class also.
  • The syntax for a generic method:

    <type_param_list>ret-type meth-name (param-list) { // …

type parameters should be precedes the return type.

// Demonstrate a simple generic method.
public class Simple {
 // Determine if an object is in an array.
 static <T extends Comparable<T>, V extends T> boolean isIn(T x, V[] y) {
 for(int i=0; i < y.length; i++)
 if(x.equals(y[i])) return true;
 return false;
 }
 public static void main(String args[]) {
 // Use isIn() on Integers.
 Integer nums[] = { 1, 2, 3, 4, 5 };
 if(isIn(2, nums))
 System.out.println("2 is in nums");
 if(!isIn(7, nums))
 System.out.println("7 is not in nums");
 System.out.println();
 // Use isIn() on Strings.
 String strs[] = { "one", "two", "three",
 "four", "five" };
 if(isIn("two", strs))
 System.out.println("two is in strs");
 if(!isIn("seven", strs))
 System.out.println("seven is not in strs");
 // Oops! Won't compile! Types must be compatible.
// if(isIn("two", nums))
// System.out.println("two is in strs");
 }
}

Output:

2 is in nums

7 is not in nums

two is in strs

seven is not in strs

static<T extends Comparable<T>, V extends T> boolean isIn(T x, V[] y) {

  • T extends Comparable. Comparable is an interface declared in java.lang. A class that implements Comparable defines objects that can be ordered.
  • V must either be the same as type T, or a subclass of T.

Generic Constructors

  • we can make constructors as generic even if the class is not generic.
// Use a generic constructor.
class GenCons {
 private double val;
 <T extends Number> GenCons(T arg) {
 val = arg.doubleValue();
 }
 void showval() {
 System.out.println("val: " + val);
 }
}
public class Simple  {
 public static void main(String args[]) {
 GenCons test = new GenCons(100);
 GenCons test2 = new GenCons(123.5F);
 test.showval();
 test2.showval();
 }
}

Output:

val: 100.0

val: 123.5

Generic Interfaces

  • Generic interfaces are specified just like generic classes.
  • The generalized syntax for a generic interface:

interface interface-name<type-param-list> { // …

  • when a generic interface is implemented, we have to specify the type arguments

class class-name<type-param-list> implements interface-name <type-arg-list> {

// A generic interface example.
// A Min/Max interface.
interface MinMax<T extends Comparable<T>> {
 T min();
 T max();
}
// Now, implement MinMax
class MyClass<T extends Comparable<T>> implements MinMax<T> {
 T[] vals;
 MyClass(T[] o) { vals = o; }
 // Return the minimum value in vals.
 public T min() {
 T v = vals[0];
 for(int i=1; i < vals.length; i++)
 if(vals[i].compareTo(v) < 0) v = vals[i];
 return v;
 }
 // Return the maximum value in vals.
 public T max() {
 T v = vals[0];
 for(int i=1; i < vals.length; i++)
 if(vals[i].compareTo(v) > 0) v = vals[i];
 return v;
 }
}
public class Simple {
 public static void main(String args[]) {
 Integer inums[] = {3, 6, 2, 8, 6 };
 Character chs[] = {'b', 'r', 'p', 'w' ,'z'};
 MyClass<Integer> iob = new MyClass<Integer>(inums);
 MyClass<Character> cob = new MyClass<Character>(chs);
 System.out.println("Max value in inums: " + iob.max());
 System.out.println("Min value in inums: " + iob.min());
System.out.println("Max value in chs: " + cob.max());
 System.out.println("Min value in chs: " + cob.min());
 }
}

Output:

Max value in inums: 8

Min value in inums: 2

Max value in chs: z

Min value in chs: b

Raw Types and Legacy Code

  • Java allows a generic class to be used without any type arguments. This creates a raw type for the class.
// Create a raw-type Gen object and give it
 // a Double value.
 Gen raw = new Gen(new Double(98.6));
 // Cast here is necessary because type is unknown.
 double d = (Double) raw.getob();
 System.out.println("value: " + d);

results matching ""

    No results matching ""