Java 如何从列表中删除键/值对<;地图<;字符串,对象>&燃气轮机;
我有一个Java 如何从列表中删除键/值对<;地图<;字符串,对象>&燃气轮机;,java,dictionary,Java,Dictionary,我有一个列表,如下所示 [{a=1, b=Kanji, c=null, d=Alphabet}] 我已经知道“c”键有时具有值=null,因此如果是这种情况,我想从列表中删除“c”键 所以我想要的结果是,如果“c”键的值为=“null”或为空 [{a=1,b=Kanji,d=Alphabet}] 所以我试过了 List<Map<String, Object>> list = ....(procedure to get the [{a=1, b=Kanji, c=nul
列表
,如下所示
[{a=1, b=Kanji, c=null, d=Alphabet}]
我已经知道“c”键有时具有值=null
,因此如果是这种情况,我想从列表中删除“c”键
所以我想要的结果是,如果“c”键的值为=“null”或为空
[{a=1,b=Kanji,d=Alphabet}]
所以我试过了
List<Map<String, Object>> list = ....(procedure to get the [{a=1, b=Kanji, c=null, d=Alphabet}] from sql)
for (Map<String, Object> child : list) {
Object value = child.get("c");
if(value == null || value == "") {
list.remove("c");
}
}
如何从列表中删除值“c”
我想得到的结果是List
您需要从子映射中删除元素(child.remove(“c”)
),而不是List
您需要从子映射中删除元素(child.remove(“c”)
),而不是从列表中删除 上面的代码尝试从列表中删除密钥,但它应该尝试从映射中删除密钥 上面的代码尝试从列表中删除密钥,但它应该尝试从映射中删除密钥 需要更改..
从映射中删除,而不是从列表中删除
迭代键
以查找值为null的map元素
试试这个..
for(Map<String, Object> child : List){
for(String key : child.keySet()){
if(child.get(key)==null)
child.remove(key);
}
}
for(映射子项:列表){
for(字符串键:child.keySet()){
if(child.get(key)==null)
移除(键);
}
}
需要更改..
从映射中删除,而不是从列表中删除
迭代键
以查找值为null的map元素
试试这个..
for(Map<String, Object> child : List){
for(String key : child.keySet()){
if(child.get(key)==null)
child.remove(key);
}
}
for(映射子项:列表){
for(字符串键:child.keySet()){
if(child.get(key)==null)
移除(键);
}
}
Map abc=newhashmap();
abc.put(“number”、“hello”);
abc.put(“value”,“there”);
List mylist=new ArrayList();
mylist.add(abc);
System.out.println(mylist);
对于(int i=0;ikey==“number”);
}
System.out.println(mylist)
------------------------------输出------------------------
[{number=hello,value=there}]
[{value=there}]
Map abc=newhashmap();
abc.put(“number”、“hello”);
abc.put(“value”,“there”);
List mylist=new ArrayList();
mylist.add(abc);
System.out.println(mylist);
对于(int i=0;ikey==“number”);
}
System.out.println(mylist)
------------------------------输出------------------------
[{number=hello,value=there}]
[{value=there}]
地图列表太傻了。只需使用地图,您就可以在子项中找到c
,而不是列表中找到。因此,您应该执行child.remove(“c”)
。此外,请考虑CAMELACK变量名,或者至少不使用与类名冲突的变量名。@ ScRyWabBeFy Spring JDCBC模板还返回QueReFistLin列表,除了试图从列表中移除而不是映射,还存在一个与空字符串比较的bug。您应该使用value.equals(“”
。感谢大家的快速响应和详细信息。地图列表太傻了。只需使用地图,您就可以在子项中找到c
,而不是列表中找到。因此,您应该执行child.remove(“c”)
。此外,请考虑CAMELACK变量名,或者至少不使用与类名冲突的变量名。@ ScRyWabBeFy Spring JDCBC模板还返回QueReFistLin列表,除了试图从列表中移除而不是映射,还存在一个与空字符串比较的bug。您应该使用value.equals(“”
。感谢大家的快速响应和详细信息。很好。删除任何具有空值的键,而不仅仅是“c”。@dave,但还不够好。如果我们在插入时重写put函数并进行验证,可能会更好。很好。删除任何具有空值的键,而不仅仅是“c”。@dave,但还不够好。如果我们在插入时覆盖put函数并进行验证,可能会更好。谢谢您的快速回答!我试试看。谢谢你的迅速回答!我试试看。
Map<String, String> abc = new HashMap<String, String>();
abc.put("number", "hello");
abc.put("value", "there");
List<Map<String, String>> mylist = new ArrayList<>();
mylist.add(abc);
System.out.println(mylist);
for (int i = 0; i < mylist.size(); i++) {
mylist.get(i).keySet().removeIf(key -> key == "number");
}
System.out.println(mylist)
------------------------------output------------------------
[{number=hello, value=there}]
[{value=there}]