Learn Java Tutorial for Beginners (Video), Part 30: Using Generics

This tutorial is on generics; a system for creating "parametrized" classes that allow classes to work with other objects, the type of which you can specify when you instantiate (create objects from) the class. Confused? Hopefully this gentle introduction will help. In this video I'll show you a simple example of a parametrized class, ArrayList, and we'll look at how the usage of this class has changed since generics were introduced.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.



Code for this tutorial:

App.java:

 

import java.util.ArrayList;
import java.util.HashMap;

class Animal {
    
}


public class App {

    public static void main(String[] args) {
        
        /////////////////// Before Java 5 ////////////////////////
        ArrayList list = new ArrayList();
        
        list.add("apple");
        list.add("banana");
        list.add("orange");
        
        String fruit = (String)list.get(1);
        
        System.out.println(fruit);
        
        /////////////// Modern style //////////////////////////////
        
        ArrayList<String> strings = new ArrayList<String>();
        
        strings.add("cat");
        strings.add("dog");
        strings.add("alligator");
        
        String animal = strings.get(1);
        
        System.out.println(animal);
        
        
        ///////////// There can be more than one type argument ////////////////////
        
        HashMap<Integer, String> map = new HashMap<Integer, String>();
        
        
        //////////// Java 7 style /////////////////////////////////
        
        ArrayList<Animal> someList = new ArrayList<>();
    }

}