思路
把所有文言放到一个不可变集合中,然后使用随机函数随机获取
需要的包
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.math.RandomUtils;
import java.util.List;
Immutable不可变集合
不可变集合,说集合是不可被修改的,集合的数据项是在创建的时候提供,并且在整个生命周期中都不可改变。
优点
1.它使用安全
2.线程安全的:immutable对象在多线程下安全,没有竞态条件
3.不可变
4.可以被使用为一个常量,并且期望在未来也是保持不变的
使用
private static final List<String> messageList = ImmutableList.of(
"1111111111111",
"222211111111",
"333331111111",
"4444411111111",
"5555551111111",
"6666666611111111",
"777777777111111",
"88888888888111",
"999999999999");
随机数获取
public static String getMeassge(){
int index = RandomUtils.nextInt(messageList.size());
return messageList.get(index);
}