我有一个字符串映射和一个映射,我想更改内部映射内的值,但我找不到执行此操作的方法。如何获取并更改内部 map 内的特定值?
这是我的 map :
String teamName;
Map<String, Integer> players = new HashMap<String,Integer>();
Map<String, Map> teams = new HashMap<String, Map>();
teams.put(teamName, players);
我想在球队 map 的球员 map 内增加或减少这个整数值。但由于我的其余代码,我无法直接使用玩家 map ,我需要从团队 map 到达它。
最佳答案
您所需要做的就是访问相应团队的 map ,然后设置号码。
如果您自始至终都使用泛型,将会有所帮助:
String teamName = "someteam";
Map<String, Integer> players = new HashMap<String,Integer>();
Map<String, Map<String, Integer>> teams = new HashMap<String, Map<String, Integer>>();
teams.put(teamName, players);
并以 teamName
作为键更改团队中的玩家数量:
teams.get(teamName).put("somename", 123);
//Or to directly overwrite with an increment:
int currentValue = teams.get(teamName).get("somename");
teams.get(teamName).put("somename", currentValue + 1);
关于java - 如何更改 map 中的 map 内的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087980/