• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeMap  RSS feed

 
umut uzumcu
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, here is my code

RecordClass rc has data No,name, color,owner
When i put the data into the TreeMap key is saved ok but the value(rc) not.

but if i just wanna save the No and the name in the TreeMap, thats working fine. eg. vdb.put(rc.getNo(),rc.getName());

Bur i need to save 1 key in the map and that key has to hold 3 values


What can be the problem?

public class DB
{
Map vdb=new TreeMap();
public void store(RecordClass rc)
{

vdb.put(rc.getNo(),rc);

System.out.println(vdb);
}
}
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"When i put the data into the TreeMap key is saved ok but the value(rc) not."

Can you explain why you think that?

Your code should work fine. But note that when you do something like this:

System.out.println(vdb);

You will probably get output that looks something like this:

{umut=RecordClass@3e25a5, uzumcu=RecordClass@19821f}

You see those strange "RecordClass@3e25a5" things because TreeMap.toString() calls RecordClass.toString() to create a string representation of your RecordClass objects, and you don't have a toString() method in your class RecordClass.

Try adding a toString() method to RecordClass that returns a String with the contents of the object formatted in a way you like.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like what you want to do is have duplicate keys. You can't do that in a Map.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!