Treemap.getValue()

need help with this bit of code

how do i convert  map.getValue(); to an int

TreeMap map = new TreeMap();
int t = map.getValue();
HomerrSimpsonAsked:
Who is Participating?
 
lhankinsCommented:
What did you put in the map...?   A String or an Integer...?   This will determine how you go about retrieving it.   For example, you can put things in the map in one of the following ways:

   //--- add a String representation of an int...
   map.put ("a", "123");  

   //--- add an Integer wrapper around an int..
   map.put ("a", new Integer(123));

Either way, you can convert the value back to an int primitive (but your extraction technique will depend on what type the value is).  For example, if you added it as a String :

               Map.Entry entry = (Map.Entry)iter.next();
               String term = (String)entry.getKey();

               String strValue = (String)entry.getValue();
               int intValue = Integer.parseInt(strValue);
 
If you added it as an Integer :


               Map.Entry entry = (Map.Entry)iter.next();
               String term = (String)entry.getKey();

               Integer intWrapper = (Integer)entry.getValue();
               int intValue = intWrapper.intValue();


0
 
objectsCommented:
depends what type of object it is :)
0
 
objectsCommented:
If its a string use:

int t = Integer.parseInt((String) map.getValue());
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
HomerrSimpsonAuthor Commented:
its an int
0
 
WebstormCommented:
Hi HomerrSimpson,

>> its an int
Maybe you mean an Integer object:
int t = ((Integer)map.getValue())).intValue();
0
 
WebstormCommented:
>> TreeMap map = new TreeMap();
There is no getValue() method in java.util.TreeMap. To get a value from the map you need the key and call the get method:
Object get(Object key)
0
 
HomerrSimpsonAuthor Commented:
ahh yeah that was typo this is what i got
i am using Map.Entry that had a getValue()

TreeMap map = new TreeMap();
Set entries = map.entrySet();
Iterator iter = entries.iterator();
            
 while (iter.hasNext()) {
               Map.Entry entry = (Map.Entry)iter.next();
               String term = (String)entry.getKey();

int t = ((Integer)entry.getValue())).intValue(); <--doesnt work either

0
 
HomerrSimpsonAuthor Commented:
ahh it does work an extra ) was there
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.