Saturday, October 19, 2013

Generics Concept

Introduction:

Before Jdk1.5.. there is no way to restrict collections to accept only objects of specified type.

Before JDK1.5

If you say List  obList = new ArrayList();
You can add string,Student or Dog or Cat Objects..

But from JDK1.5 using generics...You can restrict list to be only of Strings or Dogs.

Eg:

List<String>  stringListOnly = new ArrayList<String>();

In the angular brackets you have to specify the type of objects you want to add in
to list.If you try to add any object other than String,compilation error occurs.


 Example:
-------------

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package corejava;

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

/**
 *
 * @author Rishitha
 */
public class GenericEx {
   
   
    public static void main(String args[]){
     
        /** Old Style of using Lists
         * You can add any type of objects
         * In the following example I have added both String Objects
         * and Integer Objects. I can't restrict the list to accept
         * only String Objects.
         *
         * The return type of get method is Object.So you need
         * to cast...
         *
         */
        List preGenericList = new ArrayList();
        preGenericList.add("Anil");
        preGenericList.add("Kumar");
        preGenericList.add(new Integer(10));
       
        for(int i=0;i<preGenericList.size();i++){
           
            Object obName = preGenericList.get(i);
             if(obName instanceof String){
                 String name =(String)obName;
                 System.out.println(name);
             }
           
        }
       
        System.out.println("**************Using Generics *******************");
        /**Here you can only add String objects
         * You need not cast to String specifically because
         * compiler adds cast for you
         */
        List<String> stringListOnly = new ArrayList<String>();
        stringListOnly.add("Anil");
        stringListOnly.add("Kumar");
       
       
        for(int i=0;i<stringListOnly.size();i++){
           
            String name = stringListOnly.get(i);
            System.out.println(name);
           
        }
       
    }
   
   
   
}


Question: How to make list to allow any type of object in to list Using Generics Syntax.

List<Object> anyTypeList = new ArrayList<Object>();
anyTypeList.add(new String());
anyTypeList.add(new Integer(10));

Example:
----------

package corejava;

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

/**
 *
 * @author Rishitha
 */
public class GenericEx {
  
  
    public static void main(String args[]){
    
        /** Following List allows all type of objects using Generics Syntax **/
        List<Object> anyTypeList = new ArrayList<Object>();

        anyTypeList.add(new String("Anil"));

        anyTypeList.add(new Integer(10));
      
        for(int i=0;i<anyTypeList.size();i++){
          
            Object name = anyTypeList.get(i);
            System.out.println(name);
          
        }
      
    }
}







No comments:

Post a Comment