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?
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
Now to get the object from the set you can
and using the autoboxing feature you get