Java 填充对象的HashMap的HashMap
我有我正在处理的代码,但是我在填充HashMap的HashMap时遇到了问题。宣言是这样的Java 填充对象的HashMap的HashMap,java,object,maps,hashmap,Java,Object,Maps,Hashmap,我有我正在处理的代码,但是我在填充HashMap的HashMap时遇到了问题。宣言是这样的 HashMap<Games, HashMap<Store, Integer>> myMap = new HashMap<Games, HashMap<Store, Integer>>(); 存储类 我会这样做: void addToMap(Games games, Store store, int value) { HashMap<Sto
HashMap<Games, HashMap<Store, Integer>> myMap = new HashMap<Games, HashMap<Store, Integer>>();
存储类
我会这样做:
void addToMap(Games games, Store store, int value) {
HashMap<Store,Integer> m = myMap.get(games);
if (m == null) {
m = new HashMap<Store,Integer>();
myMap.put(games, m);
}
m.put(store, value);
}
本地商店:
public int hashCode() {
return nameOfStore.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof LocalStores)) {
return false;
}
LocalStores other = (LocalStores)obj;
return nameOfStore.equals(other.nameOfStore);
}
现在,为了简单起见,假设输入文件的每一行都包含三个由选项卡分隔的字段:游戏标题、商店名称和整数值。你可以这样读:
InputStream stream = new FileInputStream("myfile");
try {
Reader reader = new InputStreamReader(stream, "UTF-8"); // or another encoding
try {
BufferedInputStream in = new BufferedInputStream(reader);
try {
String line = in.readLine();
while (line != null) {
String[] fields = line.split("[\\t]");
if (fields.length == 3) {
addToMap(new Games(fields[0]), new LocalStores(fields[1]), Integer.parseInt(fields[2]));
}
line = in.readLine();
}
} finally {
in.close();
}
} finally {
reader.close();
}
} finally {
stream.close();
}
我会这样做:
void addToMap(Games games, Store store, int value) {
HashMap<Store,Integer> m = myMap.get(games);
if (m == null) {
m = new HashMap<Store,Integer>();
myMap.put(games, m);
}
m.put(store, value);
}
本地商店:
public int hashCode() {
return nameOfStore.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof LocalStores)) {
return false;
}
LocalStores other = (LocalStores)obj;
return nameOfStore.equals(other.nameOfStore);
}
现在,为了简单起见,假设输入文件的每一行都包含三个由选项卡分隔的字段:游戏标题、商店名称和整数值。你可以这样读:
InputStream stream = new FileInputStream("myfile");
try {
Reader reader = new InputStreamReader(stream, "UTF-8"); // or another encoding
try {
BufferedInputStream in = new BufferedInputStream(reader);
try {
String line = in.readLine();
while (line != null) {
String[] fields = line.split("[\\t]");
if (fields.length == 3) {
addToMap(new Games(fields[0]), new LocalStores(fields[1]), Integer.parseInt(fields[2]));
}
line = in.readLine();
}
} finally {
in.close();
}
} finally {
reader.close();
}
} finally {
stream.close();
}
您可能会提供游戏和存储的代码,似乎您也可以使用数据库来存储这些信息。JDK中包含一个数据库--JavaDB。您可以为游戏和存储提供代码。看起来,您还可以使用数据库来存储这些信息。JDK中包含一个数据库--JavaDB。你好,Maurice,你能解释一下你想说什么吗?这段代码将把一个整数映射到一对(游戏,商店)。然后可以通过myMap.get(games.get(store.ok.)检索整数。。但我正在考虑在这里填充对象。。。要使用文件中的字符串填充对象。。。作为标题。。。然后在指定生成的要指定的随机整数之前,在地图中创建对象的实例。。。然后从HashMap中进行查询。请你描述一下……正如有人评论的那样,你没有提供游戏和商店的代码。很难理解如何阅读这些东西…嗨,莫里斯,我已经包括了这个类。你好,莫里斯,你能解释一下你想说什么吗?这个代码将把一个整数映射到一对(游戏,商店)。然后可以通过myMap.get(games.get(store.ok.)检索整数。。但我正在考虑在这里填充对象。。。要使用文件中的字符串填充对象。。。作为标题。。。然后在指定生成的要指定的随机整数之前,在地图中创建对象的实例。。。然后从HashMap中进行查询。请你描述一下……正如有人评论的那样,你没有提供游戏和商店的代码。很难知道如何阅读这些东西…嗨,莫里斯,我已经包括了这些课程