日期:2014-05-20  浏览次数:20751 次

java 泛型问题,通配符赋值,怎么解决?
下面的test方法中使用了通配符,但是两句test()函数的调用都有编译错误,为什么?说是参数不适用。
该怎么解决?

class Test {
HashMap<String, List<String>> maplist1 = new HashMap<String, List<String>>();
HashMap<String, List<Integer>> maplist2 = new HashMap<String, List<Integer>>();
public void a() {
test(maplist1);
test(maplist2);
}

public void test(Map<String, List<?>> maplist) {
System.out.println("OK");
}
}
------解决方案--------------------
这里你把传入的map里面封装list是?, 那么上面的传入的map也必须是?的  只要改的一致就可以了,
------解决方案--------------------
public <T> void test(Map<String, List<T>> maplist) {


System.out.println("OK");
}
------解决方案--------------------
/*
 * 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");
}
}

------解决方案--------------------
引用:
应该是:
HashMap<String, ArrayList<String>> maplist1 = new HashMap<String, ArrayList<String>>();
HashMap<String, ArrayList<Integer>> maplist2 = new HashMap<String, ArrayList<Integer>>();

------解决方案--------------------
引用:
Quote: 引用:

应该是:
HashMap<String, ArrayList<String>> maplist1 = new HashMap<String, ArrayList<String>>();
HashMap<String, ArrayList<Integer>> maplist2 = new HashMap<String, ArrayList<Integer>>();
你自己编译试试就知道你的对不对了
------解决方案--------------------
引用:
你自己编译试试就知道你的对不对了





------解决方案--------------------

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) {