Reputation: 612
My Map has below key = value pair, now how to get the GCO value from the set {} ?
MAP : {containerIdentifier={ID=null, dto=null, GCO=123, version=1}, containerEditable=true}
code :
for (Entry<String, Object> entry : map.entrySet())
System.out.println("Key = " + entry.getKey() +", Value = " + entry.getValue());
Output :
Key = containerIdentifier, Value = {ID =null, dto=null, GCO=123, version=1}
Key = containerEditable, Value = true
how to get the only GCO 123 from value ?
Upvotes: 2
Views: 1054
Reputation: 1058
You could use java steam api.
map.values().stream()
.flatMap(innerMaps -> innerMaps.entrySet().stream())
.filter(entry-> "GCO".equals(entry.getKey()))
.map(Map.Entry::getValue)
.findAny().orElse("not found")
Upvotes: 2
Reputation: 393856
You have to check when the value of the original Map
is also a Map
. Then you can cast it to a Map
and lookup the value of a specific key in that inner Map
:
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
Map innermap = (Map) entry.getValue();
System.out.println(innermap.get("GCO"));
}
}
This is assuming the value of the original Map
is not always a Map
(and considering the second entry in your sample Map
, whose value is a Boolean
, that assumption is correct).
Upvotes: 2