Need for Generics
Pre Java 5 Containers allows to insert an incorrect type into a Container.Consider a container you need to store Apple objects in it.The code for scenario is as shown below
public class Generics1
{
public static void main(String[] args)
{
List arrFruits = new ArrayList();
arrFruits.add(new Apple());
arrFruits.add(new Mango());
for (Object object : arrFruits)
{
System.out.println(((Apple)object).Name);
}
}
}
class Apple
{
String Name = "Ooty";
}
class Mango
{
String Name = "Malgova";
}
Now the above code compiles Perfectly fine.But when you try to run the code you will encounter a problem in casting to Apple class as you added Mango object in to arrFruits.The code compiles without error since every thing you add using add method into ArrayList gets converted to Object before it get Stored.
By Using generics you are preventing the addition of wrong type of Object into the container as Below.
public class Generics1
{
public static void main(String[] args)
{
List<Apple> arrFruits = new ArrayList<Apple>();
arrFruits.add(new Apple());
arrFruits.add(new Mango()); //Compile Time Error
for (Apple object : arrFruits)
{
System.out.println(object.Name);
}
}
}
You can also see there is no need for Casting while using Generics.
You can also store Subtype of Parent class in Generics as Below
public class Generics1
{
public static void main(String[] args)
{
ArrayList<Fruits> arrFruits = new ArrayList<Fruits>();
arrFruits.add(new Fruits());
arrFruits.add(new Apple());
arrFruits.add(new Orange());
arrFruits.add(new Mango());
for (Fruits object : arrFruits)
{
System.out.println(object.Name);
}
}
}
class Fruits
{
String Name = "I am Fruit";
}
class Apple extends Fruits
{
public Apple()
{
Name = "I am Apple";
}
}
class Orange extends Fruits
{
public Orange()
{
Name = "I am Orange";
}
}
class Mango extends Fruits
{
public Mango()
{
Name = "I am Mango";
}
}
The Containers can be Broadly Classified in to Two types.
1.Collection
2.Map
The Collection includes the following
1.ArrayList
2.LinkedList
3.HashSet
4.TreeSet
5.LinkedHashSet
The Map includes the following
1.HashMap
2.TreeMap
3.LinkedHashMap
Boxing
Boxing is the process of converting the Primitive type to Reference type
byte to Byte
int to Integer
double to Double
char to Character
Unboxing
Unboxing is the process of converting the Reference type to Primitive type
Byte to byte
Integer to int
Double to double
Consider the Following example
List arrAges = new ArrayList();
arrAges.add(25);
int n = arrAges.get(0);
is equivalent to
List arrAges = new ArrayList();
arrAges.add(new Integer(25));
int n = arrAges.get(0).intValue();
Boxing an int or short value between -128 and 127, a Char value between ‘\u0000’ and ‘\u007f’, byte and boolean
Consider the following example
Integer a = 3;
Integer b = 2;
Integer c = 5;
Integer d = a + b;
System.out.println(c == d);
Output
true
The output is true because the value comparison is taking place instead of object comparison.Boxed values between -128 to 127 are cached. Boxing uses Integer.valueOf method, which uses the cache. Values outside the range are not cached and always created as a new instance. Since your values fall into the cached range, values are equal using == operator.
This is What happening when values boxed are between -128 to 127
Integer a = 3;
Integer b = 2;
Integer c = 5;
Integer c = Integer.valueOf(5);
Integer d = Integer.valueOf(a.intValue() + b.intValue());
Output
true
when you try to add integers which are outside the range as one below
Integer a = 300;
Integer b = 200;
Integer c = 500;
Integer d = a + b;
System.out.println(c == d);
Output
false
How to Check Content in List with String
List arrNums = new ArrayList();
arrNums.add(2);
arrNums.add(3.14);
assert arrNums.toString().equals("[2,3.14]");
List is not a Subtype of List
List is a Subtype of Collection
Doesn’t Works
List ints = Arrays.asList(1,2);
List nums = ints; //Not Ok as you try to Assign List to List
The above doesn’t works as you try to add List to List
Works
List ints1 = Arrays.asList(1,2);
Collection nums = ints1;
Addition of Subtypes in Supertype in generics is Allowed as Below
List nums = new ArrayList();
List ints2 = Arrays.asList(1,2);
List ints3 = Arrays.asList(3.1,2.15);
nums.add(3.15);
nums.addAll(ints2);
nums.addAll(ints3);
Assignment of Subtype to Supertype is not allowed
List nums = new ArrayList();
List ints = Arrays.asList(1,2);
nums.addAll(ints); //Allowed
nums = ints; //Not Allowed
Assignment of List to List is Not Allowed
Use extends wildcard when you get values out of Structure and use super wildcard when you put values into structure
List<? extends Integer> arrNums = new ArrayList();
List arrInts = new ArrayList();
arrInts.add(1);
arrInts.add(2);
arrInts.add(3);
arrNums.addAll(arrInts); //Not Allowed since wildcard is extends(get)
List<? super Integer> arrNums = new ArrayList();
List arrInts = new ArrayList();
arrInts.add(1);
arrInts.add(2);
arrInts.add(3);
arrNums.addAll(arrInts); //Allowed since wildcard is super(put)