I'm learning how to use hashmaps and I'm having some issues here. I created two classes called Zoo and Animals, and I joined both classes using an Hashmap:
private HashMap<Zoo, Animals> zoo;
I built a method to add data into the hashmap:
public void setAnimal(Zoo zoo, Animals animal ){
zoo.put(zoo, animal);
}
And I'm inserted the follwing values into the hashmap:
Main main = new Main();
Zoo zoo1 = new Zoo("France", 1);
Zoo zoo2 = new Zoo("Spain", 2);
Animals gorilla = new Animals(2015, "Gorilla");
Animals frog = new Animals(2018, "Frog");
Animals zebra = new Sedan(2016, "zebra");
Animals lion= new Sedan(2016, "lion");
main.setAnimal(zoo1, gorilla);
main.setAnimal(zoo2, frog);
main.setAnimal(zoo2, lion);
main.setAnimal(zoo2, zebra);
And I built a method to print the Zoos and the respective animals in there:
String text = "";
for(Map.Entry<Zoo, Animals> entry : zoo.entrySet()) {
Zoo key = entry.getKey();
Animals value = entry.getValue();
text += key.toString() + "\n";
text += value.toString() + "\n";
}
return text;
But I just get this:
France 1
gorilla
Spain 2
Zebra
Instead of
France 1
Gorilla
Spain 2
frog
lion
zebra
How can I get the full animals list?