djdeejay djdeejay - 1 year ago 73
Java Question

java.util.treemap gives strange results

Intentionally left blank, bye bye

Answer Source

The problem likely has to do with compareTo in AnimalCard. Check that compareTo is doing what you expect it to do, and that two AnimalCard instances with different numbers are not considered equal based on this method (i.e. it does not return 0 in such cases). If they are, then that would explain your result, since TreeMap would simply update the pre-existing entry instead of adding a new one.

Also, if you're debugging, you might want to print the return value of put() (it could give you some insight as to what's going on).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download