Java 如何在地图上迭代<;字符串,字符串>;和地图<;字符串,Map<;字符串,字符串>&燃气轮机;使用相同的递归函数?

Java 如何在地图上迭代<;字符串,字符串>;和地图<;字符串,Map<;字符串,字符串>&燃气轮机;使用相同的递归函数?,java,list,dictionary,recursion,compiler-errors,Java,List,Dictionary,Recursion,Compiler Errors,例如,为了使用相同的函数迭代List和List,我可以编写如下内容: import java.util.*; public class Test{ public static void print(Object obj) { if(obj instanceof List){ List list=(List)obj; System.out.print("["); for(Object obj2 : li

例如,为了使用相同的函数迭代List和List>,我可以编写如下内容:

import java.util.*;
public class Test{
    public static void print(Object obj) {
        if(obj instanceof List){
            List list=(List)obj;
            System.out.print("[");
            for(Object obj2 : list){
                print(obj2);
            }
            System.out.print("]");
        }else{
            System.out.print(obj+",");
        }
    }

    public static void main(String[] args){
        String l0="a";
        System.out.println(l0);

        List<String> l1=Arrays.asList("a","b");
        print(l1);

        System.out.println("");

        List<List<String> > l2=Arrays.asList(Arrays.asList("a","b"),Arrays.asList("c","d"));
        print(l2);
    }
}
现在我想迭代Map和Map>类似地,我尝试了:

import java.util.*;
public class Test{
    public static void print(Object obj) {
        if(obj instanceof Map){
            System.out.print("{");
            Map map=(Map)obj;
            for(Map.Entry<Object,Object> entry : map.entrySet()){
                print(entry.getKey());
                System.out.print(":");
                print(entry.getValue());
                System.out.print(",");
            }
            System.out.print("}");
        }else{
            System.out.print(obj);
        }
    }
    public static void main(String[] args){
        String m0="a";
        print(m0);

        System.out.println("");

        Map<String,String> m1=new HashMap<String,String>();
        m1.put("surname","Tom");
        m1.put("lastname","Bob");
        print(m1);

        System.out.println("");

        Map<String,HashMap<String,String>> m2=new HashMap<String,HashMap<String,String>>();
        HashMap<String,String> mm1=new HashMap<String,String>();
        mm1.put("surname","Tom");
        mm1.put("lastname","Bob");
        mm1.put("nickname","Penguin");
        m2.put("owner",mm1);
        HashMap<String,String> mm2=new HashMap<String,String>();
        mm2.put("name","Lucky");
        mm2.put("type","cat");
        m2.put("pet",mm2);
        print(m2);
    }
}
但它无法编译:

Test.java:20: error: incompatible types: Object cannot be converted to Entry<Object,Object>
        for(Map.Entry<Object,Object> entry : map.entrySet()){
Test.java:20:错误:不兼容的类型:无法将对象转换为条目
对于(Map.Entry:Map.entrySet()){

原因是什么?有可能修复它吗?如果没有,我如何像在图的开头递归迭代列表一样递归迭代嵌套映射?

A
map
与A
map
不同


它要么转换为
Map
,要么使用
Entry
,而不使用泛型。

A
Map
与A
Map
不同

它将转换为
映射
,或使用不带泛型的
条目

此处:

public static void print(Object obj) {
    if(obj instanceof Map){
        System.out.print("{");
        Map map=(Map)obj; // <-- raw map
   ....
该方法如下所示:

Set entrySet();
因此,此代码无法编译:

for (Map.Entry<Object, Object> entry : map.entrySet()) {
在这里:

该方法如下所示:

Set entrySet();
因此,此代码无法编译:

for (Map.Entry<Object, Object> entry : map.entrySet()) {

为什么在print()中定义
对象而不是
映射
参数?您可以尝试:
映射映射=(映射)obj;
?为什么在print()中定义
对象而不是
映射
参数?您可以尝试:
映射=(映射)obj;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
Map<?, ?> map = (Map<?, ?>) obj;
for (Map.Entry<?, ?> entry : map.entrySet()) {
    print(entry.getKey());
    System.out.print(":");
    print(entry.getValue());
    System.out.print(",");
}