每當我開始設置我的HashMap時,這些值就會被複制。我相信這是一些Java規則,我不明白什麼時候啓動HashMap。以下是我的HashMaps的DTO。Java HashMap自動值複製問題
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<>();
下面是我初始化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);
}
}
我的問題:
如何初始化包含HashMap,所以當我設置它們的值不會被複制? 如何在不發生此問題的情況下初始化HashMap?
我所試圖做的事:
I.E.2-我想國家的陣列來填補我的byCountry HashMap中在我的DTO ClientsByMonth。如(「GB」,0)和(「IR」,0)和(「DE」,0)。
I.E.2-我希望組設置器遍歷客戶端HashMap,並將存在於GroupName()下的所有名稱存儲在具有帶HashMap的DTO對象的新HashMap中。諸如HashMap組(BIT,0)和(BOOKING,0)和(TRVLFAR,0)之類的值。
我首先在HashMap中創建(預設)所有「標籤/鍵」,因爲當我嘗試在空的哈希映射上進行迭代時,總是收到空指針錯誤。
你究竟想達到什麼目的? 'HashMap [] byCountry'創建一個哈希映射數組,每個哈希映射都可以容納1個國家。你是否試圖初始化它:'private HashMap byCountry = new HashMap <>(); '在我粘貼的代碼上面? –
npinti
HashMap的數組 - 如何bizzare –
什麼是問題? – shmosel