jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes pulling from the Set collection(entrySet) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pulling from the Set collection(entrySet)" Watch "pulling from the Set collection(entrySet)" New topic

pulling from the Set collection(entrySet)

Chad Haselwander

Joined: Dec 19, 2002
Posts: 5
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?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'm assuming the me object is of type Map.Entry.
so me.getValue() is an Object that must be cast to a Counter:
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?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chad Haselwander

Joined: Dec 19, 2002
Posts: 5
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,
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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:
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.

"I'm not back." - Bill Harding, Twister
It is sorta covered in the JavaRanch Style Guide.
subject: pulling from the Set collection(entrySet)
It's not a secret anymore!