Skip to content
Advertisement

HashMap has all the entries changed to last insert for every cycle

I need to insert a key and an element, containing the key and another value (which will be selected in the end from the map sorted by keys) into an HashMap. I decided to lastly convert the HashMap to a TreeMap to sort it, but the HashMap gets filled with data that change at every insert:

here is the code

-
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 Cyclen");
                        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) {
                    };
                }
            }       
            

The output is something like this:

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

Advertisement

Answer

You need to create a new Element for every put. You can’t just reuse the same one.

Move

Element element= new Element();

into the while loop.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement