Creating a Type-Specific Map [5.0] - Java Collection Framework

Java examples for Collection Framework:Map

Description

Creating a Type-Specific Map [5.0]

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] argv) throws Exception {
    Map<Integer, String> map = new HashMap<Integer, String>();

    map.put(1, "first");
    map.put(2, "second");
    // map.put(1, 2); <- Syntax error
    Map<Number, String> numMap = new HashMap<Number, String>();

    numMap.put(.5, "half");
    numMap.put(1, "first");
    map.put(null, null);/*from   ww  w .  ja v a2s.c om*/
    Map<String, URL> urlMap = new HashMap<String, URL>();
    try {
      urlMap.put("java", new URL("http://java2s.com"));
    } catch (MalformedURLException e) {
    }
    String s = urlMap.get("java").getHost();
  }
}

Related Tutorials