This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

pulling from the Set collection(entrySet)  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone:
I'm having trouble pulling a value out of my Set collection. Instead of the value I get a reference like: java146.util.Counter@cfb549
What I'm looking for is a numeric value not the reference. I tried type casting:
pw.println(me.getKey()+":"+"\t"+((Counter)me.getValue())); however I still got the reference as the value.
Any suggestions?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the container classes only hold Objects. They cannot directly hold built-in types like int or double. To store these native types, you must use the wrapper classes like Integer and Double. So to get teh value that is stored, you have to cast to the appropriate container class and then use the approperiate member function to get the value. Check the API docs for more information about the wrapper classes.
HTH
Layne
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the me object is of type Map.Entry.
so me.getValue() is an Object that must be cast to a Counter:
(Counter)(me.getValue())
Then you want to use the toString() or some getValue() method to get at the required attribute of Counter: ((Counter)(me.getValue())).toString() or ((Counter)(me.getValue())).getValue()
By the way have I seen this Counter class somewhere?
 
Chad Haselwander
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Layne and Barry:
I figured it out! I wasn't calling the getOccurance method in my Counter object. (Duh!)
So the code i had was correct only missing one key part.(the method call)
now my project is complete!
Thanks again,
Chad
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you want to use the toString() or some getValue() method to get at the required attribute of Counter:
In fact, toString() was being called implicitly by the original code:
pw.println(me.getKey()+":"+"\t"+((Counter)me.getValue()));
String concatenation (+) on an Object automatically invokes toString() if the Object is not already a String. So you may find it useful to add a toString() method to your Counter class which gives you the desired output:

Now anytime you include a Counter object in a print statement, it will convert to a meaningful String rather than the ugly serial-number-type thing which you inherit by default from the Object class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!