I'm working on a program for class. Using a TreeMap to store IDs (String - Key) and earnings amounts (double - value). I'm importing the values from a text file using a Scanner. My problem at the moment is that I need the values to accumulate rather than overwriting with the last value read. So my question is how do you use a Map to do calculations like that? Any help would be appreciated.
Asked
Active
Viewed 135 times
3 Answers
3
There is no implicit functionality in Map
. Idea behind your homework assignment is for you to learn how to insert, find, get and replace to/from a Map
. There are functions for each of these and ou should use all to get this done.

Kashyap
- 15,354
- 13
- 64
- 103
1
When adding a new value to your map, if the key already exists, you can get the associated value, add the new value to it, and put it back into the map. Example:
// Assuming that key and value were read from your file, and that
// myMap is declared as "Map<String, Double>"
if (myMap.containsKey (key)) {
double oldValue = myMap.get (key);
value += oldValue;
}
myMap.put (key, value);

Laf
- 7,965
- 4
- 37
- 52
-
@thekashyap if it makes you feel better this code doesn't work for me as it is. The second line gives an incompatible type error because the variable is a double but the key is a string... I really wish spec docs were easier to read. – Steve the Maker Feb 29 '12 at 18:02
-
@StevetheMaker How did you declare your `Map`? – Laf Feb 29 '12 at 18:12
-
@Laf I just used: TreeMap agentMap = new TreeMap(); When I tried to specify types in the constructor I was getting errors that I couldn't figure out so I thought I'd give this a try. – Steve the Maker Feb 29 '12 at 18:18
-
1You should use generics to declare your tree, so that you get type checking at compile time rather than errors at runtime. Your declaration should look like: `TreeMap
agentMap = new TreeMap – Laf Feb 29 '12 at 18:20();`. This is assuming that your key is a `String`, and your values are `Double`. -
OMG... I just wasn't capitalizing the word Double. Why would that need to be caps sometimes and lowercase other times? – Steve the Maker Feb 29 '12 at 18:26
-
1Capitalized means that you are using the class, and the lowercase means you are using the primitive type. Maps can only store objects, so in this case you need a `Double`, and not a `double`. Java will automatically convert `Double` into `double` and vice-versa (you can read more information on auto-boxing [here](http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html)). – Laf Feb 29 '12 at 18:32
-
@StevetheMaker it does make me feel better that it didn't work.. you also learned that you didn't know enough of template specialization (generics), implicit type casting (auto-boxing) etc. Java's javadocs are sooo much better to browse than man pages and C++ comments.. – Kashyap Feb 29 '12 at 18:41
-
Well, I appreciate the hard-handed and the soft-handed approach. I'm trying to get a degree online and I don't want a pile of certs and no ability to code when I'm finished (I'm at least past fizzbuzz at this point). At the same time, I don't have a single human to bounce ideas off of, so SO and SE are essentially my classroom. – Steve the Maker Feb 29 '12 at 18:52
-
I wish to note some issue. For the first, why do you use TreeMap? It is a sorted implementation, its search takes O(log n) time. So, if you may consider using HashMap istead. – Alexandr Mar 01 '12 at 12:43
-
for the second, program to interfaces, so, never use concreate implementations when create a reference. Use Map
agentMap = new TreeMap – Alexandr Mar 01 '12 at 12:44(); instead. -
for the third, concerning performance issue, I would recommend to use primitive collections, you may read here for details http://stackoverflow.com/questions/2504959/storing-primitive-values-in-a-java-collection – Alexandr Mar 01 '12 at 12:47
1
1) Check whether value with same key exists in the map 2) If it exists then read it and add the currently read value. Put it back into map

JProgrammer
- 1,135
- 1
- 10
- 27