【问题标题】:Add values to Hashmap based on some criteria根据某些条件向 Hashmap 添加值
【发布时间】:2017-11-20 14:07:40
【问题描述】:

以下代码检查 ArrayList 中是否存在 id,然后将其添加到地图中。

List<Map<Object, Object>> myListOfMaps = new ArrayList<Map<Object, Object>>();
ArrayList<String> al1 = new ArrayList<String>();
al1.add("92");
al1.add("94");  
al1.add("91");  
al1.add("98");
Map<Object,Object> map = new HashMap<Object,Object>();
ArrayList<String> result = new ArrayList<String>();               
String id = "95,98,94";
for(int i=0;i<al1.size();i++)
{
    if(id.contains(al1.get(i)))
    {
        result.add("true");
        map.put("Access", true);                
    }            
    else 
    {
        result.add("false");
        map.put("Access", false);               
    }
    myListOfMaps.add(map);               

}
  for (int i = 0 ; i < myListOfMaps.size() ; i++) {
    Map<Object, Object> myMap = myListOfMaps.get(i);          
    for (Entry<Object, Object> entrySet : myMap.entrySet()) {                                
        System.out.println("Key = " + entrySet.getKey() + " , Value = " + entrySet.getValue());
    }
}          
System.out.println(result);

输出

Key = Access , Value = true
Key = Access , Value = true
Key = Access , Value = true
Key = Access , Value = true
[false, true, false, true]

arraylist 结果很好,但 mapvalues 仅返回 true。请帮助我如何根据条件将 k,v 添加到 map,以便 mapresult 看起来像这样 [{Access=false},{Access=true},{Access=false },{Access=true}]。

【问题讨论】:

  • 见置顶答案第二条:stackoverflow.com/questions/19843506
  • 你只是放了一个键,并用不同的值覆盖了同一个键。 98 是您遍历的最后一个并且它在列表中,因此您在地图中插入键为“Access”,值为 true,因此每次都为 true

标签: java arraylist data-structures hashmap


【解决方案1】:

hashmap 是一种键值对数据结构。您存储项目的方式基于

话虽如此,如果您将键 Access 添加到具有值 false 的地图并打印它将显示的地图:Access: false

如果您再次添加 Access 与值 true 不会添加新键,而是要替换以前的值,因此它将打印 Access: true

因此,不要将键 Access 添加到地图中,而是添加 id。 map.put(al1.get(i), true);

【讨论】:

  • 虽然对“为什么”的解释是正确的(虽然不完整;为什么打印相同的entrySet 4 次?),但该解决方案不符合 OP 的输出要求。跨度>
【解决方案2】:

您必须每次在循环中声明一个新地图

//...
al1.add("98");
 //Map<Object,Object> map = new HashMap<Object,Object>();
ArrayList<String> result = new ArrayList<String>();               
String id = "95,98,94";
for(int i=0;i<al1.size();i++)
{
    Map<Object,Object> map = new HashMap<Object,Object>();
    if(id.contains(al1.get(i)))
    {
        result.add("true");
        map.put("Access", true);                
    }            
    else 
    {
        result.add("false");
        map.put("Access", false);               
    }
    myListOfMaps.add(map);               

}
//...

【讨论】:

  • 解决不了问题.. 每次都会覆盖值
【解决方案3】:

此代码将帮助您:

List<Map<String, Boolean>> myListOfMaps = new ArrayList<>();
List<String> al1 = new ArrayList<>();
al1.add("92");
al1.add("94");
al1.add("91");
al1.add("98");

String id = "95,98,94";

List<String> result = new ArrayList<>();
for (int i = 0; i < al1.size(); i++) {
    Map<String, Boolean> map = new HashMap<>();
    if (id.contains(al1.get(i))) {
        result.add("true");
        map.put("Access", true);
    } else {
        result.add("false");
        map.put("Access", false);
    }
    myListOfMaps.add(map);
}
myListOfMaps.forEach(System.out::println);
System.out.println(result);

【讨论】:

  • 如果你不每次都实例化一个新地图,你将使用相同的旧引用,最后一个值将是true,这就是为什么你会在任何地方看到 true
  • 我们真的需要在每次迭代中创建地图吗?
  • 对于这个结构,OP 需要一个带有这个 for 循环和这个键和这些值的映射(键值对),我说是的。不过我更喜欢另一种结构,但我不知道他/她需要什么。
  • Tamas Ambrus 能给我推荐一个结构吗
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 2012-10-11
  • 2023-03-30
  • 2021-12-02
  • 2021-11-03
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多