Mutable接口提供了一个通用的接口去实现可变数据类型的实现
所有已知实现Mutable接口的类有MutableBoolean, MutableByte, MutableDouble, MutableFloat, MutableInt, MutableLong, MutableObject, MutableShort,这些类都是可变的,也就是修改对象的值不需要重新创建新的对象;
典型的用例是使用原始数据类型或字符串作为参数传递给一个方法并且允许方法修改原始数据或者字符串;
另外一种典型的用例是存储经常变动的原始数据类型到容器中(例如:存入map)无需创建Integer/Long包装器;
实例
package org.apache.commons.lang3.mutable;
public abstract interface Mutable<T> { public abstract T getValue(); public abstract void setValue(T paramT); }
接口的定义如下。
package org.apache.commons.lang3.mutable; public abstract interface Mutable<T> { public abstract T getValue(); public abstract void setValue(T paramT); }
MutableInt可变整型类详解
只举例说明一下整型,其余的类同
package org.apache.commons.lang3.mutable; import org.apache.commons.lang3.math.NumberUtils; public class MutableInt extends Number implements Comparable<MutableInt>, Mutable<Number> { private static final long serialVersionUID = 512176391864L; private int value; /** * 声明一个可变整数,默认值为0 **/ public MutableInt() {} /** *声明一个参数为int类型的值初始化MutableInt构造函数 */ public MutableInt(int value) { this.value = value; } /** * 声明一个参数为Number类型的值初始化MutableInt构造函数 */ public MutableInt(Number value) { this.value = value.intValue(); } /** *声明一个参数为String类型的值初始化MutableInt构造函数 */ public MutableInt(String value) throws NumberFormatException { this.value = Integer.parseInt(value); } /** * 获取MutableInt存储的值,并将值转换为Integer包装类型返回 */ public Integer getValue() { return Integer.valueOf(this.value); } /** * 传递一个int类型的参数来修改MutableInt存储的默认值 */ public void setValue(int value) { this.value = value; } /** * 传递一个Number类型额参数修改MutableInt存储的默认值 */ public void setValue(Number value) { this.value = value.intValue(); } /** * MutableInt加1 */ public void increment() { this.value += 1; } /** * 该方法将MutableInt的值加一并返回未加一之前的数据,非线程安全的 */ public int getAndIncrement() { int last = this.value; this.value += 1; return last; } /** * 将MutableInt存储的值加一并返回 */ public int incrementAndGet() { this.value += 1; return this.value; } /** * 将MutableInt存储的值减一 */ public void decrement() { this.value -= 1; } /** * 将MutableInt存储的值减一并返回未减一之前的数据 */ public int getAndDecrement() { int last = this.value; this.value -= 1; return last; } /** * 将MutableInt存储的值减一并返回 */ public int decrementAndGet() { this.value -= 1; return this.value; } /** * 将MutableInt存储的值加上指定的int类型值 */ public void add(int operand) { this.value += operand; } /** * 将MutableInt存储的值加上指定的Number类型的值 */ public void add(Number operand) { this.value += operand.intValue(); } /** * 将MutableInt存储的值减去指定的int类型值 */ public void subtract(int operand) { this.value -= operand; } /** * 将MutableInt存储的值减去指定的Number类型的值 */ public void subtract(Number operand) { this.value -= operand.intValue(); } /** * 将MutableInt存储的值加上指定的int类型的值并返回结果 */ public int addAndGet(int operand) { this.value += operand; return this.value; } /** * 将MutableInt存储的值将上指定的Number类型的值并返回结果 */ public int addAndGet(Number operand) { this.value += operand.intValue(); return this.value; } /** * 将MutableInt存储的值加上指定的int类型的值并返回之前的值 */ public int getAndAdd(int operand) { int last = this.value; this.value += operand; return last; } /** * 将MutableInt存储的值加上指定的Number类型的值并返回之前的值 */ public int getAndAdd(Number operand) { int last = this.value; this.value += operand.intValue(); return last; } /** * 将MutableInt类型转换成int类型 */ public int intValue() { return this.value; } /** * 将MutableInt类型转换成long类型 */ public long longValue() { return this.value; } /** * 将MutableInt类型转换成float类型 */ public float floatValue() { return this.value; } /** * 将MutableInt类型转换成double类型 */ public double doubleValue() { return this.value; } /** * 将MutableInt类型转换成Integer类型 */ public Integer toInteger() { return Integer.valueOf(intValue()); } /** * 比较两个MutableInt类型数据是否相等 */ public boolean equals(Object obj) { if ((obj instanceof MutableInt)) { return this.value == ((MutableInt)obj).intValue(); } return false; } /** * */ public int hashCode() { return this.value; } /** * 比较两个MutableInt数据的大小 */ public int compareTo(MutableInt other) { return NumberUtils.compare(this.value, other.value); } /** * */ public String toString() { return String.valueOf(this.value); } }
使用示例:
public static void main(String[] args) throws Exception { //Integer count = 0 ; //IntStream.range(1,10).forEach(x->{ // count++; //这样子编译是报错的 //}); //System.out.println(count); //因此此处我们可以采用MutableInt改进 MutableInt count = new MutableInt(0); IntStream.range(1,10).forEach(x->{ count.increment(); }); System.out.println(count.getValue()); //9 //采用MutableInt代替AtomicInteger 效率更高 //AtomicInteger count = new AtomicInteger(0); }
因此你对线程安全没有要求,再不得不使用可变类型的情况下,请使用MutableInt代替AtomicInteger 效率更高
Tips:如过你是共享变量,对线程安全有要求,请使用AtomicInteger
最后
最后,在介绍几个不太常用的工具。但有时候用好了也能事半功倍哟
NumberUtils:专门处理数字之间转化、精度转换、找对大值等等操作
ExceptionUtils:一些对异常打印的处理,course by等等
ToStringBuilder、HashCodeBuilder、EqualsBuilder等等能很方便切效率更高的方式去处理三个基础方法。
@Override public String toString() { return ToStringBuilder.reflectionToString(this); } @Override public boolean equals(final Object o) { return EqualsBuilder.reflectionEquals(this, o); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); }