简体   繁体   中英

Java HashMap automatic value replicating issue

Whenever I start setting my HashMaps, the values get replicated. I believe that it is some Java rule that I don't understand when initilizing the HashMap. Below is my DTO of my HashMaps.

public class ClientsByMonth {
private int pax;
private int folios;
private int totalStays;
private HashMap<String, Integer> byCountry = new HashMap<>();   
private HashMap<String, Integer> groups = new HashMap<>();

Below is where I am initializing HashMaps.

public class CMBSetter{ 
private HashMap<Integer, Clients> clients = new HashMap<>();
private HashMap<Integer, ClientsByMonth> clientsBM = new HashMap<>();

 public void preSetterList(){
    // ----     --- COUNTRY SETTER ---      ----
     HashMap<String, Integer> byCountry = new HashMap();

    String[] countrys = {"GB ", "PT ", "ES ", "BE ", "IE ", "FR ", "DE ", "CH ", "IR ", "NL ", "   ", "Others"};
    for(int i = 0; i < 12; i++){
        byCountry.put(countrys[i], 0);

    }
    //  ****    *** GROUPS SETTER ***   ****
    HashMap<String, Integer> groups = new HashMap<>();
    Collection<String> keysGroup = groups.keySet();
    groups.put("test", 0);

    Collection<Integer> keysCleint = clients.keySet();

    for(Integer keyC: keysCleint){            
        String groupNameClient = clients.get(keyC).getGroupName();
        boolean namefound = false;

        for(String keyG: keysGroup){
            if(groupNameClient.equals(keyG)){
                namefound = true;
            }  
        }
        if(!namefound){
        groups.put(groupNameClient, 0);
        } 
    }
     //  _)_)_)_   )_)_  DTO SETTER )_)_   _)_)_)_

    for(int i = 0; i < 12; i++){

        clientsBM.put(i, new ClientsByMonth());
        clientsBM.get(i).setByCountry(byCountry[i]);
        clientsBM.get(i).setGroups(groups);
    }
}

My question:

How do I initialize the HashMaps so the values are not replicated when I set them? How do I initialize the HashMaps without this issue occurring?

What I am trying to do:

IE2- I want to the array of countries to fill in my byCountry HashMap in my DTO ClientsByMonth. Such as ("GB", 0) and ("IR", 0) and ("DE", 0).

IE2- I want the Groups setter to iterate through the clients HashMap and store the all the names that exist under GroupName() in my new HashMap which has a DTO object with a HashMap. Values such as HashMap groups(BIT, 0) and (BOOKING, 0) and (TRVLFAR, 0).

I am first creating (presetting) all the "labels/keys" in the HashMap because I am always getting Null pointer errors when I try to iterate over hash map that is empty.

Solution from the comments

Joop Eggen

One beginner's pitfall in doing something like clientsBM.get(i).setGroups(groups); is that you now are sharing the object held by groups. Any change afterwards to groups will hold for all clientsBM.get(i)

heniv181

"I am first creating (presetting) all the "labels/keys" in the HashMap because I am always getting Null pointer errors when I try to iterate over hash map that is empty". Use an Iterator from the keySet to avoid hitting null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM