Java 如何为hashmap列表使用流过滤器?ArrayList<;HashMap<;字符串,对象>&燃气轮机;
我想使用流API过滤包含哈希映射的数组列表Java 如何为hashmap列表使用流过滤器?ArrayList<;HashMap<;字符串,对象>&燃气轮机;,java,java-8,java-stream,Java,Java 8,Java Stream,我想使用流API过滤包含哈希映射的数组列表 ArrayList<HashMap<String, Object>> resultslist = new ArrayList<HashMap<String, Object>>(); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { HashMap<String, Object> returndatas = ne
ArrayList<HashMap<String, Object>> resultslist = new ArrayList<HashMap<String, Object>>();
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
HashMap<String, Object> returndatas = new HashMap<String, Object>();
int colcnt = rs.getMetaData().getColumnCount();
for (int i = 1; i <= colcnt; i++) {
if (rs.getObject(i) instanceof BigInteger) {
returndatas.put(rs.getMetaData().getColumnName(i), rs.getInt(i));
} else {
returndatas.put(rs.getMetaData().getColumnName(i), rs.getObject(i));
}
}
resultslist.add(returndatas);
}
最后,它开始工作了
我用过这个。谢谢大家
List<HashMap<String, Object>> reducedList = resultslist.stream()
.filter(hashmap ->((hashmap.get("parent_item_id")) == null ))
.collect(Collectors.toList());
List reducedList=resultslist.stream()
.filter(hashmap->((hashmap.get(“父项\u项\u id”))==null))
.collect(Collectors.toList());
如果您只是试图根据==值的存在将哈希映射列表减少到0,那么这将完成任务。但是,使用字符串、对象进行映射会使您看起来好像真的应该创建一个对象而不是贴图
List<Map<String, Object>> reducedList = access_collections.stream()
.filter(hashmap -> hashmap.containsKey("parent_item_id"))
.filter(hashmap -> (Integer) hashmap.get("parent_item_id") == 0)
.collect(Collectors.toList());
List reducedList=access\u collections.stream()
.filter(hashmap->hashmap.containsKey(“父项\u项\u id”))
.filter(hashmap->(整数)hashmap.get(“父项\u项\u id”)==0)
.collect(Collectors.toList());
根据上面的代码片段,我假设父项id为整数。另一个原因是将地图中的任何内容切换到实际对象,而不是尝试在地图中建模,在地图中我们必须猜测每个值的类型 如果您只是试图根据值==的存在将哈希映射列表减少到0,那么这将完成任务。但是,使用字符串、对象进行映射会使您看起来好像真的应该创建一个对象而不是贴图
List<Map<String, Object>> reducedList = access_collections.stream()
.filter(hashmap -> hashmap.containsKey("parent_item_id"))
.filter(hashmap -> (Integer) hashmap.get("parent_item_id") == 0)
.collect(Collectors.toList());
List reducedList=access\u collections.stream()
.filter(hashmap->hashmap.containsKey(“父项\u项\u id”))
.filter(hashmap->(整数)hashmap.get(“父项\u项\u id”)==0)
.collect(Collectors.toList());
根据上面的代码片段,我假设父项id为整数。另一个原因是将地图中的任何内容切换到实际对象,而不是尝试在地图中建模,在地图中我们必须猜测每个值的类型 问题还不清楚,因此另一种过滤带有
null的映射的解决方案是:
// If you are sure the key "parent_item_id" exists:
List<HashMap<String, Object>> reducedList =
access_collections.stream()
.filter(m -> m.get("parent_item_id") == null)
.collect(toList());
// If you are not sure then:
List<HashMap<String, Object>> reducedList =
access_collections.stream()
.filter(m -> m.containsKey("parent_item_id") && m.get("parent_item_id") == null)
.collect(toList());
//如果您确定键“parent\u item\u id”存在:
列表简化列表=
访问\u collections.stream()
.filter(m->m.get(“父项\u项\u id”)==null)
.collect(toList());
//如果您不确定,则:
列表简化列表=
访问\u collections.stream()
.filter(m->m.containsKey(“父项id”)和&m.get(“父项id”)==null)
.collect(toList());
这个问题并不清楚,因此使用空值过滤贴图的另一个解决方案是:
// If you are sure the key "parent_item_id" exists:
List<HashMap<String, Object>> reducedList =
access_collections.stream()
.filter(m -> m.get("parent_item_id") == null)
.collect(toList());
// If you are not sure then:
List<HashMap<String, Object>> reducedList =
access_collections.stream()
.filter(m -> m.containsKey("parent_item_id") && m.get("parent_item_id") == null)
.collect(toList());
//如果您确定键“parent\u item\u id”存在:
列表简化列表=
访问\u collections.stream()
.filter(m->m.get(“父项\u项\u id”)==null)
.collect(toList());
//如果您不确定,则:
列表简化列表=
访问\u collections.stream()
.filter(m->m.containsKey(“父项id”)和&m.get(“父项id”)==null)
.collect(toList());
与其他类似,但您可以使用谓词
Predicate containsKey=hashmap->hashmap.containsKey(“父项id”);
谓词isNull=hashmap->hashmap.get(“父项\u项\u id”)==null;
List reducedList=access\u collections.stream()
.filter(containsKey.and(isNull))
.collect(Collectors.toList());
或者您可以在while(rs.next()){
期间使用此谓词,并且:
if(containsKey.and(isNull).test(returndatas)){
结果列表。添加(返回数据);
}
更新
下面是可能模糊第一个解决方案的唯一概括(作为没有向解决方案添加任何内容的练习):
将谓词
更改为BiPredicate
,并使用更通用的解决方案
BiPredicate containsKey=
(标签,hashmap)->hashmap.containsKey(标签);
BiPredicate isNull=
(label,hashmap)->hashmap.get(label)==null;
函数检查=
(label)->(hashmap)->containsKey.and(isNull.test)(label,hashmap);
谓词checkParentItemId=checkFor.apply(“父项id”);
List reducedList=access\u collections.stream()
.filter(checkParentItemId)
.collect(Collectors.toList());
及
if(检查.apply(“父项id”).test(返回数据)){
结果列表。添加(返回数据);
}
与其他类似,但您可以使用谓词
Predicate containsKey=hashmap->hashmap.containsKey(“父项id”);
谓词isNull=hashmap->hashmap.get(“父项\u项\u id”)==null;
List reducedList=access\u collections.stream()
.filter(containsKey.and(isNull))
.collect(Collectors.toList());
或者您可以在while(rs.next()){
期间使用此谓词,并且:
if(containsKey.and(isNull).test(returndatas)){
结果列表。添加(返回数据);
}
更新
下面是可能模糊第一个解决方案的唯一概括(作为没有向解决方案添加任何内容的练习):
将谓词
更改为BiPredicate
,并使用更通用的解决方案
BiPredicate containsKey=
(标签,hashmap)->hashmap.containsKey(标签);
BiPredicate isNull=
(label,hashmap)->hashmap.get(label)==null;
函数检查=
(label)->(hashmap)->containsKey.and(isNull.test)(label,hashmap);
谓词checkParentItemId=checkFor.apply(“父项id”);
List reducedList=access\u collections.stream()
.filter(checkParentItemId)
.collect(Collectors.toList());
及
if(检查.apply(“父项id”).test(返回数据)){
结果列表。添加(返回数据);
}
我想选择hashmap对象是否有空值如果您只对一个键感兴趣“parent\u item\u id”
,不要流式处理整个入口集。如果您想测试hashmap中是否存在值为空的值,请写入