开发者社区> 问答> 正文

如何添加项目以列出内部哈希图?

HashMap<String, List<Person.Personal>> hashMap = new HashMap();

var attachment = new Person.Personal(name, surname, birthDate);

我需要添加带有来自另一张地图的钥匙的物品。

然后我需要下面的代码;

if(hashMap.containsKey(courseGroup.getKey().get(0)))
{
    hashMap.put(courseGroup.getKey().get(0), attachment);
}
else
{
    hashMap.put(courseGroup.getKey().get(0), new Arraylist<Person.Personal> (attachment));
}

如果哈希图具有键,则添加其值列表“ attachment”,如果没有,则创建一个列表,然后添加“ attachment”,这是我需要的。

展开
收起
垚tutu 2019-11-28 19:30:58 1023 0
1 条回答
写回答
取消 提交回答
  • #include

    由于arraylist已经存在,所以不必重新放置它,而要更新arraylist。

    例如:

    if(hashMap.containsKey(courseGroup.getKey().get(0)))
    {
        // adding values directly to the arraylist.
        hashMap.get(courseGroup.getKey().get(0)).add(attachment);
    }
    else
    {
    
        // create a new array and put it there.
        hashMap.put(courseGroup.getKey().get(0), new Arraylist<Person.Personal> (attachment));
    }
    
    2019-11-28 19:31:10
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
数据+算法定义新世界 立即下载
LOCKet:安全源于独立 立即下载
安全,源于独立 立即下载