Thursday, November 7, 2013

Generics

Generics is a powerful feature in Java programming language. It is implemented in Collections Framework of Java. The Generics allows you to create generic class, interface, method, and variable. A generic class, interface, method, or variable can work with different types of data because you can specify the type of data as a parameter. Since the generic class, interface, method, or variable does not restrict the type of data when it is defined, it can be reused. For example, you can create only a LinkedList class to store text, integer values, or floating-point values.
You can create a generic class or interface by placing the generic type parameter between the smaller and the greater signs (< and >) after name of the class. The example code below creates a generic class called ListNode. T is the generic type parameter. When you define a generic method or variable, you can place the generic type parameter as you do with a normal data type. This generic type parameter must be the same as the generic type parameter defined with the class.

Example:

class ListNode<T>
{
  public T val;
  public ListNode<T> next;
  public ListNode(T val) { this.val = val; next = null; }

}

When you use a generic class to store value by creating an object and pass the value to it, you should specify the specific data type of the value.

Example:

ListNote<Integer> node=new ListNode<Integer>(123);

You can use many generic type parameters in a class, interface, or methods. You will need to separate the type parameters by comma. See the example below.
Example:

class MyMap<T1,T2>{
  private T1 key;
  private T2 value;
  MyMap(T1 key, T2 value){
    this.key=key;
    this.value=value;
  }
}


Here is the complete LinkedList program that uses the Generics feature.

class ListNode<T>
    {
        public T val;
        public ListNode<T> next;
        public ListNode(T val) { this.val =val; next = null; }
       

    }

interface ListOperations<T>
    {
        void insertNode(T val, int post);
        void deleteNode(int post);
        void printList();
       
    }
class LinkedList<T> implements ListOperations<T>

{
    protected ListNode<T> pfirst; //pfirst points to the first element of the linked list
    protected ListNode<T> plast;//plast points to the last element of the linked list
    protected int NumItems;
    public LinkedList() { pfirst = plast = null; NumItems = 0; }
    
    public void insertNode(T val,int post)
    {
        ListNode<T> newNode = new ListNode<T>(val);
        if (pfirst == null)
        {
            pfirst = newNode;
            plast = newNode;
            NumItems++;

        }
        else
        {
            if (post == 1)
            {
                newNode.next = pfirst;
                pfirst =newNode;
                NumItems++;
            }

            else if(post>1 && post<=NumItems){
                ListNode<T> ta = pfirst;
                for (int i = 1; i < post - 1; i++) ta = ta.next;
                newNode.next = ta.next;
                ta.next = newNode;
                NumItems++;
            }
            else if (post == (NumItems + 1))
            {
                plast.next = newNode;
                plast = newNode;
                NumItems++;
            }
            else
            {
                System.out.println("Invalid position");

            }


        }

    }
    public void deleteNode(int post)
    {
        if (pfirst == null)
        {
        System.out.println("Empty list");
            return;
        }

        if (post == 1)
        {
            if (NumItems == 1)
            {
                pfirst = null;
                plast = null;
            }
            else
            {
                ListNode<T> temp = pfirst;
                pfirst = pfirst.next;
                temp = null;
            }
        }
        else if(post>=1 && post<=NumItems){
            ListNode<T> temp, ta;
            ta = pfirst;
            for(int i=0;i<post-1;i++) ta=ta.next;
            temp = ta.next;
            ta.next = temp.next;
            if (temp.next == null) plast = ta;
            temp = null;
        }

    }
    public void printList()
    {
        ListNode<T> ta = pfirst;
        while (ta != null)
        {
        System.out.println(ta.val);
            ta = ta.next;
        }
    }
    
    

public Object[] toArray(){

  Object[] arr=new Object[NumItems];
  ListNode<T> ta = pfirst;
  int i=0;
       while (ta != null)
       {
          arr[i]=ta.val;
           ta = ta.next;
           i++;
       }
       
       return arr;
  
   }
   

}



public class GenericsJava {
public static void main(String[] args){
//Creating linked list to store text
LinkedList<String> lstString = new LinkedList<String>();
lstString.insertNode("Item1", 1);
lstString.insertNode("Item2", 2);
lstString.insertNode("Item3", 3);
lstString.printList();
//creating linked list to store numbers
LinkedList<Integer> lstNumber = new LinkedList<Integer>();
lstNumber.insertNode(10, 1);
lstNumber.insertNode(13, 2);
lstNumber.insertNode(13, 3);
lstNumber.printList();

}


}

1 comment: