Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 914
  • Last Modified:

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();
0
HomerrSimpson
Asked:
HomerrSimpson
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
objectsCommented:
depends what type of object it is :)
0
 
objectsCommented:
If its a string use:

int t = Integer.parseInt((String) map.getValue());
0
 
HomerrSimpsonAuthor Commented:
its an int
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
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
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.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now