Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归函数中的类型安全未知返回类型?_Java_Generics_Recursion_Unchecked - Fatal编程技术网

Java 递归函数中的类型安全未知返回类型?

Java 递归函数中的类型安全未知返回类型?,java,generics,recursion,unchecked,Java,Generics,Recursion,Unchecked,我有一种递归方法,可以在流中展平嵌套贴图。它返回object,因为我事先不知道嵌套的深度: static Stream<Object> nestedMapFlattener(Object o) { if (o instanceof Map<?, ?>) { return ((Map<?, ?>) o) .entrySet()

我有一种递归方法,可以在
流中展平嵌套贴图。它返回object,因为我事先不知道嵌套的深度:

static Stream<Object> nestedMapFlattener(Object o) {
    if (o instanceof Map<?, ?>) {
        return
                ((Map<?, ?>) o)
                        .entrySet()
                        .stream()
                        .flatMap(entry ->
                                nestedMapFlattener(entry.getValue())
                                        .map(entryOrSingle -> new AbstractMap.SimpleEntry<>(entry.getKey(), entryOrSingle))
                        )
                ;
    }
    return Stream.of(o);
}
静态流嵌套MapFlatter(对象o){
if(o映射的实例){
返回
((地图)o)
.entrySet()
.stream()
.flatMap(条目->
NestedMapFlatter(entry.getValue())
.map(entryOrSingle->new AbstractMap.SimpleEntry(entry.getKey(),entryOrSingle))
)
;
}
(o)的回流;
}
为了使用它,我在流中强制转换,当然会得到一个未检查的警告:

@Test
public void nestedMapFlattenerTest() {
    Map<Integer, Map<Integer, Map<Integer, Integer>>> threeFoldNestMap = new HashMap<Integer, Map<Integer, Map<Integer, Integer>>>() {{
        put(
                1,
                new HashMap<Integer, Map<Integer, Integer>>() {{
                    put(
                            11,
                            new HashMap<Integer, Integer>() {{
                                put(111, 1110);
                                put(112, 1120);
                            }}
                    );
                    put(
                            12,
                            new HashMap<Integer, Integer>() {{
                                put(121, 1210);
                                put(122, 1220);
                            }}
                    );
                }}
        );
        put(
                2,
                new HashMap<Integer, Map<Integer, Integer>>() {{
                    put(
                            21,
                            new HashMap<Integer, Integer>() {{
                                put(211, 2110);
                                put(212, 2120);
                            }}
                    );
                    put(
                            22,
                            new HashMap<Integer, Integer>() {{
                                put(221, 2210);
                                put(222, 2220);
                            }}
                    );
                }}
        );
        put(
                3,
                new HashMap<Integer, Map<Integer, Integer>>() {{
                    put(
                            31,
                            new HashMap<Integer, Integer>() {{
                                put(311, 3110);
                                put(312, 3120);
                            }}
                    );
                    put(
                            32,
                            new HashMap<Integer, Integer>() {{
                                put(321, 3210);
                                put(322, 3220);
                            }}
                    );
                }}
        );

    }};


     nestedMapFlattener(threeFoldNestMap)
        .map(o -> (Map.Entry<Integer, Map.Entry<Integer, Map.Entry<Integer, Integer>>>) o)
        .map(o -> "" + o.getKey() + " " + o.getValue().getKey() + " " + o.getValue().getValue().getKey() + " " + o.getValue().getValue().getValue())
        .forEach(System.out::println);
    //prints:
    //1 11 112 1120
    //1 11 111 1110
    //1 12 121 1210
    //1 12 122 1220
    //2 21 211 2110
    //2 21 212 2120
    //2 22 221 2210
    //2 22 222 2220
    //3 32 321 3210
    //3 32 322 3220
    //3 31 311 3110
    //3 31 312 3120
}
@测试
公共无效嵌套MapFlatterTest(){
Map threeFoldNestMap=新HashMap(){{
放(
1.
新HashMap(){{
放(
11,
新HashMap(){{
put(1111110);
put(1121120);
}}
);
放(
12,
新HashMap(){{
put(1211210);
put(1221220);
}}
);
}}
);
放(
2.
新HashMap(){{
放(
21,
新HashMap(){{
put(2112110);
put(212、2120);
}}
);
放(
22,
新HashMap(){{
put(2212210);
put(2222220);
}}
);
}}
);
放(
3.
新HashMap(){{
放(
31,
新HashMap(){{
put(3113110);
put(3123120);
}}
);
放(
32,
新HashMap(){{
put(3213210);
put(3223220);
}}
);
}}
);
}};
嵌套贴图展平器(三折贴图)
.map(o->(map.Entry)o)
.map(o->“”+o.getKey()+“”+o.getValue().getKey()+“”+o.getValue().getValue().getKey()+“”+o.getValue().getValue().getValue())
.forEach(System.out::println);
//印刷品:
//1 11 112 1120
//1 11 111 1110
//1 12 121 1210
//1 12 122 1220
//2 21 211 2110
//2 21 212 2120
//2 22 221 2210
//2 22 222 2220
//3 32 321 3210
//3 32 322 3220
//3 31 311 3110
//3 31 312 3120
}

我想知道,是否有更好的方法来实现这一点,因为我认为它有点滥用了类型安全的概念?

所以
nestedmapplatter
返回一个
Intger
流,对吗?这取决于具体情况。在测试用例中,它返回
Map.Entry
。但是,如果您仅使用整数的
列表
来计算它,它将返回
。因此
nestedMapFlatter
将返回
Intger
流,这取决于具体情况。在测试用例中,它返回
Map.Entry
。但是,如果只使用整数的
列表
来计算它,它将返回