commons-lang3的NumberUtils类的常用方法总结

NumberUtils

    该类是操作与数值相关的工具类,例如,字符串转换数字,数字间的比较,校验数字范围等,还可以对基本类型数组内的值取最大值,最小值等,下面我们来学习下,常用的方法。

 

和纯java实现方法不同之处

    当我们实现一个从字符串转换数字的传统方法,一般使用Integer#valueOf的方法,但是,此方法如果将一个不是int类型的数据转换成int时,会抛出异常,NumberUtils类中,字符串转换int时,如果抛异常,可以设定一个初期值,使程序更加灵活。

 

常用方法

  • toInt

        将字符串转换为数字,如果,转换失败的时候,返回0,还可以自己设定当转换失败时的默认值,不只toInt方法,还提供了其他基本数据类型的方法,例如,toByte,toShort,toDouble等等,要做的事情大同小异,我就不一一举例了,大家可以自行查看源码

    public static int toInt(String str) {


    public static int toInt(String str, int defaultValue) {

 

测试代码:

package NumberUtilsTest;

import org.apache.commons.lang3.math.NumberUtils;

public class ToIntTest {

    public static void main(String[] args) {

        System.err.println(NumberUtils.toInt("1"));
        System.err.println(NumberUtils.toInt("iosoft2020"));
        System.err.println(NumberUtils.toInt("iosoft2020", -1));

    }

}

执行结果

1
0
-1

  • max和min

        在指定数组中,找到最大值和最小值,并返回,同样的方法,也提供了其他基本类型的数组,如byte[],short[]等等

    public static int max(int[] array) {


    public static int min(int[] array) {

测试代码:

package NumberUtilsTest;

import org.apache.commons.lang3.math.NumberUtils;

public class MaxAndMinTest {

    public static void main(String[] args) {

        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        System.err.println("max=" + NumberUtils.max(array));
        System.err.println("min=" + NumberUtils.min(array));

    }

}

执行结果

max=9
min=1

 

其他数值操作的工具类

    common-lang包中也提供了其他操作数值的工具类

  • Range类

        该类是操作数值的范围类,本文在写的时候,用的是commons-lang3-3.0的版本,它移除了IntRange,DoubleRange,FloatRange,LongRange,NumberRange类,直接使用Range类,该类的构造方法是私有的,需要调用between和is方法来获取实例,我们看看他的常用方法

  1. contains

            该方法用来判断一个数值的范围内是否包括指定的值

    public boolean contains(T element) {

      2.isOverlappedBy

            该方法用来判断指定的数值范围是否和已知的数值范围有交集

    public boolean isOverlappedBy(Range otherRange) {

     3.containsRange

            该方法用来判断已知的数值范围是否包括指定的数值范围

    public boolean containsRange(Range otherRange) {

测试代码:

package OtherUtilsTest;

import org.apache.commons.lang3.Range;

public class RangeTest {

    public static void main(String[] args) {
        Range range = Range.between(20, 29);
        System.err.println("test contains");
        System.err.println(range.contains(20));
        System.err.println(range.contains(29));
        System.err.println(range.contains(30));
        System.err.println("test isOverlappedBy");
        System.err.println(range.isOverlappedBy(Range.between(25, 35)));
        System.err.println(range.isOverlappedBy(Range.between(30, 35)));
        System.err.println("test containsRange");
        System.err.println(range.containsRange(Range.between(20, 25)));
        System.err.println(range.containsRange(Range.between(26, 30)));

    }

}

执行结果

test contains
true
true
false
test isOverlappedBy
true
false
test containsRange
true
false

 

  • RandomUtils

        该类是生成指定范围内的随机数,提供了基本数据类型生成随机数的方法

    public static int nextInt(final int startInclusive, final int endExclusive) {

 

测试代码:

package OtherUtilsTest;

import java.util.Random;

import org.apache.commons.lang3.RandomUtils;

public class RandomUtilsTest {

    public static void main(String[] args) {
        System.err.println(new Random().nextInt(10));   //传统java实现一个生成10以下的随机数
        System.err.println(RandomUtils.nextInt(0, 10));

    }

}

执行结果

6
5

 

    到今天为止,学习了,commons-lang3包下,操作字符串,数组,数值,随机数等的方法,大家感觉怎么样,到现在学到的方法是不是能解决80%开发中基本操作,下篇文章再见。

 

你可能感兴趣的:(apache工具类,字符串,java)