0

I created a Hashmap that stores a stock symbol as the key and ETFs associated with the stock symbol as values. the only thing I'm trying to figure out now is how to print what was stored in the HashMap. Ive tried various ways that either give me gibberish or a NullPointException. Below is my code and the various ways I tried printing the HashMap:

public static void main(String[] args){

        Map<TickerSymbol, List<ETF>> exampleMap = new HashMap<>();

        // create list one and store values
        List<ETF> setOne = new ArrayList<>();
        setOne.add(new ETF("Number 1", .2, .3, .4, .5, .6));
        setOne.add(new ETF("Number 2", .241, .312, .4312, .5423, .642));
        setOne.add(new ETF("Number 3", .21, .31, .41, .51, .61));    

        // create list two and store values
        List<ETF> setTwo = new ArrayList<>();
        setTwo.add(new ETF("Number 4", .3, .4, .5, .6, .8));
        setTwo.add(new ETF("Number 5", .3524, .442, .542, .665, .80));
        setTwo.add(new ETF("Number 6", .23, .32, .43, .76, .89));

        // create list three and store values
        List<ETF> setThree = new ArrayList<>();
        setThree.add(new ETF("Number 7", .37, .47, .57, .68, .89));
        setThree.add(new ETF("Number 8", .38, .48, .58, .68, .89));
        setThree.add(new ETF("Number 9", .39, .49, .59, .68, .89));

        // put values into map
        exampleMap.put(new TickerSymbol("stockA"), setOne);
        exampleMap.put(new TickerSymbol("stockB"), setTwo);
        exampleMap.put(new TickerSymbol("stockC"), setThree);    


        //gibberish prints out with this one
        for (Map.Entry<TickerSymbol, List<ETF>> entry : exampleMap.entrySet()) {
            TickerSymbol key = entry.getKey();
            List<ETF> values = entry.getValue();
            System.out.println("Key = " + key);
            System.out.println("Values = " + values + "n");
        }


        //gibberish prints out with this one
        for (Map.Entry<TickerSymbol, List<ETF>> entry : exampleMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue().toString());
        }


        //gibberish prints out with this one
        exampleMap.forEach((key, value) -> System.out.println(key + ":" + value));
        System.out.println(exampleMap.keySet().toString());

        //I get a null pointer exception with this one
        Iterator iterator = exampleMap.keySet().iterator();

        while (iterator.hasNext()) {
            String key = iterator.next().toString();
            String value = exampleMap.get(key).toString();

            System.out.println(key + " " + value);
        }

    }

Really hoping someone can help me here... I'm all out of ideas.

azro
  • 53,056
  • 7
  • 34
  • 70
Stephan
  • 19
  • 1
  • 9

1 Answers1

0

The gibberish you are seeing is the result of toString() from an ArrayList (implemented in AbstractCollection). It calls toString() on each stored element. In your case, these elements are instances of ETF. If you don't overwrite toString() in ETF, it inherits the implementation from Object, which is:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

So add a toString() method in ETF, like

@Override
public String toString() {
    return number + " (" + floats + ")";
}
steffen
  • 16,138
  • 4
  • 42
  • 81