java.util.treemap gives strange results -


i have following method :

     public final navigableset<animalcard> getcowtradecardssorted() {     treemap<animalcard,animalcard> ssa = new treemap<animalcard,animalcard>();     iterator<e> = this.cowtradecards.iterator();     system.out.println("input is:"+this.cowtradecards.tostring());     while (i.hasnext()) {         e = i.next();         system.out.println("adding 2 ssa:"+a.tostring());          ssa.put((animalcard) ,(animalcard) a); //this.getcountofonecard(a));         system.out.println("ssa now"+ssa.tostring());      }     system.out.println("returnvalue is"+ ssa.descendingkeyset().tostring());     return ssa.descendingkeyset().descendingset(); } 

where expect following output returnvalue: [1000,500,160,10]

but [1000]

here output fom console:

   input is:[1000, 10, 160, 500]    adding 2 ssa:1000    ssa now{1000=1000}    adding 2 ssa:10    ssa now{1000=10}    adding 2 ssa:160    ssa now{1000=160}    adding 2 ssa:500    ssa now{1000=500}    returnvalue is[1000] 

what wrong? looks treemap binding value first key?

ps: this.cowtradecards arraylist doesn´t matter really

    public class animalcard extends card { ..........        // super has :public int getvalue() {   //        return this.value;   //       }       @override public int compareto(object o) {     // pufferelement vom typ animalcard     animalcard buffer;      // kontrolle, ob übergebenes objekt vom typ "animalcard" ist     if (o instanceof moneycard) {   <--------------- here error !!!!! should animalcard !         // wenn ja, dann typcasting vornehmen         buffer = (animalcard) o;          if (super.getvalue() < buffer.getvalue()) {             return -1;         }         if (super.getvalue() > buffer.getvalue()) {             return 1;         }         return 0;     } else {         // not "animalcard"      }     return 0; }  after fixed compareto:     input is:[1000, 10, 160, 500]     adding 2 ssa:1000     ssa now{1000=1000}     adding 2 ssa:10     ssa now{10=10, 1000=1000}     adding 2 ssa:160     ssa now{10=10, 160=160, 1000=1000}     adding 2 ssa:500     ssa now{10=10, 160=160, 500=500, 1000=1000}     returnvalue is[1000, 500, 160, 10] 

the problem has compareto in animalcard. check compareto doing expect do, , 2 animalcard instances different numbers not considered equal based on method (i.e. not return 0 in such cases). if are, explain result, since treemap update pre-existing entry instead of adding new one.

also, if you're debugging, might want print return value of put() (it give insight what's going on).


Comments

Popular posts from this blog

c++ - End of file on pipe magic during open -

basic authentication with http post params android -

data.table making a copy of table in R -