INTRODUCTION OF GENERIC COLLECTION

Introduction Of Generic Collection


Generic Collection:-

  • Generic collection is a combination of generics and collection frame work. It is JDK1.5 feature.

  • The below Example illustrates Generic collection.

Example 1:

 
            
              package com.sdj;
              import java.util.ArrayList;
              public class GenCollDemo
              {
                public static void main(String[] args)
                {
                  ArrayList list=new ArrayList();
                  list.add(" SDJ");
                  list.add("java     ");//here space also counting as one character 
                  list.add("j2ee");
                  list.add("oracle");
                  list.add(10);
                  /* here-> Exception in thread "main"  java.lang.ClassCastException: java.lang.Integer */
                  /* at com.sdj.gencollDemo.main(gencollDemo.java:15) */
                  for(Object obj:list)
                  {
                    String str=(String)obj;
                    System.out.println(str.length());
                    System.out.println(list);
                  }
                }
              } 
                   

Output:

5[ SDJ, java , j2ee, oracle] 9[ SDJ, java , j2ee, oracle] 4[ SDJ, java , j2ee, oracle] 6[ SDJ, java , j2ee, oracle]
     
     
  • We can’t use primitive datatypes for generic collection .we can use only Derived datatypes .

  • This is for primitive syntax : List list=new ArrayList( );// false.

  • This is for derived syntax : List list=new ArrayList( );// true

  • Syntax:

      List< Number> list=new ArrayList< Number>( );

Example 2:

 
            
              {
                ArrayList list =new ArrayList();
                list.add(new Double(20.23));
                list.add(new Float(20.55f));
                System.out.println(list);
              }       
                  

Output:

[20, 20.23, 20.55]

addAll( ):

    Syntax:

      addAll(collection <? extends E >)

Case 1.

  • ‘?’=Float,E=Integer there is relationship between float and interger (C.T.E)

Case 2.

  • ‘ ?’=Float,E=Number ,subtype for Number is Integer,Float,Double (C.S).

Example 3:

 
            
              package com.sdj;
              import java.util.ArrayList;
              import java.util.List;
              public class GenCollDemo1
              {
                public static void main(String[] args)
                {
                  List list =new ArrayList();
                  list.add(new Integer(20));
                  list.add(abc);
                  list.add(new Double(20.23));
                  list.add(new Integer(20));
                  System.out.println(list);
                }
              } 
                

Output:

[abc,20, 20.23, 20]
    Note
    • Generic type sets only for reference variable and not for object.
    • Generic scope defines only for reference variable and not for object.
    • Incase of Generic collections the Generic type should be same for reference variable and object creation.
    • If we want to assign super type to subtype then we can use below type.
      • List <? extends Number> list=new ArrayList<Integer>( );

    • The above List can be able to hold same type or subclass type to Number class.
    • By using ? extends it is not applicable to add element to that reference variable i.e list but we can retrieve .
    • “?”= represents any generic type (it is called as wild operator)
      • List<?>list = ! new List <Object > ( );

    • “?”Wild card can be used only for method parameter purpose.

Wild card Generic type:

  • “?” wild card Generic type is only applicable for reference variable but not for object creation.

  • Wild card represents assigned value as Generic type

    • List <? extends String > list=new ArrayList<? extends String >( );

  • Implement key word is not applicable for wild card operator Generic type .

How to add only super generic type:

  • By using super key word ,we can achieve

  • List<? super Number> list=new array List<Number>( ); //true

    List<? super Number> list=new array List< Object>( ); //false //true

    List<? super Number> list=new array List<String>( ); //false//true

  • It is not applicable because by using wildcard with super we can assign (or) add only same type (or) subtype but here we can assign super type i.e Integer and other type .

    Note
    • By using <? super> we can able to add sub type elements and not super type (or) other type.
      • List<? extends Number>list=new Arraylist<Integer>( );

    • If we use <?extends> as generic type ,Then can’t be able to add elements for the collection object.
      • List <? super Number>list=new Arraylist<Object>( );

    • Above generic list varible with <? super > can able to point out same (or) super generic type , but we can’t able to add same (or)subtype elements to list object.
    • If we can able to write then it is permitted only for same type(or)super type but not sub type .
    • In case of adding elements we can able to add same type (or) sub class.
    • Continues : sub type elements but not permission to add super type elements.

      List<? extends Number>list1=new ArrayList<Number>( );

      List<? extends Number>list2=new ArrayList<Integer>( );

      List<? extends Number>list3=newArrayList<Float>( );

Naming Convention
GenricCollection

Example 4:

 
            
              package com.sdj;
              import java.util.ArrayList;
              import java.util.List;
              abstract class Animal
              {
                public abstract void checkAnimal(); 
              }
              class Dog extends Animal
              {
                public void checkAnimal()
                {
                  System.out.println("Dog type");
                }

                class  cat extends Anima
                {
                  public void checkAnimal()
                  {
                    System.out.println("Cat type");
                  }
                }
                class  Bird  extends Animal
                {
                  public void checkAnimal()
                  {
                    System.out.println(" Bird type");
                  }
                }
                class Animaldoctor
                {
                  public void checkUpAnimal(List list)
                  {
                    for(Animal element:list)
                    {
                      element.checkAnimal();
                    }
                  }
                  public void addAnimal( Listlist1)
                  {
                    list1.add(new Dog());
                    list1.add(new  cat());
                    list1.add(new  Bird());
                  }
                  public class  AnimalsDemo
                  {
                    public static void main(String[] args)
                    {
                      List list= new ArrayList();   
                      list.add(new Dog());
                      list.add(new  cat());
                      list.add(new  Bird());  
                      Animaldoctor doct= new Animaldoctor();
                      doct.checkUpAnimal(list);
                      List list1=new ArrayList();
                      list1.add(new Dog());
                      list1.add(new  cat());
                      list1.add(new  Bird());
                      System.out.println(list1);
                    }
                  } 
                  

Output:

  Dog type
  Cat type
  Bird type