SlideShare a Scribd company logo
1 of 43
GENERICS IN JAVA
BYLECTURERSURAJPANDEYCCTCOLLEGE
WHAT ARE GENERICS?
 At its core, the term generics means
parameterized types. Parameterized types are
important because they enable you to create
classes, interfaces, and methods in which the
type of data upon which they operate is specified
as a parameter. Using generics, it is possible to
create a single class, for example, that
automatically works with different types of data.
A class,interface, or method that operates on a
parameterized type is called generic, as in generic
class or generic method.
BYLECTURERSURAJPANDEYCCT
COLLEGE
GENERIC METHODS
 You can write a single generic method declaration that can be called with
arguments of different types. Based on the types of the arguments passed
to the generic method, the compiler handles each method call
appropriately. Following are the rules to define Generic Methods −
 All generic method declarations have a type parameter section delimited
by angle brackets (< and >) that precedes the method's return type ( < E >
in the next example).
 Each type parameter section contains one or more type parameters
separated by commas. A type parameter, also known as a type variable, is
an identifier that specifies a generic type name.
 The type parameters can be used to declare the return type and act as
placeholders for the types of the arguments passed to the generic method,
which are known as actual type arguments.
 A generic method's body is declared like that of any other method. Note
that type parameters can represent only reference types, not primitive
types (like int, double and char).
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Example
 Following example illustrates how we can print
an array of different type using a single Generic
method −
BYLECTURERSURAJPANDEYCCT
COLLEGE
BYLECTURERSURAJPANDEYCCT
COLLEGE
Output
Array integerArray
contains: 1 2 3 4 5
Array doubleArray
contains: 1.1 2.2 3.3
4.4 Array
characterArray
contains: H E L L O
BOUNDED TYPE PARAMETERS
 There may be times when you'll want to restrict the kinds
of types that are allowed to be passed to a type parameter.
For example, a method that operates on numbers might
only want to accept instances of Number or its subclasses.
This is what bounded type parameters are for.
 To declare a bounded type parameter, list the type
parameter's name, followed by the extends keyword,
followed by its upper bound.
 Example
 Following example illustrates how extends is used in a
general sense to mean either "extends" (as in classes) or
"implements" (as in interfaces). This example is Generic
method to return the largest of three Comparable objects −
BYLECTURERSURAJPANDEYCCT
COLLEGE
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Output
 Max of 3, 4 and 5 is 5
 Max of 6.6,8.8 and 7.7 is 8.8
 Max of pear, apple and orange is pear
BYLECTURERSURAJPANDEYCCT
COLLEGE
GENERICS WORK ONLY WITH REFERENCE
TYPES
 When declaring an instance of a generic type, the
type argument passed to the type parameter
must be a reference type. You cannot use a
primitive type, such as int or char.
 For example, with Gen, it is possible to pass
any class type to T, but you cannot pass a
primitive type to a type parameter. Therefore,
the following declaration is illegal:
 Gen<int> intOb = new Gen<int>(53); // Error,
can't use primitive type
BYLECTURERSURAJPANDEYCCT
COLLEGE
GENERIC CLASSES
 A generic class declaration looks like a non-
generic class declaration, except that the class
name is followed by a type parameter section.
 As with generic methods, the type parameter
section of a generic class can have one or more
type parameters separated by commas. These
classes are known as parameterized classes or
parameterized types because they accept one or
more parameters.
 Example
 Following example illustrates how we can define
a generic class −
BYLECTURERSURAJPANDEYCCT
COLLEGE
BYLECTURERSURAJPANDEYCCT
COLLEGE
BYLECTURERSURAJPANDEYCCT
COLLEGE
A GENERIC CLASS WITH TWO TYPE
PARAMETERS
 You can declare more than one type parameter in
a generic type. To specify two or more type
parameters, simply use a comma-separated list.
For example, the following TwoGen class is a
variation of the Gen class that has two type
parameters:
BYLECTURERSURAJPANDEYCCT
COLLEGE
 // A simple generic class with two type
 // parameters: T and V.
class TwoGen<T, V> {
T ob1;
V ob2;
 // Pass the constructor a reference to
 // an object of type T and an object of type V.
TwoGen(T o1, V o2) {
ob1 = o1;
ob2 = o2;
}
 // Show types of T and V.
void showTypes() {
System.out.println("Type of T is " +
ob1.getClass().getName());
System.out.println("Type of V is " +
ob2.getClass().getName());
}
T getob1() {
return ob1;
}
BYLECTURERSURAJPANDEYCCT
COLLEGE
V getob2() {
return ob2;
}
}
// Demonstrate TwoGen.
class SimpGen {
public static void main(String args[]) {
TwoGen<Integer, String> tgObj =
new TwoGen<Integer, String>(88, "Generics");
 // Show the types.
tgObj.showTypes();
 // Obtain and show values.
int v = tgObj.getob1();
System.out.println("value: " + v);
String str = tgObj.getob2();
System.out.println("value: " + str);
 }
 }
BYLECTURERSURAJPANDEYCCT
COLLEGE
 The output from this program is shown here:
 Type of T is java.lang.Integer
 Type of V is java.lang.String
 value: 88
 value: Generics
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Notice how TwoGen is declared:
 class TwoGen<T, V> {
 It specifies two type parameters: T and V, separated by a
comma. Because it has two type parameters, two type
arguments must be passed to TwoGen when an object is
created, as shown next:
 TwoGen<Integer, String> tgObj = new TwoGen<Integer,
String>(88, "Generics");
 In this case, Integer is substituted for T, and String is
substituted for V.
 Although the two type arguments differ in this example, it is
possible for both types to be the same. For example, the following
line of code is valid:
 TwoGen<String, String> x = new TwoGen<String, String> ("A",
"B");
 In this case, both T and V would be of type String. Of course, if
the type arguments were always the same, then two type
parameters would be unnecessary.
BYLECTURERSURAJPANDEYCCT
COLLEGE
THE GENERAL FORM OF A GENERIC
CLASS
 The generics syntax shown in the preceding
examples can be generalized. Here is the
 syntax for declaring a generic class:
 class class-name<type-param-list > { // …
 Here is the full syntax for declaring a reference
to a generic class and instance creation:
 class-name<type-arg-list > var-name = new class-
name<type-arg-list >(cons-arg-list);
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Bounded Types
 In the preceding examples, the type parameters
could be replaced by any class type. This is fine
for many purposes, but sometimes it is useful to
limit the types that can be passed to a type
parameter. For example, assume that you want
to create a generic class that contains a method
that returns the average of an array of numbers.
Furthermore, you want to use the class to obtain
the average of an array of any type of number,
including integers, floats, and doubles. Thus,
you want to specify the type of the numbers
generically, using a type parameter.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 To create such a class, you might try something like this:
 // Stats attempts (unsuccessfully) to
 // create a generic class that can compute
 // the average of an array of numbers of
 // any given type.
 //
 // The class contains an error!
class Stats<T> {
T[] nums; // nums is an array of type T
 // Pass the constructor a reference to
 // an array of type T.
Stats(T[] o) {
nums = o;
}
 // Return type double in all cases.
double average() {
double sum = 0.0;
for(int i=0; i < nums.length; i++)
sum += nums[i].doubleValue(); // Error!!!
return sum / nums.length;
 }
 }
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Creating a Generic Method
 As the preceding examples have shown, methods
inside a generic class can make use of a class’
type parameter and are, therefore, automatically
generic relative to the type parameter.
 However, it is possible to declare a generic
method that uses one or more type parameters of
its own. Furthermore, it is possible to create a
generic method that is enclosed within a non-
generic class.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Let’s begin with an example. The following
program declares a non-generic class called
 GenMethDemo and a static generic method
within that class called isIn( ). The isIn( )
method determines if an object is a member of an
array. It can be used with any type of object and
array as long as the array contains objects that
are compatible with the type of the object being
sought.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 // Demonstrate a simple generic method.
 class GenMethDemo {
 // 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();
BYLECTURERSURAJPANDEYCCT
COLLEGE
 // 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");
}
}
BYLECTURERSURAJPANDEYCCT
COLLEGE
 The output from the program is shown here:
 2 is in nums
 7 is not in nums
 two is in strs
 seven is not in strs
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Generic Types
 A generic type is a generic class or interface that is
parameterized over types. The following Box class will be
modified to demonstrate the concept.
 A Simple Box Class
 Begin by examining a non-generic Box class that operates on
objects of any type. It needs only to provide two methods: set,
which adds an object to the box, and get, which retrieves it:
 public class Box { private Object object; public void set(Object
object) { this.object = object; } public Object get() { return
object; } }
 Since its methods accept or return an Object, you are free to
pass in whatever you want, provided that it is not one of the
primitive types. There is no way to verify, at compile time,
how the class is used. One part of the code may place an
Integer in the box and expect to get Integers out of it, while
another part of the code may mistakenly pass in a String,
resulting in a runtime error.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Type Parameter Naming Conventions
 By convention, type parameter names are single,
uppercase letters. This stands in sharp contrast to the
variable naming conventions that you already know
about, and with good reason: Without this convention,
it would be difficult to tell the difference between a
type variable and an ordinary class or interface name.
 The most commonly used type parameter names are:
 E - Element (used extensively by the Java Collections
Framework)
 K - Key
 N - Number
 T - Type
 V - Value
 S,U,V etc. - 2nd, 3rd, 4th types
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Hierarchy and classification
 According to Java Language Specification:
 A type variable is an unqualified identifier. Type variables are introduced by generic class
declarations, generic interface declarations, Generic method declarations, and by generic
constructor declarations.
 A class is generic if it declares one or more type variables. These type variables are known
as the type parameters of the class. It defines one or more type variables that act as
parameters. A generic class declaration defines a set of parameterized types, one for each
possible invocation of the type parameter section. All of these parameterized types share
the same class at runtime.
 An interface is generic if it declares one or more type variables. These type variables are
known as the type parameters of the interface. It defines one or more type variables that
act as parameters. A generic interface declaration defines a set of types, one for each
possible invocation of the type parameter section. All parameterized types share the same
interface at runtime.
 A method is generic if it declares one or more type variables. These type variables are
known as the formal type parameters of the method. The form of the formal type parameter
list is identical to a type parameter list of a class or interface.
 A constructor can be declared as generic, independently of whether the class that the
constructor is declared in is itself generic. A constructor is generic if it declares one or more
type variables. These type variables are known as the formal type parameters of the
constructor. The form of the formal type parameter list
 is identical to a type parameter list of a generic class or interface.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 The Java Generics programming is introduced
in J2SE 5 to deal with type-safe objects.
 Before generics, we can store any type of objects
in collection i.e. non-generic. Now generics, forces
the java programmer to store specific type of
objects.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Advantage of Java Generics
 There are mainly 3 advantages of generics. They are as
follows:
 1) Type-safety : We can hold only a single type of objects
in generics. It doesn’t allow to store other objects.
 2) Type casting is not required: There is no need to
typecast the object.
 Before Generics, we need to type cast.
1. List list = new ArrayList();  
2. list.add("hello");  
3. String s = (String) list.get(0);//typecasting  
 After Generics, we don't need to typecast the object.
1. List<String> list = new ArrayList<String>();  
2. list.add("hello");  
3. String s = list.get(0);  
BYLECTURERSURAJPANDEYCCT
COLLEGE
 3) Compile-Time Checking: It is checked at
compile time so problem will not occur at
runtime. The good programming strategy says it
is far better to handle the problem at compile
time than runtime.
1. List<String> list = new ArrayList<String>();  
2. list.add("hello");  
3. list.add(32);//Compile Time Error  
 Syntax to use generic collection
 ClassOrInterface<Type>  
 Example to use Generics in java
 ArrayList<String>  
BYLECTURERSURAJPANDEYCCT
COLLEGE
FULL EXAMPLE OF GENERICS IN
JAVA
 Here, we are using the ArrayList class, but you
can use any collection class such as ArrayList,
LinkedList, HashSet, TreeSet, HashMap,
Comparator etc.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 import java.util.*;  
 class TestGenerics1{  
 public static void main(String args[]){  
 ArrayList<String> list=new ArrayList<String>();  
 list.add("rahul");  
 list.add("jai");  
 //list.add(32);//compile time error  
 String s=list.get(1);//type casting is not required  
 System.out.println("element is: "+s);  
 Iterator<String> itr=list.iterator();  
 while(itr.hasNext()){  
 System.out.println(itr.next());  
 }  
 }  
 }  
 Output:element is: jai
 rahul
 jai
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Example of Java Generics using Map
 Now we are going to use map elements using
generics. Here, we need to pass key and value.
Let us understand it by a simple example:
BYLECTURERSURAJPANDEYCCT
COLLEGE
 import java.util.*;  
 class TestGenerics2{  
 public static void main(String args[]){  
 Map<Integer,String> map=new HashMap<Integer,String>();  
 map.put(1,"vijay");  
 map.put(4,"umesh");  
 map.put(2,"ankit");   
 //Now use Map.Entry for Set and Iterator  
 Set<Map.Entry<Integer,String>> set=map.entrySet();  
 Iterator<Map.Entry<Integer,String>> itr=set.iterator();  
 while(itr.hasNext()){  
 Map.Entry e=itr.next();//no need to typecast  
 System.out.println(e.getKey()+" "+e.getValue());  
 }  
 }}  
 Output:1 vijay
 2 ankit
 4 umesh
BYLECTURERSURAJPANDEYCCT
COLLEGE
GENERIC CLASS
 A class that can refer to any type is known as
generic class. Here, we are using T type
parameter to create the generic class of specific
type.
 Let’s see the simple example to create and use
the generic class.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Creating generic class:
 class MyGen<T>{  
 T obj;  
 void add(T obj){this.obj=obj;}  
 T get(){return obj;}  
 }  
 The T type indicates that it can refer to any type
(like String, Integer, Employee etc.). The type
you specify for the class, will be used to store and
retrieve the data.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Using generic class:
 Let’s see the code to use the generic class.
 class TestGenerics3{  
 public static void main(String args[]){  
 MyGen<Integer> m=new MyGen<Integer>();  
 m.add(2);  
 //m.add("vivek");//Compile time error  
 System.out.println(m.get());  
 }}  
 Output:2
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Type Parameters
 The type parameters naming conventions are
important to learn generics thoroughly. The
commonly type parameters are as follows:
 T - Type
 E - Element
 K - Key
 N - Number
 V - Value
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Generic Method
 Like generic class, we can create generic method
that can accept any type of argument.
 Let’s see a simple example of java generic
method to print array elements. We are using
here E to denote the element.
BYLECTURERSURAJPANDEYCCT
COLLEGE
 public class TestGenerics4{  
    public static < E > void printArray(E[] elements) {  
         for ( E element : elements){          
             System.out.println(element );  
          }  
          System.out.println();  
     }  
     public static void main( String args[] ) {  
         Integer[] intArray = { 10, 20, 30, 40, 50 };  
         Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T'
 };  
         System.out.println( "Printing Integer Array" );  
         printArray( intArray  );   
        System.out.println( "Printing Character Array" );  
         printArray( charArray );   
     }   
 }  
BYLECTURERSURAJPANDEYCCT
COLLEGE
 Output:Printing Integer Array
 10
 20
 30
 40
 50
 Printing Character Array
 J
 A
 V
 A
 T
 P
 O
 I
 N
 T
BYLECTURERSURAJPANDEYCCT
COLLEGE
BYLECTURERSURAJPANDEYCCT
COLLEGE

More Related Content

What's hot

Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaAbhilash Nair
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 

What's hot (20)

Java collection
Java collectionJava collection
Java collection
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
07 java collection
07 java collection07 java collection
07 java collection
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java operators
Java operatorsJava operators
Java operators
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Generics in java

Similar to Generics in java (20)

Generics C#
Generics C#Generics C#
Generics C#
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Generic programming in java
Generic programming in javaGeneric programming in java
Generic programming in java
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
Templates
TemplatesTemplates
Templates
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
Generics
GenericsGenerics
Generics
 
Evolution of c# - by K.Jegan
Evolution of c# - by K.JeganEvolution of c# - by K.Jegan
Evolution of c# - by K.Jegan
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Hemajava
HemajavaHemajava
Hemajava
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
SOEN6441.generics.ppt
SOEN6441.generics.pptSOEN6441.generics.ppt
SOEN6441.generics.ppt
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Java execise
Java execiseJava execise
Java execise
 
Generics
GenericsGenerics
Generics
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 

More from suraj pandey

Systemcare in computer
Systemcare in computer Systemcare in computer
Systemcare in computer suraj pandey
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructorsuraj pandey
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.netsuraj pandey
 
Basic in Computernetwork
Basic in ComputernetworkBasic in Computernetwork
Basic in Computernetworksuraj pandey
 
History of computer
History of computerHistory of computer
History of computersuraj pandey
 
Basic of Internet&email
Basic of Internet&emailBasic of Internet&email
Basic of Internet&emailsuraj pandey
 
Basic fundamental Computer input/output Accessories
Basic fundamental Computer input/output AccessoriesBasic fundamental Computer input/output Accessories
Basic fundamental Computer input/output Accessoriessuraj pandey
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.netsuraj pandey
 
Transmission mediums in computer networks
Transmission mediums in computer networksTransmission mediums in computer networks
Transmission mediums in computer networkssuraj pandey
 
Introduction to vb.net
Introduction to vb.netIntroduction to vb.net
Introduction to vb.netsuraj pandey
 
Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computersuraj pandey
 
Computer Fundamental Network topologies
Computer Fundamental Network topologiesComputer Fundamental Network topologies
Computer Fundamental Network topologiessuraj pandey
 
Basic of Computer software
Basic of Computer softwareBasic of Computer software
Basic of Computer softwaresuraj pandey
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Javasuraj pandey
 
Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Javasuraj pandey
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)suraj pandey
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVAsuraj pandey
 

More from suraj pandey (20)

Systemcare in computer
Systemcare in computer Systemcare in computer
Systemcare in computer
 
vb.net Constructor and destructor
vb.net Constructor and destructorvb.net Constructor and destructor
vb.net Constructor and destructor
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
 
Basic in Computernetwork
Basic in ComputernetworkBasic in Computernetwork
Basic in Computernetwork
 
Computer hardware
Computer hardwareComputer hardware
Computer hardware
 
Dos commands new
Dos commands new Dos commands new
Dos commands new
 
History of computer
History of computerHistory of computer
History of computer
 
Dos commands
Dos commandsDos commands
Dos commands
 
Basic of Internet&email
Basic of Internet&emailBasic of Internet&email
Basic of Internet&email
 
Basic fundamental Computer input/output Accessories
Basic fundamental Computer input/output AccessoriesBasic fundamental Computer input/output Accessories
Basic fundamental Computer input/output Accessories
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
 
Transmission mediums in computer networks
Transmission mediums in computer networksTransmission mediums in computer networks
Transmission mediums in computer networks
 
Introduction to vb.net
Introduction to vb.netIntroduction to vb.net
Introduction to vb.net
 
Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computer
 
Computer Fundamental Network topologies
Computer Fundamental Network topologiesComputer Fundamental Network topologies
Computer Fundamental Network topologies
 
Basic of Computer software
Basic of Computer softwareBasic of Computer software
Basic of Computer software
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 
Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Java
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
 
Graphical User Interface in JAVA
Graphical User Interface in JAVAGraphical User Interface in JAVA
Graphical User Interface in JAVA
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

Generics in java

  • 2. WHAT ARE GENERICS?  At its core, the term generics means parameterized types. Parameterized types are important because they enable you to create classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter. Using generics, it is possible to create a single class, for example, that automatically works with different types of data. A class,interface, or method that operates on a parameterized type is called generic, as in generic class or generic method. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 3. GENERIC METHODS  You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods −  All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).  Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.  The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.  A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char). BYLECTURERSURAJPANDEYCCT COLLEGE
  • 4.  Example  Following example illustrates how we can print an array of different type using a single Generic method − BYLECTURERSURAJPANDEYCCT COLLEGE
  • 5. BYLECTURERSURAJPANDEYCCT COLLEGE Output Array integerArray contains: 1 2 3 4 5 Array doubleArray contains: 1.1 2.2 3.3 4.4 Array characterArray contains: H E L L O
  • 6. BOUNDED TYPE PARAMETERS  There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.  To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound.  Example  Following example illustrates how extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). This example is Generic method to return the largest of three Comparable objects − BYLECTURERSURAJPANDEYCCT COLLEGE
  • 8.  Output  Max of 3, 4 and 5 is 5  Max of 6.6,8.8 and 7.7 is 8.8  Max of pear, apple and orange is pear BYLECTURERSURAJPANDEYCCT COLLEGE
  • 9. GENERICS WORK ONLY WITH REFERENCE TYPES  When declaring an instance of a generic type, the type argument passed to the type parameter must be a reference type. You cannot use a primitive type, such as int or char.  For example, with Gen, it is possible to pass any class type to T, but you cannot pass a primitive type to a type parameter. Therefore, the following declaration is illegal:  Gen<int> intOb = new Gen<int>(53); // Error, can't use primitive type BYLECTURERSURAJPANDEYCCT COLLEGE
  • 10. GENERIC CLASSES  A generic class declaration looks like a non- generic class declaration, except that the class name is followed by a type parameter section.  As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.  Example  Following example illustrates how we can define a generic class − BYLECTURERSURAJPANDEYCCT COLLEGE
  • 13. A GENERIC CLASS WITH TWO TYPE PARAMETERS  You can declare more than one type parameter in a generic type. To specify two or more type parameters, simply use a comma-separated list. For example, the following TwoGen class is a variation of the Gen class that has two type parameters: BYLECTURERSURAJPANDEYCCT COLLEGE
  • 14.  // A simple generic class with two type  // parameters: T and V. class TwoGen<T, V> { T ob1; V ob2;  // Pass the constructor a reference to  // an object of type T and an object of type V. TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; }  // Show types of T and V. void showTypes() { System.out.println("Type of T is " + ob1.getClass().getName()); System.out.println("Type of V is " + ob2.getClass().getName()); } T getob1() { return ob1; } BYLECTURERSURAJPANDEYCCT COLLEGE
  • 15. V getob2() { return ob2; } } // Demonstrate TwoGen. class SimpGen { public static void main(String args[]) { TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics");  // Show the types. tgObj.showTypes();  // Obtain and show values. int v = tgObj.getob1(); System.out.println("value: " + v); String str = tgObj.getob2(); System.out.println("value: " + str);  }  } BYLECTURERSURAJPANDEYCCT COLLEGE
  • 16.  The output from this program is shown here:  Type of T is java.lang.Integer  Type of V is java.lang.String  value: 88  value: Generics BYLECTURERSURAJPANDEYCCT COLLEGE
  • 17.  Notice how TwoGen is declared:  class TwoGen<T, V> {  It specifies two type parameters: T and V, separated by a comma. Because it has two type parameters, two type arguments must be passed to TwoGen when an object is created, as shown next:  TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics");  In this case, Integer is substituted for T, and String is substituted for V.  Although the two type arguments differ in this example, it is possible for both types to be the same. For example, the following line of code is valid:  TwoGen<String, String> x = new TwoGen<String, String> ("A", "B");  In this case, both T and V would be of type String. Of course, if the type arguments were always the same, then two type parameters would be unnecessary. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 18. THE GENERAL FORM OF A GENERIC CLASS  The generics syntax shown in the preceding examples can be generalized. Here is the  syntax for declaring a generic class:  class class-name<type-param-list > { // …  Here is the full syntax for declaring a reference to a generic class and instance creation:  class-name<type-arg-list > var-name = new class- name<type-arg-list >(cons-arg-list); BYLECTURERSURAJPANDEYCCT COLLEGE
  • 19.  Bounded Types  In the preceding examples, the type parameters could be replaced by any class type. This is fine for many purposes, but sometimes it is useful to limit the types that can be passed to a type parameter. For example, assume that you want to create a generic class that contains a method that returns the average of an array of numbers. Furthermore, you want to use the class to obtain the average of an array of any type of number, including integers, floats, and doubles. Thus, you want to specify the type of the numbers generically, using a type parameter. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 20.  To create such a class, you might try something like this:  // Stats attempts (unsuccessfully) to  // create a generic class that can compute  // the average of an array of numbers of  // any given type.  //  // The class contains an error! class Stats<T> { T[] nums; // nums is an array of type T  // Pass the constructor a reference to  // an array of type T. Stats(T[] o) { nums = o; }  // Return type double in all cases. double average() { double sum = 0.0; for(int i=0; i < nums.length; i++) sum += nums[i].doubleValue(); // Error!!! return sum / nums.length;  }  } BYLECTURERSURAJPANDEYCCT COLLEGE
  • 21.  Creating a Generic Method  As the preceding examples have shown, methods inside a generic class can make use of a class’ type parameter and are, therefore, automatically generic relative to the type parameter.  However, it is possible to declare a generic method that uses one or more type parameters of its own. Furthermore, it is possible to create a generic method that is enclosed within a non- generic class. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 22.  Let’s begin with an example. The following program declares a non-generic class called  GenMethDemo and a static generic method within that class called isIn( ). The isIn( ) method determines if an object is a member of an array. It can be used with any type of object and array as long as the array contains objects that are compatible with the type of the object being sought. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 23.  // Demonstrate a simple generic method.  class GenMethDemo {  // 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(); BYLECTURERSURAJPANDEYCCT COLLEGE
  • 24.  // 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"); } } BYLECTURERSURAJPANDEYCCT COLLEGE
  • 25.  The output from the program is shown here:  2 is in nums  7 is not in nums  two is in strs  seven is not in strs BYLECTURERSURAJPANDEYCCT COLLEGE
  • 26.  Generic Types  A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.  A Simple Box Class  Begin by examining a non-generic Box class that operates on objects of any type. It needs only to provide two methods: set, which adds an object to the box, and get, which retrieves it:  public class Box { private Object object; public void set(Object object) { this.object = object; } public Object get() { return object; } }  Since its methods accept or return an Object, you are free to pass in whatever you want, provided that it is not one of the primitive types. There is no way to verify, at compile time, how the class is used. One part of the code may place an Integer in the box and expect to get Integers out of it, while another part of the code may mistakenly pass in a String, resulting in a runtime error. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 27.  Type Parameter Naming Conventions  By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.  The most commonly used type parameter names are:  E - Element (used extensively by the Java Collections Framework)  K - Key  N - Number  T - Type  V - Value  S,U,V etc. - 2nd, 3rd, 4th types BYLECTURERSURAJPANDEYCCT COLLEGE
  • 28.  Hierarchy and classification  According to Java Language Specification:  A type variable is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, Generic method declarations, and by generic constructor declarations.  A class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class at runtime.  An interface is generic if it declares one or more type variables. These type variables are known as the type parameters of the interface. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at runtime.  A method is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or interface.  A constructor can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type parameter list  is identical to a type parameter list of a generic class or interface. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 29.  The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects.  Before generics, we can store any type of objects in collection i.e. non-generic. Now generics, forces the java programmer to store specific type of objects. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 30.  Advantage of Java Generics  There are mainly 3 advantages of generics. They are as follows:  1) Type-safety : We can hold only a single type of objects in generics. It doesn’t allow to store other objects.  2) Type casting is not required: There is no need to typecast the object.  Before Generics, we need to type cast. 1. List list = new ArrayList();   2. list.add("hello");   3. String s = (String) list.get(0);//typecasting    After Generics, we don't need to typecast the object. 1. List<String> list = new ArrayList<String>();   2. list.add("hello");   3. String s = list.get(0);   BYLECTURERSURAJPANDEYCCT COLLEGE
  • 31.  3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is far better to handle the problem at compile time than runtime. 1. List<String> list = new ArrayList<String>();   2. list.add("hello");   3. list.add(32);//Compile Time Error    Syntax to use generic collection  ClassOrInterface<Type>    Example to use Generics in java  ArrayList<String>   BYLECTURERSURAJPANDEYCCT COLLEGE
  • 32. FULL EXAMPLE OF GENERICS IN JAVA  Here, we are using the ArrayList class, but you can use any collection class such as ArrayList, LinkedList, HashSet, TreeSet, HashMap, Comparator etc. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 33.  import java.util.*;    class TestGenerics1{    public static void main(String args[]){    ArrayList<String> list=new ArrayList<String>();    list.add("rahul");    list.add("jai");    //list.add(32);//compile time error    String s=list.get(1);//type casting is not required    System.out.println("element is: "+s);    Iterator<String> itr=list.iterator();    while(itr.hasNext()){    System.out.println(itr.next());    }    }    }    Output:element is: jai  rahul  jai BYLECTURERSURAJPANDEYCCT COLLEGE
  • 34.  Example of Java Generics using Map  Now we are going to use map elements using generics. Here, we need to pass key and value. Let us understand it by a simple example: BYLECTURERSURAJPANDEYCCT COLLEGE
  • 35.  import java.util.*;    class TestGenerics2{    public static void main(String args[]){    Map<Integer,String> map=new HashMap<Integer,String>();    map.put(1,"vijay");    map.put(4,"umesh");    map.put(2,"ankit");     //Now use Map.Entry for Set and Iterator    Set<Map.Entry<Integer,String>> set=map.entrySet();    Iterator<Map.Entry<Integer,String>> itr=set.iterator();    while(itr.hasNext()){    Map.Entry e=itr.next();//no need to typecast    System.out.println(e.getKey()+" "+e.getValue());    }    }}    Output:1 vijay  2 ankit  4 umesh BYLECTURERSURAJPANDEYCCT COLLEGE
  • 36. GENERIC CLASS  A class that can refer to any type is known as generic class. Here, we are using T type parameter to create the generic class of specific type.  Let’s see the simple example to create and use the generic class. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 37.  Creating generic class:  class MyGen<T>{    T obj;    void add(T obj){this.obj=obj;}    T get(){return obj;}    }    The T type indicates that it can refer to any type (like String, Integer, Employee etc.). The type you specify for the class, will be used to store and retrieve the data. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 38.  Using generic class:  Let’s see the code to use the generic class.  class TestGenerics3{    public static void main(String args[]){    MyGen<Integer> m=new MyGen<Integer>();    m.add(2);    //m.add("vivek");//Compile time error    System.out.println(m.get());    }}    Output:2 BYLECTURERSURAJPANDEYCCT COLLEGE
  • 39.  Type Parameters  The type parameters naming conventions are important to learn generics thoroughly. The commonly type parameters are as follows:  T - Type  E - Element  K - Key  N - Number  V - Value BYLECTURERSURAJPANDEYCCT COLLEGE
  • 40.  Generic Method  Like generic class, we can create generic method that can accept any type of argument.  Let’s see a simple example of java generic method to print array elements. We are using here E to denote the element. BYLECTURERSURAJPANDEYCCT COLLEGE
  • 41.  public class TestGenerics4{       public static < E > void printArray(E[] elements) {            for ( E element : elements){                        System.out.println(element );             }             System.out.println();        }        public static void main( String args[] ) {            Integer[] intArray = { 10, 20, 30, 40, 50 };            Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T'  };            System.out.println( "Printing Integer Array" );            printArray( intArray  );            System.out.println( "Printing Character Array" );            printArray( charArray );         }     }   BYLECTURERSURAJPANDEYCCT COLLEGE
  • 42.  Output:Printing Integer Array  10  20  30  40  50  Printing Character Array  J  A  V  A  T  P  O  I  N  T BYLECTURERSURAJPANDEYCCT COLLEGE