Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performing Math on Integer Class

 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know of an efficient way to perform math on an Integer class or a better way to store an integer value as an object that is mutable?

I have a TreeMap that looks like this ...

What is the best approach?



Thanks,
Aryeh
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just make a new class to do what you want:

This way, you don't need to keep creating new Integer objects and inserting them in place of the old one.

Alternately, java.util.concurrent.atomic.AtomicInteger already exists with most of the methods you might want.
[ January 23, 2008: Message edited by: Jim Yingst ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
General code critique:
intValue() returns an int, not an Integer.
you are assigning the intValue() to an Integer,
it gets autoboxed, doing more work than needed,

the map contains Integers, get() will return an Integer

there is no reason for ++i, you never use i again, use +1

finally: if you want a mutable integer class, you can implement one, i don't think you need one - don't worry about "efficiency" until you're sitting around waiting on your code to execute

recoded:


fwiw, you are implementing a Bag semantic

[ January 23, 2008: Message edited by: Bill Shirley ]
[ January 23, 2008: Message edited by: Bill Shirley ]
 
Aryeh Golob
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim & Bill,

I appreciate your advice!

-Aryeh
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bill]: finally: if you want a mutable integer class, you can implement one, i don't think you need one - don't worry about "efficiency" until you're sitting around waiting on your code to execute

I agree regarding efficiency. I was mostly thinking in terms of ease-of-use. I want the people who use the counter to not have to type several lines just to do an increment. On reflection, it would probably be better to package this a little differently, so users also don't have to think about having to create a new Counter in case of null:

[ January 23, 2008: Message edited by: Jim Yingst ]
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are using generics, I assume that you are using Java 5. That means you can just use autoboxing:



It may not be the best solution in terms of performance, because there is a lot of stuff going on in the background, but it is so much easier on the eye.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the Integer count is being held by a Map, then count++ has no effect on what's in the Map. You're just creating a new Integer object - but the Map still contains a reference to the old one, not the new one. That's why Aryeh was originally asking for a mutable object, and why Bill, because he used an immutable object, had to re-insert the new Integer into the Map after it was incremented. Simply using a ++ operator doesn't help unless you also show how the object in the Map gets updated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic