Tuesday, 16 May 2017

Java Tutorial: Generics in java | Java Generics [How to define a Generic method - addAndReturn]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ghV0jvqIzvo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Generics in java | Java Generics [How to define a Generic method - addAndReturn] 
GenericDemo.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GenericDemo
{
    public static void main(String[] args)
            throws IllegalAccessException, InstantiationException
    {

        String stringElement = "Peter";
        List<String> stringList = new ArrayList<String>();

        String strReturnElement = addAndReturn(stringElement, stringList);
        System.out.println(strReturnElement);

        Integer integerElement = new Integer(500);
        List<Integer> integerList = new ArrayList<Integer>();

        Integer integerReturnElement = addAndReturn(integerElement,
                                                        integerList);
        System.out.println(integerReturnElement);

    }

    public static <T> T addAndReturn(T element, Collection<T> collection)
    {
        collection.add(element);
        return element;
    }

}
Output
Peter
500
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsMethodDemo_Add_R_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsMethodDemo_Add_R_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/GenericsMethodDemo_Add_R_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge