gpt4 book ai didi

java - Java中 map Map的通用函数

转载 作者:行者123 更新时间:2023-12-01 08:57:17 25 4
gpt4 key购买 nike

我正在尝试编写一个接受以下两种数据类型的通用函数

Map <Integer, Map<Integer, Long>>
Map <Integer, Map<Integer, Double>>

我的函数是这样的,

function(Map<Integer, Map<Integer, ? extends Number>> arg) {}

但是我遇到了不兼容的类型错误。它适用于 map ,但不适用于 map 的 map 。我不明白为什么?有什么办法吗?

最佳答案

你可以尝试类似的东西

static <T extends Number> void function(Map<Integer, Map<Integer, T>> map) {}

public static void main(String[] args) {
Map<Integer, Map<Integer, Long>> map1 = new HashMap<Integer, Map<Integer, Long>>();
Map<Integer, Map<Integer, Double>> map2 = new HashMap<Integer, Map<Integer, Double>>();
Map<Integer, Map<Integer, String>> map3 = new HashMap<Integer, Map<Integer, String>>();
function(map1);
function(map2);
function(map3);// <-- compilation error here, String is not Number
}

关于java - Java中 map Map的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com