Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get HashMap from HashMap

I need to get HashMap which is already inside of HashMap as a value.

HashMap<String, HashMap<String, Product>> myOrders = new HashMap<>();
myOrders = firebaseMethods.getOrders(FirebaseAuth.getInstance().getCurrentUser().getUid(),snapshot);
List<String> Keys = new ArrayList<>(myOrders.keySet());
HashMap<String,Product> Values = myOrders.values();

I’ve tried already .values() method but it didn’t work, any ideas how I can achieve this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Calling .values() will return a collection of ‘values’ (in your case, a collection of HashMaps). You can use something like:

List<String> keys = new ArrayList<>(myOrders.keySet());

for(String key: keys) {
    HashMap<String, Product> productMap = myOrders.get(key); // This will return the HashMap for the 'key'
    ... // processing logic goes here

Or better still (as suggested in the comments):

for(Entry<String, Map<String, Product>> entry: map.entrySet()) {
    Map<String, Product> productMap = entry.getValue();
    // processing logic here ... 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading