HashMap 将所有条目更改为每个循环的最后插入
HashMap has all the entries changed to last insert for every cycle
我需要插入一个键和一个元素,包含键和另一个值(最后从按键排序的地图中选择)到一个HashMap中。我决定最后将 HashMap 转换为 TreeMap 以对其进行排序,但是 HashMap 中充满了每次插入时都会发生变化的数据:
这是代码
-
public class main {
public static void main(String[] args) throws FileNotFoundException {
Map<Integer, Element> map_football = new HashMap<>(); //map has key = string, value element_football
Map<Integer, Element> map_weather = new HashMap<>(); //map has key = int, value element_weather
Element element= new Element();
BufferedReader br = null;
String myFile="weather.dat";
//Wreader(map_weather,inputpath);
try {
File file = new File(myFile);
Scanner scanner = new Scanner(file);
scanner.nextLine(); //the first row is skipped
String fileContent = "";
while(scanner.hasNextLine()){ //reads the whole file
try{
Integer Dy=scanner.nextInt();
Integer MxT = scanner.nextInt();
Integer MnT = scanner.nextInt();
Integer exc=MxT-MnT;
element.setDy(Dy);
element.setExc(exc);
System.out.println(element.toString());
map_weather.put(exc, element);
map_weather.forEach((key, value) -> System.out.println(key + ":" + value));
System.out.println("New Cycle\n");
scanner.nextLine();
}catch(InputMismatchException ex){
scanner.nextLine();
}
}
//now i got my map with key=Dy and value=Mxt-MnT
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Always close the BufferedReader
if (br != null) {
try {
br.close();
}
catch (Exception e) {
};
}
}
输出是这样的:
Element{Dy=1, exc=29}
29:Element{Dy=1, exc=29}
New Cycle
Element{Dy=2, exc=16}
16:Element{Dy=2, exc=16}
29:Element{Dy=2, exc=16}
New Cycle
Element{Dy=3, exc=22}
16:Element{Dy=3, exc=22}
22:Element{Dy=3, exc=22}
29:Element{Dy=3, exc=22}
New Cycle
Element{Dy=4, exc=18}
16:Element{Dy=4, exc=18}
18:Element{Dy=4, exc=18}
22:Element{Dy=4, exc=18}
29:Element{Dy=4, exc=18}
New Cycle
您需要为每个 put
创建一个新的 Element
。你不能重复使用同一个。
移动
Element element= new Element();
进入while
循环。
我需要插入一个键和一个元素,包含键和另一个值(最后从按键排序的地图中选择)到一个HashMap中。我决定最后将 HashMap 转换为 TreeMap 以对其进行排序,但是 HashMap 中充满了每次插入时都会发生变化的数据:
这是代码
-
public class main {
public static void main(String[] args) throws FileNotFoundException {
Map<Integer, Element> map_football = new HashMap<>(); //map has key = string, value element_football
Map<Integer, Element> map_weather = new HashMap<>(); //map has key = int, value element_weather
Element element= new Element();
BufferedReader br = null;
String myFile="weather.dat";
//Wreader(map_weather,inputpath);
try {
File file = new File(myFile);
Scanner scanner = new Scanner(file);
scanner.nextLine(); //the first row is skipped
String fileContent = "";
while(scanner.hasNextLine()){ //reads the whole file
try{
Integer Dy=scanner.nextInt();
Integer MxT = scanner.nextInt();
Integer MnT = scanner.nextInt();
Integer exc=MxT-MnT;
element.setDy(Dy);
element.setExc(exc);
System.out.println(element.toString());
map_weather.put(exc, element);
map_weather.forEach((key, value) -> System.out.println(key + ":" + value));
System.out.println("New Cycle\n");
scanner.nextLine();
}catch(InputMismatchException ex){
scanner.nextLine();
}
}
//now i got my map with key=Dy and value=Mxt-MnT
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Always close the BufferedReader
if (br != null) {
try {
br.close();
}
catch (Exception e) {
};
}
}
输出是这样的:
Element{Dy=1, exc=29}
29:Element{Dy=1, exc=29}
New Cycle
Element{Dy=2, exc=16}
16:Element{Dy=2, exc=16}
29:Element{Dy=2, exc=16}
New Cycle
Element{Dy=3, exc=22}
16:Element{Dy=3, exc=22}
22:Element{Dy=3, exc=22}
29:Element{Dy=3, exc=22}
New Cycle
Element{Dy=4, exc=18}
16:Element{Dy=4, exc=18}
18:Element{Dy=4, exc=18}
22:Element{Dy=4, exc=18}
29:Element{Dy=4, exc=18}
New Cycle
您需要为每个 put
创建一个新的 Element
。你不能重复使用同一个。
移动
Element element= new Element();
进入while
循环。