Creating a Type-Specific List with generic List - Java Collection Framework

Java examples for Collection Framework:List

Description

Creating a Type-Specific List with generic List

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) throws Exception {
    List<Integer> intlist = new ArrayList<Integer>();

    intlist.add(new Integer(123));

    List<Number> numlist = new ArrayList<Number>();

    numlist.add(new Integer(123));
    numlist.add(new Float(123));
    intlist.add(null);//  w  w w . jav a  2 s  . com
    List<URL> urlList = new ArrayList<URL>();
    try {
      urlList.add(new URL("https://java2s.com"));
    } catch (MalformedURLException e) {
    }
    String s = urlList.get(0).getHost();
  }
}

Related Tutorials