Genric Data Type in Java


Java Generic Type will  enable programmers to specify, specific Data type in Java .we can use Generic for class,Method,Collection.

*  Suppose if we take example of Hashset normally when we create Object of set it will allow all data type in reference . so while retrieving value we need to perform type casting because we don't know which type of Data it is .

* So to overcome typecasting problem and enable to add only one type on data in reference   of set we use generic in Java

 --> How to create Generic type in Java
   Set<String> hashset=new HashSet<String>();//it will allow to add only String type
and while retrieving  no need to perform typecasting .

*   if we try to add other then String type then it will compile time error like "The method add(String) in the type Set<String> is not applicable for the arguments (int)"

==============Code of Generic in Collection ================


import java.util.*;


public class GenericType {
/*
 ** author:Anand
 */
  public static void main(String[] args){
   String value=null;
   //Let's create Hashset Object with Genric Data type
  Set<String> hashset=new HashSet<String>();//it will allow only String type to add on Reference
  hashset.add("Bangalore");
  hashset.add("Chennai");
  hashset.add("Hyderabad");
  Iterator<String> itr=hashset.iterator();//Iterate only String object
  while(itr.hasNext()){
   value=itr.next();
   System.out.println(value);
  }

 }
}

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java