(java@oneshot)logo/  logo/  logo/  logo/


Java Tutorial » Generics » Generics Basics  
12.1.7.Nested generic typePrevious/Next

A generic type is itself a type and can be used as a type variable. For example, if you want your List to store lists of strings:

List<List<String>> myListOfListsOfStrings;

To retrieve the first string from the first list in myList, you would use:

String s = myListOfListsOfStrings.get(0).get(0);

Working with List of Lists

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

public class MainClass {
  public static void main(String[] args) {
    List<String> listOfStrings = new ArrayList<String>();
    listOfStrings.add("Hello again");
    List<List<String>> listOfLists = new ArrayList<List<String>>();
    String s = listOfLists.get(0).get(0);
    System.out.println(s)// prints "Hello again"
12.1.Generics Basics
12.1.1. Life without Generics
12.1.2. What Are Generics? A Simple Generics Example
12.1.3. Generics Work Only with Objects
12.1.4. A Generic Class with Two Type Parameters
12.1.5. Introducing Generic Types
12.1.6. Working with generic List
12.1.7. Nested generic type
12.1.8. A generic type can accept more than one type variables.
12.1.9. Raw Types and Legacy Code