• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

TreeMap

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic