Solved

Treemap.getValue()

Posted on 2004-08-10
8
888 Views
Last Modified: 2011-08-18
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
Comment
Question by:HomerrSimpson
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
depends what type of object it is :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
If its a string use:

int t = Integer.parseInt((String) map.getValue());
0
 

Author Comment

by:HomerrSimpson
Comment Utility
its an int
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
Hi HomerrSimpson,

>> its an int
Maybe you mean an Integer object:
int t = ((Integer)map.getValue())).intValue();
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 20 total points
Comment Utility
>> 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
 

Author Comment

by:HomerrSimpson
Comment Utility
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
 

Author Comment

by:HomerrSimpson
Comment Utility
ahh it does work an extra ) was there
0
 
LVL 7

Accepted Solution

by:
lhankins earned 30 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now