Monday, August 21, 2017

Generics Learning Step by Step

List Interface:
---------------
The List interface represents a list of Object instances. This means that we could put any object into a List.

List list = new ArrayList();
list.add(new Integer(2));
list.add("a String");

Here we have inserted integer and string in list. If we want to read, then we need to cast as follows.

Integer integer = (Integer) list.get(0);
String string   = (String) list.get(1);

Why it is required to cast?
Ans:
Because multiple type of data are inserted in the list. So it is required to cast in access time. If we want to eradicate this casting we should use generics which confirms only single type of data will be inserted in the collection.

Advantages of Generic Features:
1. With the Java Generics features you can set the type of the collection to limit what kind of objects can be inserted into the collection.
2. You don't have to cast the values you obtain from the collection.

Example of Java Generic feature:
List<String> strings = new ArrayList<String>();
strings.add("a String");

String aString = strings.get(0);

What is diamond operator(<>)?
When you just write a diamond operator as generic type, the Java compiler will assume that the class instantiated is to have the same type as the variable it is assigned to. In the example, that means String because the List variable has String set as its type.
List<String> strings = new ArrayList<>();
=======================================================================================================

Iterator without generics example:
---------------------------------
    ArrayList names = new ArrayList();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");

    Iterator it = names.iterator();

    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }

Iterator with generics example:
-------------------------------
    ArrayList<String> names = new ArrayList<String>();
    names.add("Chaitanya");
    names.add("Steve");
    names.add("Jack");

    Iterator<String> it = names.iterator();

    while(it.hasNext()) {
      String obj = it.next();
      System.out.println(obj);
    }

Difference between Iterator and Enumeration:
--------------------------------------------

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
1) Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
2) Method names have been improved. hashNext() method of iterator replaced hasMoreElements() method of enumeration, similarly next() replaced nextElement().

=======================================================================================================
while vs for-each loop in generics:
-----------------------------------
List<String> strings = new ArrayList<String>();

//... add String instances to the strings list...

for(String aString : strings){
  System.out.println(aString);
}
For each loop:
-------------
1. This for-each loop iterates through all String instances kept in the strings list.
2. For each iteration, the next String instance is assigned to the aString variable.
3. This for-loop is shorter than original while-loop

while loop:
-----------
1.  you would iterate the collections "Iterator" and call Iterator.next() to obtain the next instance.
=======================================================================================================

Resource Link:
http://tutorials.jenkov.com/java-generics/generic-list.html
https://beginnersbook.com/2014/06/java-iterator-with-examples/

No comments:

Post a Comment