tags:

views:

5503

answers:

6

I have a TreeSet, which will be full of integers. To make a long story short, I'm trying to loop starting after the last (greatest) value stored in the list. What I'm doing now to get the starting variable is:

    Object lastObj = primes.last();
    Integer last = new Integer(lastObj.toString());
    int start = 1 + last.intValue(); // the added 1 is just for program logic

I'm sure that there must be a better way to cast an object (which I know will always be an int) into the int 'start'. Anyone know of a better way of doing this?

+8  A: 

In J2SE 5 or later it happens automatically, with the "autoboxing" feature.

int start = 1 + last;

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

Also, if you know they're all going to be Integer objects, use the parameterized type:

List<Integer> obj = new ArrayList<Integer>()...

Then combine it with the autoboxing feature:

int start = 1 + obj.last();
jbm
My only objection to autoboxing is a situations where you "appear" to be working with ints, so you do a == comparison which is a reference comparision, rather than the expected value comparison.
Ryan Emerle
Ryan, I thought it if you tried int == Integer, then the Integer would autobox into an int and Java would compare the two primitives.
James McMahon
How true. Every time you rely on something happening automatically, you add another layer of thought that needs to be happening in the back of your head. C++ auto-generated constructors!
jbm
@nemo - from the docs "The == operator performs reference identity comparisons on Integer expressions and value equality comparisons on int expressions." Cursed Java "magic" :)
Ryan Emerle
+4  A: 

If you know that they were 'int' when they were put in, then they were transformed to Integer while in the Collection (Collections cannot contain primitives, only objects), as such, you can simply Integer last = (Integer)lastObj;.

Ideally though, you would use a TreeSet<Integer> and then it would just feed you Integers in the first place.

lostlogic
+2  A: 

Post Java 1.4, you can use autoboxing.

So it becomes,

int start = 1 + (Integer) primes.last(); // the added 1 is just for program logic

If you used generics with your TreeSet (TreeSet<Integer>) you could remove the initial cast to Integer.

James McMahon
+2  A: 

Why can't you just cast it, rather that converting to a string, then parsing that string and creating a new reference?

Object lastObj = primes.last();
int start = 1 + ( ( Integer )lastObj ).intValue();
Ryan Emerle
+6  A: 

Are you using Java version 1.6? In that case you can take advantage of autoboxing and generics to clean up the code.

First, the TreeSet can be declared as containing only Integer objects

TreeSet<Integer> primes;

Now to get the object from the set you can

Integer last = primes.last();

and using the autoboxing feature you get

int start = 1 + last;
Vincent Ramdhanie
you could just do, int start = 1 + primes.last();
James McMahon
That would be even less code to write...but to illustrate the generics improvement vs the autoboxing improvements its best to leave them as separate statements for this answer
Vincent Ramdhanie
Yeah I see where you are coming from. Just posted my comment as a footnote.
James McMahon
Autoboxing and generics are actually available in Java 1.5 and later.
newacct
+3  A: 

If you know primes is holding just integers, you should make primes into a TreeSet<Integer>.

It would then become:

int start = 1 + primes.last().intValue();

If you can't use generics use this:

int start = 1 + ((Integer)prime.last()).intValue();

Casting into a string would just be silly.

By the way, I don't suggest using autoboxing. It does all kinds of things behind your back. Explicitly using Integer seems clearer to me. But this is just my personal preference, you can use autoboxing if you want.

Zifre
Autoboxing is built into the language; you can't choose not to use it. I advise just the opposite: never declare anything (variable, method argument, etc.) as Integer if you can use int instead (nd the same goes for the other primitive types). Bloch gives the same advice in Effective Java 2ed.
Alan Moore
@Alan M: Obviously you would want to use int instead of Integer when possible, but I would suggest always making the conversion explicit, so you know when you are doing it.
Zifre