0

I have a HashMap as follows:

HashMap<Integer, List<MyClassObj>> hmap = new HashMap<Integer, List<MyClassObj>>();

In a loop, I append to a List and then I put in a key, value pair where List is the value into my hmap object

How can I extract the values in the list for each key?

for (Integer k: hmap.keySet()) {
    String key = k.toString();
    String value = hmap.get(k).toString();
    System.out.println(key + " " + value);
}

This outputs :

43115 [mypackage@176f3f8, mypackage@176f3f8, mypackage@176f3f8]
40715 [mypackage@176f3f8, mypackage@176f3f8, mypackage@176f3f8]

Why is this returning my package and a hashkey?

How can I see the contents of each list?

arsenal88
  • 1,040
  • 2
  • 15
  • 32

3 Answers3

1

You need to override toString() method in your MyClassObj class.

For example, assuming that your object has name and age

@Override
public String toString() { 
    return "Name: '" + this.name + "', Age: '" + this.age + "'";
} 

Then, you need to go through the elements of the List and print them out (using the toString() method). For example,

for(int i = 0; i < youObjectInstance.size(); i++) {
    System.out.println(youObjectInstance.get(i).toString());
}

As a general advice, you should override toString() method for almost every Object you create so that you can print it in a convenient format, depending on the use-case.

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

To answer your question: Java is printing what it is printing because you're trying to print a List Object. The default implementation for toString show a representation of the storage address for the object.

I assume you want to do something with every object in the list for every key. In this case you can use an entryset in Java. In your case something like this:

for (Map.Entry<Integer, List<myObject>> entry : hmap.entrySet()) {
    for (String s : entry.getValue()) { 
        //do something with the object     
    }
}

The outer loops gives you all integer -> list pairs. the inner loops gives you access to every object you added to that list earlier.

Markus Mauksch
  • 397
  • 1
  • 9
0

Here is code snippet for better understanding. As it was mentioned above you should override toString() method of MyClassObj, Class Person will be used as an example of MyClassObj.

    HashMap<Integer, List<Person>> hmap = new HashMap<>();
    List<Person> personList1 = Arrays.asList(new Person("John", 31, "green", "football"),
            new Person("Thomas", 25, "blue", "basketball"));
    List<Person> personList2 = Arrays.asList(new Person("Susan", 28, "black", "surfing"));
    hmap.put(0, personList1);
    hmap.put(1, personList2);

    for (Integer k: hmap.keySet()) {
        String key = k.toString();
        String value = hmap.get(k).toString();
        System.out.println(key + " " + value);
    }

Where Person object:

public class Person {
    private String name;
    private Integer age;
    private String eyecolor;
    private String hobby;

    public Person(String name, Integer age, String eyecolor, String hobby) {
        this.name = name;
        this.age = age;
        this.eyecolor = eyecolor;
        this.hobby = hobby;
    }
    //... getters, setters

    @Override
    public String toString() {
        return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]").add("name='" + name + "'").add("age=" + age)
                .add("eyecolor='" + eyecolor + "'").add("hobby='" + hobby + "'").toString();
    }
}
samabcde
  • 6,988
  • 2
  • 25
  • 41