Using Generics in Java

package tt.taher.javahunter;

import java.util.ArrayList;
import java.util.List;

/**
 *@author taher_javahunter
 * Using Generics in Java
 */
public class Generics_JavaHunter<T extends Object> {

    public static void main(String[] args){
        Generics_JavaHunter test = new Generics_JavaHunter();
        List<Object> list1 = test.getData(1);
        System.out.println("List 1 : "+list1.size());
        for (Object object : list1) {
            System.out.println(object);
        }

        List<Object[]> list2 = test.getData(2);
        System.out.println("List 2 : "+list2.size());
        for (Object[] object : list2) {
            System.out.println(object[0]);
            System.out.println(object[1]);
        }
    }

    public List<T> getData(int i){
        List<T> list= new ArrayList<T>();
        if(i==1){
            Object data1 = "123";
            list.add((T) data1);
        }else{
            Object[] data1 = {"abc","xyz"};
            list.add((T) data1);
        }
        return list;
    }
}

Wildcards

There are three types of wildcards:

  1. “? extends Type”: Denotes a family of subtypes of type Type. This is the most useful wildcard
  2. “? super Type”: Denotes a family of supertypes of type Type
  3. “?”: Denotes the set of all types or any

Shape.java(Super Class)


package generic;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author taher_javaHunter
 */
public  class Shape{

    public void draw(List<? super Rectangle> list){
        list.add(new Rectangle());
        System.out.println("");
    }
    public void draw2(List<? extends Shape> list){
        
    }

    public static void main(String[] args) {
        Shape Shape =  new Shape();
        List<Shape> list = new ArrayList<Shape>();
        list.add(new Shape());
        list.add(new Shape());
        list.add(new Shape());
        list.add(new Shape());
        list.add(new Shape());
        Shape.draw(list);
    }
}

Rectangle.java(Sub Class)

package generic;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author taher_javaHunter
 */
public class Rectangle extends Shape{

    @Override
    public void draw2(List<? extends Shape> list) {
        int i=1;
        for (Shape shape : list) {
            System.out.println("Cnt : "+(i++));
        }
    }

    public static void main(String[] args) {
        Rectangle rectangle =  new Rectangle();
        List<Rectangle> list = new ArrayList<Rectangle>();
        list.add(new Rectangle());
        list.add(new Rectangle());
        list.add(new Rectangle());
        list.add(new Rectangle());
        list.add(new Rectangle());
        rectangle.draw2(list);
    }
}

Advertisements
This entry was posted in Core JAVA, Just 4 Knowledge. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s