java哈希映射。put()重写已激活的值,即使键值不同
这个问题早些时候已经提出过。但是这些回复都没有解决我的问题。谁来帮忙
我有两个哈希图,
final HashMap<String,String> dataList = new HashMap<String,String>();
final HashMap<String,HashMap<String,String>> full = new HashMap<String,HashMap<String,String>>();
这是我的密码
int i=0;
for(DataSnapshot snap : dataSnapshot.getChildren()) {
i++;
String id = "Name "+String.valueOf(i);
strArray.add(snap.child("email").getValue().toString());
String name = snap.child("FirstName").getValue().toString() + " " + snap.child("LastName").getValue();
dataList.put("Name", name);
dataList.put("Email",snap.child("email").getValue().toString());
dataList.put("Mobile", snap.child("Mobile").getValue().toString());
dataList.put("Birthdate", snap.child("birthdate").getValue().toString());
System.out.println("Datalist " + dataList);
full.put(id, dataList);}
我希望有,{Name 1 = {Birthdate=2012/02/15, Email=gwcsathsara@gmail.com, Mobile=, Name= },Name 2 = {Birthdate=2012/02/15, Email=xc, Mobile=8524, Name=Sdcc Kkn}}
但它凌驾于激励值之上。这是我得到的结果,
{Name 1={Birthdate=2012/02/15, Email=xc, Mobile=8524, Name=Sdcc Kkn}, Name 2={Birthdate=2012/02/15, Email=xc, Mobile=8524, Name=Sdcc Kkn}}
谁来帮我解决这个问题。如果需要更多信息,请发表评论。 多谢各位
# 1 楼答案
您需要在这一行之前添加这一行:
HashMap<String,String> dataList = new HashMap<String,String>();
发生的情况是,您正在使用
Map
,因此每次都覆盖密钥,最终结果是您处理的最后一个子项