Abby
Abby

Reputation: 7

Map inside a map in Firestore

enter image description here

I have a code in getting the value of a map named PUJ.

Map<String, Object> map = document.getData();
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getKey().equals("PUJ")) {
        Log.i(TAG, entry.getValue().toString());
    }
}

Is it possible to get only the values: ABC123, YAG916?

Upvotes: 0

Views: 72

Answers (2)

Ravi Sharma
Ravi Sharma

Reputation: 170

   Map<String, Object> map = document.getData();
    for (String key : map.keySet()) {
        if ("PUJ".equals(key) && map.get(key) instanceof Map) {
            Map<String, Object> pujMap = (Map<String, Object>) map.get(key);
            // 
        }
    }

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317497

The nested maps are also Map<String, Object>, just like the document data. You can cast it if you want to assume the type:

Map<String, Object> map = document.getData();
Map<String, Object> puj = (Map<String, Object>) map.get("PUJ");
Set<String> keys = puj.keysSet();
// keys is now a Set that contains both "ABC123" and "YAG916"

But you should also check it with instanceof to be sure.

Upvotes: 1

Related Questions