[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Treemap.getValue()

Posted on 2004-08-10
8
Medium Priority
?
902 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11768165
depends what type of object it is :)
0
 
LVL 92

Expert Comment

by:objects
ID: 11768168
If its a string use:

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

Author Comment

by:HomerrSimpson
ID: 11768201
its an int
0
Independent Software Vendors: 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!

 
LVL 13

Expert Comment

by:Webstorm
ID: 11768231
Hi HomerrSimpson,

>> its an int
Maybe you mean an Integer object:
int t = ((Integer)map.getValue())).intValue();
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 80 total points
ID: 11768249
>> 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
ID: 11768305
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
ID: 11768312
ahh it does work an extra ) was there
0
 
LVL 7

Accepted Solution

by:
lhankins earned 120 total points
ID: 11769549
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

656 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