- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
每当我开始设置 HashMap 时,值就会被复制。我相信这是我在初始化HashMap时不理解的一些Java规则。下面是我的 HashMap 的 DTO。
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<>();
下面是我初始化 HashMap 的地方。
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-我想要国家数组来填写我的 DTO ClientsByMonth 中的 byCountry HashMap。如(“GB”,0)和(“IR”,0)和(“DE”,0)。
I.E.2-我希望 Groups setter 迭代客户端 HashMap 并将 GroupName() 下存在的所有名称存储在我的新 HashMap 中,该新 HashMap 具有带有 HashMap 的 DTO 对象。 HashMap groups(BIT, 0) 和 (BOOKING, 0) 和 (TRVLFAR, 0) 等值。
我首先在 HashMap 中创建(预设)所有“标签/键”,因为当我尝试迭代空的 HashMap 时,我总是遇到空指针错误。
最佳答案
解决方案来自评论
乔普·埃根
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.
关于Java HashMap 自动值复制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36883385/
我有不同的结构,它们都包含一个 HashMap与 String作为键,但具有不同的值类型。例如,一个结构有一个类型为 HashMap 的成员, 另一个将有一个 HashMap 类型的成员, 等等。 我
我想制作一个包含学生姓名和科目的板,每个学生在每个科目中都有一个成绩(或者没有..他可以离开考试而不写,然后他的案子将是空的)。我只想使用 HashMap。我的意思是,它会是这样的: HashMap>
是否有内存和速度高效的方法来在 HashMap 中动态存储唯一键:值对? key 保证是唯一的,但它们的数量经常变化。插入和删除必须很快。 我所做的是包含有符号距离场的八叉树(非线性/完整)。八叉树经
有谁知道为什么选择通过 LinkedList 而不是另一个 Hashmap 来实现 HashMap 的存储桶。如果桶本身变成了 HashMap,那么 contains 或 get 的时间复杂度似乎是
我想创建一个具有嵌套结构的 HashMap,就像这个复杂的示例: { type: boy name: Phineas father: type: man
这个问题在这里已经有了答案: How do I create a global, mutable singleton? (7 个答案) 关闭 7 年前。 我想要一个可扩展的字典,将 Object 与
HashMap> hm = new HashMap>(); hm.put("Title1","Key1"); for(int i=0;i hm1 = new H
我必须修改当前代码以适应 Spring MVC。我有 HashMap hashmap = new HashMap(); request.setAttribute("dslrErrors", hashm
我正在尝试进行一些错误捕获。 错误应该检查数组的长度是否小于 2,并检查 HashMap 是否包含用户输入的键。 捕获的错误必须仅使用 if 语句,并且必须使用 .length() 方法,并且必须使用
在 stackoverflow 上提出另一个问题后,(Java- Why this program not throwing concurrent Modification exception)我开始
我有两个类,想使用 org.dozer.Mapper( http://dozer.sourceforge.net/ ) 将 Female 对象的属性映射到 Male 对象。 第一类是: public
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
是否有任何方法可以检查 HashMap 是否包含一组特定的键(这些键是在数组中给出的)。当我尝试类似下面的代码时,它返回 false。 map.containsKey(arrayOf("2018-01
跟进我的问题:How To Access hash maps key when the key is an object 我想尝试这样的事情:webSearchHash.put(xfile.getPa
我有一个可扩展的 ListView ,对于每个 child ,我需要有 4 个“额外”或字符串或其他名称来调用它:- 子标题- 描述- 链接1- 链接2 跟着教程,创建 ListView 、不同的 p
我想确保这是正确的,因为如果不正确,它可能会破坏我的应用程序。 我有这个: private static HashMap> balance = new HashMap<>(); 如果我得到这样的值:
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我想做以下事情: 为某个键查找Vec,并将其存储以备后用。 如果它不存在,则为键创建一个空的 Vec,但仍将其保存在变量中。 如何有效地做到这一点?自然地,我认为我可以使用 match: use st
我是一名优秀的程序员,十分优秀!