日期:2014-05-20 浏览次数:20818 次
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools
------解决方案--------------------
Templates
* and open the template in the editor.
*/
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Administrator
*/
public class Test{
HashMap<String, ArrayList<String>> maplist1 = new HashMap<>();
HashMap<String, ArrayList<Integer>> maplist2 = new HashMap<>();
public void a() {
test(maplist1);
test(maplist2);
}
public <T> void test(Map<String, ArrayList<T>> maplist) {
System.out.println("OK");
}
public static void main(String[] args){
Test t=new Test();
t.a();
}
}
public class Test
{
public static void main(String[] args) throws Exception
{
List<Integer> listInteger = new ArrayList<Integer>();
List<String> listString = new ArrayList<String>();
printList(listInteger); //编译无错;
printList(listString);
HashMap<String, List<Integer>> maplist1 = new HashMap<String, List<Integer>>();
HashMap<String, List<String>> maplist2 = new HashMap<String, List<String>>();
test(maplist1); //编译有错;
test(maplist2);
}
//因为是泛型,
public static void printList(List<?> list)
{
System.out.println("print OK");
}
//因为是泛型,HashMap后的String 和 List<?> 都应该是确定的;
public void test(HashMap<String, List<?>> maplist22)
{
System.out.println("OK");
}
}
你自己编译试试就知道你的对不对了
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Test {
Map<String, List<String>> maplist1 = new HashMap<String, List<String>>();
Map<String, List<Integer>> maplist2 = new HashMap<String, List<Integer>>();
public void a() {
List<String> l1 = new ArrayList<String>();
l1.add("t1");
List<Integer> l2 = new ArrayList<Integer>();
l2.add(1);
maplist1.put("1", l1);
maplist2.put("1", l2);
test(maplist1);
test(maplist2);
}
public <T> void test(Map<String, List<T>> maplist) {