I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in?
http://
No, Java doesn't have these built in. And that is for a reason. Using mutable types is dangerous, as they can easily be misused. Additionally, it is really easy to implement it. For example, commons-lang has a MutableInt.
You can import the org.omg.CORBA package(or just the class you need) and in it you can use the Holder classes.
For example, it has the "IntHolder" where the field where it stores the integer is public, giving access to modify it.
public static void triple(IntHolder x){
x.value = 3 * x.value;
}
IntHolder mutableInt = new IntHolder(10);
triple(mutableInt);
System.out.println(mutableInt.value);
It also has "LongHolder" and "DoubleHolder" and tons of others that you can use. Use with caution.
Here is the api for it: https://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/package-summary.html
Since JDK 1.5 java now has java.util.concurrent.atomic.AtomicInteger
This is a thread safe mutable integer, example of use:
final AtomicInteger value = new AtomicInteger(0);
then later on:
value.incrementAndGet();
Here's a small class I made for a mutable integer:
public class MutableInteger {
private int value;
public MutableInteger(int value) {
this.value = value;
}
public void set(int value) {
this.value = value;
}
public int intValue() {
return value;
}
}
You could easily extend this to any other primitive. Of course, like everyone else is saying, you should use it carefully.
You could always wrap the value in an array like int[] mutable = {1};
if including the code for a mutable wrapper class is too cumbersome.
AtomicInteger
has already been mentioned. Mutable Double
s can be emulated with AtomicReference<Double>
. The already mentioned warnings apply and it is bad style, but sometimes you have code like this
double sum=0
for (Data data:someListGenerator())
sum+=data.getValue()
and want to refactor it in functional Java 8 style. If the code follows this pattern but adds considerable complexity to it, the most sensible conversion could be
AtomicReference<Double> sumref=new AtomicReference<>(0d);
someStreamGenerator().forEach(data->
sumref.set(sumref.get().doubleValue()+data.getValue()));
double sum=sumref.get().doubleValue();
Of course, this is at least questionable style. But I found myself more than once in a situation with a twisted loop over a ResultSet
computing and partly cumulating three different information from it. This makes it really hard to convert the code into proper functional style. Converting the cumulating parts according to the above pattern seemed to me a reasonable tradeoff between clean code and oversimplified refactoring.