Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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