Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Updating an object in a HashMap

Reply
Thread Tools

Updating an object in a HashMap

 
 
alacrite@gmail.com
Guest
Posts: n/a
 
      02-13-2008
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;
//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.
 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      02-13-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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;
> //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?


The problem here is that Integers (along with Strings, Doubles, etc) are
immutable. For Integer i, the expression i += 5 is autoboxing shorthand for

i = new Integer (i.intValue() + 5)

hm.get("test1") gives you the reference to Integer object, but is not itself
a variable that can be assigned to--it's just the returned expression value
(in this case the reference) Instead you must do

hm.put("test1", hm.get("test1") + 5)

Note that if you had an array of integers Integer [] iarray. you could say
iarray[j] += 5; because iarray[j] does work as a variable.

Matt Humphrey http://www.iviz.com/


 
Reply With Quote
 
 
 
 
Arne Vajhj
Guest
Posts: n/a
 
      02-13-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
> //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?


The left hand side is not a literal.

It is an expression and you can not assign to an expression.

Arne
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-14-2008
(E-Mail Removed) 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-14-2008
Arne Vajhøj wrote:
> (E-Mail Removed) 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;
>> //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?

>
> The left hand side is not a literal.
>
> It is an expression and you can not assign to an expression.


For example, the statements

int i;
(i) = 5;

would also fail to compile.

--
Lew
 
Reply With Quote
 
John W. Kennedy
Guest
Posts: n/a
 
      02-14-2008
(E-Mail Removed) 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;
> //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.


hm.get returns the Integer, but then you've done intValue of that, and
that's a plain int.

> I would have assume a reference to the object would have been
> returned and calling += would have unboxed the Integer and aggregated
> the value.


Before you called intValue, you had a reference to the original Integer,
but Integer objects (just like Boolean, Byte, Short, Character, Long,
Float, Double, and String) are immutable. But after intValue, you had a
plain int, and you can't have a reference to a boolean, byte, short,
char, long, float, or double -- only objects.

> Could someone explain to me what is going on? Also, what is the best
> way to accomplish updating this value?


Since the Integer is immutable, you have to replace it with a new one.

hm.put("test1", hm.get("test1") + 5);

Autoboxing and unboxing translates this, in effect, to

hm.put("test1", new Integer(hm.get("test1").intValue + 5));

If the case were different -- say you were using an imaginary type
called "MutableInteger" -- you could do something like this:

hm.put("test1", new MutableInteger(0));
hm.get("test1").MutableInteger.add(5));
--
John W. Kennedy
"The grand art mastered the thudding hammer of Thor
And the heart of our lord Taliessin determined the war."
-- Charles Williams. "Mount Badon"
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-14-2008
Matt Humphrey wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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;
>> //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?

>
> The problem here is that Integers (along with Strings, Doubles, etc) are
> immutable. For Integer i, the expression i += 5 is autoboxing shorthand for
>
> i = new Integer (i.intValue() + 5)

<pedantry>
Actually, its short hand for i = Integer.valueOf(i.intValue()+5);
valueOf does some interning for a specific range of numbers.
</pedantry>
>
> hm.get("test1") gives you the reference to Integer object, but is not itself
> a variable that can be assigned to--it's just the returned expression value
> (in this case the reference) Instead you must do
>
> hm.put("test1", hm.get("test1") + 5)
>
> Note that if you had an array of integers Integer [] iarray. you could say
> iarray[j] += 5; because iarray[j] does work as a variable.
>
> Matt Humphrey http://www.iviz.com/
>
>


Alternatively, you could do this:

public class IntWrapper {
private int value;
public void incBy(int inc) {
value += inc;
}
}

Map<String, IntWrapper> hm = getHm();
hm.get("test1").incBy(5);



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
alacrite@gmail.com
Guest
Posts: n/a
 
      02-14-2008
On Feb 13, 6:00 pm, (E-Mail Removed) 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;
> //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.


Thank you all for your replies. I did not realize that Boolean, Byte,
Short, Character, Long,
Float, and Double objects were immutable.


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      02-14-2008
Lew wrote:
> Arne Vajhøj wrote:
>> The left hand side is not a literal.
>>
>> It is an expression and you can not assign to an expression.

>
> For example, the statements
>
> int i;
> (i) = 5;
>
> would also fail to compile.
>


So does that mean that (i) is an r-value in Java? And are only
primitives (and references, being one type of primitive) assignable in
Java? Are primitives the only l-value in Java?

I could probably get off my lazy tush and look this up in the JLS, but I
though it might be interesting topic for discussion. (Thereby
justifying my laziness.)

And autoboxing with the Object forms of the primitives looks like it's
been crowbarred to update the reference, not the Object....
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-14-2008
Arne Vajhøj wrote:
>>> The left hand side is not a literal.
>>>
>>> It is an expression and you can not assign to an expression.


Lew wrote:
>> For example, the statements
>>
>> int i;
>> (i) = 5;
>>
>> would also fail to compile.


Mark Space wrote:
> So does that mean that (i) is an r-value in Java? And are only
> primitives (and references, being one type of primitive) assignable in
> Java? Are primitives the only l-value in Java?


I'm not sure Java talks about r-values and l-values. The bottom line is that
only variables can have values assigned to them.

As an old C programmer myself, I think of variables as l-values, but really
that's not the Java terminology AIUI.

> And autoboxing with the Object forms of the primitives looks like it's
> been crowbarred to update the reference, not the Object....


I'm not sure exactly how you mean that. What do you mean by "crowbarred"?

There's no way to "update" a number wrapper class object like an Integer once
instantiated. So only the reference, i.e., the variable even can be updated.

Autoboxing causes a sequence like

Integer foo = new Integer( 17 );
foo += 23;

to assign a new Integer instance to foo. No Integer object is changed; the
Integer representing 17 is dropped and a new one referenced. Because values
from -128 to 127 are sort-of interned, and a larger range could be, there is
pretty likely no GC involved.
<http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7>

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reuse HashMap$Entry (or HashMap in total) to avoid millions of allocations Vince Darley Java 4 03-02-2010 07:48 AM
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
Updating JTable Atomically - block data changes while updating. news.rcn.com Java 2 12-10-2007 11:49 AM
HashMap<Object, Double> returns what instead of null? aaronfude@gmail.com Java 4 05-09-2006 09:48 AM
why did this hashmap always returned the same object? ravi mannan Java 2 11-23-2003 10:10 PM



Advertisments