Reputation: 437
I want to filter an array list containing hash maps using the stream API.
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);
}
The above method gives the resultslist
. Now I want to filter this list by some condition and collect a new array list of hash maps.
For example, I want to select hash maps that have a "null" value and string have "parent_item_id".
I tried the following.
ArrayList<HashMap<String, Object>> reducedList = resultslist .stream()
.filter((HashMap<String, Object> hasmap) -> hasmap.entrySet().stream().anyMatch(e -> e.getKey().equals("parent_item_id") && e.getValue().equals(0)))
.collect(Collectors.toList());
Finally, it's working
I used this. Thankyou for all
List<HashMap<String, Object>> reducedList = resultslist.stream()
.filter(hashmap ->((hashmap.get("parent_item_id")) == null ))
.collect(Collectors.toList());
Upvotes: 1
Views: 9906
Reputation: 4140
Similar to other but you can use Predicate
.
Predicate<HashMap<String, Object>> containsKey = hashmap -> hashmap.containsKey("parent_item_id");
Predicate<HashMap<String, Object>> isNull = hashmap -> hashmap.get("parent_item_id") == null;
List<Map<String, Object>> reducedList = access_collections.stream()
.filter(containsKey.and(isNull))
.collect(Collectors.toList());
Or you can use this predicat during while (rs.next()) {
and:
if (containsKey.and(isNull).test(returndatas)) {
resultslist.add(returndatas);
}
Below is only generalization that may blur the first solution (as exercise that not added anything to solution):
Change Predicate
to BiPredicate
and have more generic solution.
BiPredicate<String, HashMap<String, Object>> containsKey =
(label, hashmap) -> hashmap.containsKey(label);
BiPredicate<String, HashMap<String, Object>> isNull =
(label, hashmap) -> hashmap.get(label) == null;
Function<String, Predicate<HashMap<String, Object>>> checkFor =
(label) -> (hashmap) -> containsKey.and(isNull).test(label, hashmap);
Predicate<HashMap<String, Object>> checkParentItemId = checkFor.apply("parent_item_id");
List<Map<String, Object>> reducedList = access_collections.stream()
.filter(checkParentItemId)
.collect(Collectors.toList());
and
if (checkFor.apply("parent_item_id").test(returndatas)) {
resultslist.add(returndatas);
}
Upvotes: 0
Reputation: 7289
Well the question is not clear, hence another solution for filtering maps with 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());
Upvotes: 0
Reputation: 223
It you are just trying to reduce your list of hashmaps based on the presence of a value being == to 0 this will do the job. However, mapping with String, Object makes it seem like you really should be creating an object instead of a map.
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());
Based on your snippet above I am assuming parent_item_id is an integer. Another reason to switch whatever is in your map to an actual object instead of trying to model it in a map where we have to guess which type each of your values are.
Upvotes: 0