I have a List of objects containing a Map. The object looks like this :
public class SwiftObject {
private Map<String, String> metadata;
...
}
I'm trying to filter my objects based on the keys and values stored in their Map.
My filter criteria are contained in another Map.
I'd like to filter and keep all the objects which Map has all key/value contained in the filter Map.
This is the code I wrote :
List<? extends SwiftObject> swiftObjects = documentStore.listAllObjects(container);
Map<String, String> searchMetadata = gssDocument.getMetadataMap();
List<SwiftObject> filteredSwiftObjects = swiftObjects.stream()
.filter(swiftObject ->
searchMetadata.entrySet().stream()
.allMatch(entry -> entry.getValue().equals(swiftObject.getMetadata().get(entry.getKey())))
)
.collect(Collectors.ToList());
Is it the right way to do this or can it be refactored/optimized?
The searchMetadata map does not contain null values
Use Map.equals(Map other)
to check if both maps contain the same mappings. If you just need to check if a map is a subset of another map, use map.entrySet().containsAll(other.entrySet())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With