• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
I am displeased. You are no longer allowed to read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic