Java按值传递引用问题,Map in Map in Map in Map
这是我想要的Java数据的表示形式 e、 g.初始评级为0的国家-->联盟-->赛季-->团队Java按值传递引用问题,Map in Map in Map in Map,java,hashmap,Java,Hashmap,这是我想要的Java数据的表示形式 e、 g.初始评级为0的国家-->联盟-->赛季-->团队 英格 PRL 2013/14 阿森纳0 曼彻斯特。0 2014/15 阿森纳0 曼彻斯特。0 恰恰 2013/14 蒙古包 包子 2013/14 拜耳纳米0 但问题是,这些值似乎被Java的pass-by-reference-by-value覆盖 因此,我得到的,总是地图上的一个结果,而不是一种列表 在这里,您将看到我对对象季节的实现 public class Se
- 英格
- PRL
- 2013/14
- 阿森纳0
- 曼彻斯特。0
- 2014/15
- 阿森纳0
- 曼彻斯特。0
- 2013/14
- 恰恰
- 2013/14
- PRL
- 蒙古包
- 包子
- 2013/14
- 拜耳纳米0
- 2013/14
- 包子
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);
}
}
}
公共课季{
私人HashMap季节性分析和团队;
公众假期({
this.mapseasesleaguesandteams=new HashMap();
}
公共赛季(弦乐国家队、弦乐联盟、弦乐赛季、,
(弦乐队){
Map seasionteam=newhashmap();
Map teamRating=newhashmap();
Map leagueSeason=新HashMap();
//检查该国家/地区是否已存在,否则创建新的
if(地图季节性AGUESANDTEAMS.containsKey(国家)){
//检查联盟是否已经存在,如果该国家存在
如果(映射季节性队列和团队。获取(国家)。包含值(联赛)){
//如果我们知道我们有一个有效的国家和联赛,检查这个赛季是否存在
如果(地图季节)球队和球队。获得(国家)。获得(联赛)
.containsKey(季节)){
//检查该队在该国联赛的赛季中是否存在
//如果存在,请不要做任何事情,否则请将其放入
如果(地图季节)球队和球队。获得(国家)。获得(联赛)
.get(季节).containsKey(团队)){
}否则{
teamRating.put(团队,0.0);
地图季节联赛和球队。获取(国家)。获取(联盟)
.put(季节、团队评级);
}
}
}否则{
teamRating.put(团队,0.0);
季节团队。put(季节,团队评级);
地图季节性球队和球队。获得(国家队)。放置(联赛,季节性球队);
}
}否则{
teamRating.put(团队,0.0);
季节团队。put(季节,团队评级);
联赛.推杆(联赛,赛季队);
地图季节性比赛和团队。推杆(国家队、联赛);
}
}
}
我已经提出了一个解决方案。问题是它覆盖了现有的地图。
换句话说,我必须检查它是否已经存在
解决方案
我得到了所需的结果+它是灵活的 封装是您的朋友。维护此代码对我来说是一场噩梦:(我会改变设计,至少让我自己的类包含
Map
s来存储数据,然后将这些类关联起来,而不是使用普通的MapI。我有一个小建议给你…不要使用字符串作为嵌套映射的键。如果你能帮上忙的话,不要嵌套映射。这是我觉得AP不是答案,而是问题所在。
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);
}
}
}
mapSeasonsLeaguesAndTeams.get(country).get(league).get(season).get(team)