aspose file tools*
The moose likes Java in General and the fly likes Performing Math on Integer Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Performing Math on Integer Class" Watch "Performing Math on Integer Class" New topic
Author

Performing Math on Integer Class

Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Thanks Jim & Bill,

I appreciate your advice!

-Aryeh
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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

Joined: Feb 20, 2006
Posts: 132
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

Joined: Jan 30, 2000
Posts: 18671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performing Math on Integer Class