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

TreeMap & Iterators

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm writing a program using TreeMap and iterator, to store the vehicle details and get it printed.(key/value).

The following method is used in one class.
public Iterator getIterator()
{
Collection vals = vehicleDetails.values();
Iterator iter = vehicleDetails.entrySet().iterator();
return iter;
}

I have to print the key/value in a test program.
code:
vdb.getIterator();
Iterator iter = vdb.getIterator();

while (iter.hasNext()){
Map.Entry entry = (Map.Entry) iter.next() ;
System.out.println(entry.getKey() + "-->" + entry.getValue());

But this is not printing the values.
How can I get the return value and store it in a variable.

Can anyone help me please.
thanks.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alph! welcome to javaranch.

I tried your code and it works fine for me. I modified it a bit and here is the code



I think you are placing custom objects as values or keys. In that case to be able to see the value of that object, you will have to override toString method in your custom class. Then only you will get meaningful result...
[ November 18, 2008: Message edited by: Ankit Garg ]
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
Thank you very very much for your help.
It worked. I did use the toString() method like you said.

But one more thing please.
If I have more than 1 object for the values (eg: Make, Colour of the vehicle), will I be able to print these. Or only key & one value can be printed?

Thanks.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I have more than 1 object for the values (eg: Make, Colour of the vehicle), will I be able to print these.



First of all there can be only one value per key. I think you are talking about the object that you add in the Map has these values(Lets suppose the class is Car). Then if you want to display them, you have two choices. Either you override the toString method in that class as

public String toString()
{
return "Make:"+make+"\nColor:"+color+"vehicle:"+vehicle;
}

or you must extract elements from the Map.Entry object and display these values manually like

System.out.println(entry.getKey() + "-->");
Car car = (Car)entry.getValue();
System.out.println(car.getMake());
System.out.println(car.getColor());
System.out.println(car.getVehicle());
[ November 19, 2008: Message edited by: Ankit Garg ]
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ankit.

It worked fine.
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic