Jessica Dionisio
Jessica Dionisio

Reputation: 151

Adding new key-value pair gets other keys' values replaced in HashMap

So, I have a HashMap<String,ArrayList> that stores an arraylist per String. But when I add another pair with new value of ArrayList, the other key values are being replaced. Hence, all the values for the different keys are getting the same.

public class Reports{
    
    private ArrayList<Resource> resourceList;
    private HashMap<String,ArrayList<Resource>> consolidatedAttendance = new HashMap<String,ArrayList<Resource>>(); 
    
    public void readReport(String reportFile){
        //initialized with resources from config file
        ArrayList<Resource> repResourceList = new ArrayList<Resource>(getResourceList());

        try (BufferedReader br = new BufferedReader(new FileReader(reportFile))) {
            String line;
            line = br.readLine(); // disregards first line (columns)
            while ((line = br.readLine()) != null) {
                
                if(line.length()==0){
                    break;
                }
                
                //store each resource status in resourceList
                String[] values = line.split(",");      
                String resourceName = values[1], resourceStatus = values[2];
                int resourceIndex = indexOfResource(resourceList, resourceName);
                // to add validation 
                if(resourceIndex!=-1){
                    repResourceList.get(resourceIndex).setStatus(resourceStatus);
                }
                
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        
        
        //get Date
        String reportFilename = reportFile.substring(0, reportFile.indexOf("."));
        String strDate = reportFilename.substring(reportFilename.length()-9);
        consolidateRecords(strDate, new ArrayList<Resource>(repResourceList));
        
    }
    
    
    public void consolidateRecords(String strDate, ArrayList<Resource> repResourceList){
        //consolidate records in hashmap
        consolidatedAttendance.put(strDate, repResourceList);
        
        // test print
        
        for (String key: consolidatedAttendance.keySet()){
            ArrayList<Resource> resources = consolidatedAttendance.get(key);  
            for(Resource resource: resources){
                System.out.println(key+": "+resource.getNickname()+" "+resource.getEid()+" "+resource.getStatus());
            }
        } 
    }
}

So the output for the map when it is printed is:

First key added:

"21-Dec-20": John Working
"21-Dec-20": Alice Working
"21-Dec-20": Jess Working

For second key, there's difference in the list. But, When second key is added (after put() method), the first key's values have been replaced.

"21-Dec-20": John SL
"21-Dec-20": Alice Working
"21-Dec-20": Jess SL

"28-Dec-20": John SL
"28-Dec-20": Alice Working
"28-Dec-20": Jess SL

Upvotes: 1

Views: 386

Answers (1)

Eran
Eran

Reputation: 393841

The values of your Map are Lists whose elements are the same as the elements of the List returned by getResourceList(). The fact that you are creating a copy of that List (twice), doesn't change that.

If each call to getResourceList() returns a List containing the same instances, all the keys in your Map will be associated with different Lists that contain the same instances.

Upvotes: 2

Related Questions