这是我在Java中想要的数据的表示。
e.g。国家 - >联赛 - >季节 - >初始评级为0的团队
但问题在于,值似乎通过Java的值引用传递来覆盖。
因此,我总是在地图中获得一个结果,而不是一种列表。
在这里,您将看到我对象Season的实现。
public class Season {
private HashMap<String, Map<String, Map<String, Map<String, Double>>>> mapSeasonsLeaguesAndTeams;
public Season() {
this.mapSeasonsLeaguesAndTeams = new HashMap<String, Map<String, Map<String, Map<String, Double>>>>();
}
public void collectSeasons(String country, String league, String season,
String team) {
Map<String, Map<String, Double>> seasonTeam = new HashMap<String, Map<String, Double>>();
Map<String, Double> teamRating = new HashMap<String, Double>();
Map<String, Map<String, Map<String, Double>>> leagueSeason = new HashMap<String, Map<String, Map<String, Double>>>();
// check if the the country already exist otherwise create new
if (mapSeasonsLeaguesAndTeams.containsKey(country)) {
// check if the league already exists if that country exists
if (mapSeasonsLeaguesAndTeams.get(country).containsValue(league)) {
// check if the season exists if we know we have a valide country and league
if (mapSeasonsLeaguesAndTeams.get(country).get(league)
.containsKey(season)) {
// check if that team exists in the season of that league in that country
// if it exists, do nothing otherwise put it in
if (mapSeasonsLeaguesAndTeams.get(country).get(league)
.get(season).containsKey(team)) {
} else {
teamRating.put(team, 0.0);
mapSeasonsLeaguesAndTeams.get(country).get(league)
.put(season, teamRating);
}
}
} else {
teamRating.put(team, 0.0);
seasonTeam.put(season, teamRating);
mapSeasonsLeaguesAndTeams.get(country).put(league, seasonTeam);
}
} else {
teamRating.put(team, 0.0);
seasonTeam.put(season, teamRating);
leagueSeason.put(league, seasonTeam);
mapSeasonsLeaguesAndTeams.put(country, leagueSeason);
}
}
}
答案 0 :(得分:0)
我已经找到了解决方案。问题是它覆盖了现有的地图。 换句话说,我必须检查它是否已经存在
public void collectSeasons(String country, String league, String season,
String team) {
try {
if(!mapSeasonsLeaguesAndTeams.get(country).get(league).get(season).containsKey(team)){
mapSeasonsLeaguesAndTeams.get(country).get(league).get(season).put(team, 0.0);
}
} catch (Exception e) {
if (mapSeasonsLeaguesAndTeams.containsKey(country)) {
if (mapSeasonsLeaguesAndTeams.get(country).containsKey(league)) {
Map<String,Double> teamRating = new HashMap<String,Double>();
Map<String, Map<String, Double>> seasonTeam = new HashMap<String, Map<String, Double>>();
teamRating.put(team, 0.0);
seasonTeam.put(season, teamRating);
mapSeasonsLeaguesAndTeams.get(country).get(league).put(season, teamRating);
} else {
Map<String,Double> teamRating = new HashMap<String,Double>();
Map<String, Map<String, Double>> seasonTeam = new HashMap<String, Map<String, Double>>();
teamRating.put(team, 0.0);
seasonTeam.put(season, teamRating);
mapSeasonsLeaguesAndTeams.get(country).put(league, seasonTeam);
}
} else {
Map<String,Double> teamRating = new HashMap<String,Double>();
Map<String, Map<String, Double>> seasonTeam = new HashMap<String, Map<String, Double>>();
Map<String, Map<String, Map<String, Double>>> leagueSeason = new HashMap<String, Map<String, Map<String, Double>>>();
teamRating.put(team, 0.0);
seasonTeam.put(season, teamRating);
leagueSeason.put(league, seasonTeam);
mapSeasonsLeaguesAndTeams.put(country, leagueSeason);
}
}
}
因此,我使用了try catch。换句话说,将“新团队”添加到嵌套的哈希映射中“只是尝试”。
如果失败了。检查哪个hashmap“还没有”。如果您已找到它,请创建所需的哈希图并添加它们
with:
mapSeasonsLeaguesAndTeams.get(country).get(league).get(season).get(team)
我得到了所需的结果+它很灵活。