Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Map inside a Map... How to load and store values from files using serialisation?

package com.ledgerProject.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mridul on 08-09-2015.
 */
public class User {
    private String mName;
    private Map<String, Map<String, List<Investment>>> mInvestmentMap;

    public User(String name) {
        this.mName = name;
        mInvestmentMap = new HashMap<>();
    }

    public String getName() {
        return mName;
    }

    public Map<String, Map<String, List<Investment>>> getInvestmentMap() {
        return mInvestmentMap;
    }

    public void insertToMap(String a, String b, Investment inv) {

        if (!mInvestmentMap.containsKey(a)) {
            List<Investment> list = new ArrayList<>();
            list.add(inv);
            Map<String, List<Investment>> innerMap = new HashMap<>();
            innerMap.put(b, list);
            mInvestmentMap.put(a, innerMap);
        } else if (!mInvestmentMap.get(a).containsKey(b)) {
            List<Investment> list = new ArrayList<>();
            list.add(inv);
            mInvestmentMap.get(a).put(b, list);
        } else {
            mInvestmentMap.get(a).get(b).add(inv);
        }

    }

    @Override
    public String toString() {
        return "User{" +
                "mName='" + mName + '\'' +
                ", mInvestmentMap=" + mInvestmentMap +
                '}';
    }

}

[Edited] So I think i wrote a function for insertion of value to my list inside the map inside a map. But now i want to load and store these values in files. Please need a little help in that.

Thanks a lot.

1 Answer

    public void exportTo() {
        try (
                FileOutputStream fos = new FileOutputStream("/saves/" + mName + ".ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {

            for (Map.Entry entry1 : mInvestmentMap.entrySet()) {
                Map<String,List<String>> innerMap = (Map<String, List<String>>) entry1.getValue();
                for (Map.Entry entry2 : )

            }
        }

I am doing something like this, but i could think what to write for the second for loop...Please help guys...Someone