commons-lang3-3.2.jar中的常用工具类的使用

时间:2023-01-15 19:18:32

===========commons-lang包======

   这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用。

1.StringUtils工具类

  可以判断是否是空串,是否为null,默认值设置等操作:

    /**
* StringUtils
*/
public static void test1() {
System.out.println(StringUtils.isBlank(" "));// true----可以验证null, ""," "等
System.out.println(StringUtils.isBlank("null"));// false
System.out.println(StringUtils.isAllLowerCase("null"));// t
System.out.println(StringUtils.isAllUpperCase("XXXXXX"));// t
System.out.println(StringUtils.isEmpty(" "));// f---为null或者""返回true
System.out.println(StringUtils.defaultIfEmpty(null, "default"));// 第二个参数是第一个为null或者""的时候的取值
System.out.println(StringUtils.defaultIfBlank(" ", "default"));//// 第二个参数是第一个为null或者""或者" "的时候的取值
}

isBlank()  可以验证空格、null、"",如果是好几个空格也返回true

isEmpty验证不了空格,只有值为null和""返回true

两者都验证不了"null"字符串,所以如果验证"null"还需要自己用equals进行验证。

结果:

true
false
true
true
false
default
default

简单的贴出几个源码便于记录:

    public static boolean isBlank(final CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
} public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
    public static String defaultIfEmpty(String str, String defaultStr) {
return StringUtils.isEmpty(str) ? defaultStr : str;
}

  CharSequence是一个接口,String,StringBuffer,StringBuilder等都实现了此接口

public abstract interface CharSequence {
public abstract int length(); public abstract char charAt(int paramInt); public abstract CharSequence subSequence(int paramInt1, int paramInt2); public abstract String toString();
}

补充:StringUtils也可以将集合和数组转为String,并且以指定符号链接里面的数据

        List list = new ArrayList(2);
list.add("张三");
list.add("李四");
list.add("王五");
String list2str = StringUtils.join(list, ",");
System.out.println(list2str);

结果:

  张三,李四,王五

有时候我们希望给拼接后的字符串都加上单引号,这个在拼接SQL  in条件的时候非常有用,例如:

        //需求:将逗号里面的内容都加上单引号
String string = "111,222,333";
string = "'"+string+"'";//字符串前后加'
string = StringUtils.join(string.split(","),"','");//先按逗号分隔为数组,然后用','连接数组
System.out.println(string);

结果:

'111','222','333'

  join方法也可以接收数组,可变参数等,也可以对数组进行拼接,如下:

        int[] numbers = { 1, 3, 5 };
System.out.println(StringUtils.join(numbers, ','));

结果:

1,3,5

其重载方法如下:

commons-lang3-3.2.jar中的常用工具类的使用

补充:null和字符串"null"的区别

null在JVM中没有分配内存,引用中无任何东西,debug也看不到任何东西,"null"字符串是一个正常的字符串,在JVM分配内存而且可以看到东西

"null"字符串有东西

commons-lang3-3.2.jar中的常用工具类的使用

null无任何东西:

commons-lang3-3.2.jar中的常用工具类的使用

补充:String.format(format,Object)也可以对字符串进行格式化,例如在数字前面补齐数字

        int num = 50;
String format = String.format("%0" + 5 + "d", num);
System.out.println(format);

结果:

00050

补充:StringUtils也可以截取字符串,判断是否大小写等操作

        String string = "123_45_43_ss";
System.out.println(StringUtils.isAllLowerCase(string));// 判断全部小写
System.out.println(StringUtils.isAllUpperCase(string));// 判断全部大写
System.out.println(StringUtils.substringAfter(string, "123"));// 截取123之后的
System.out.println(StringUtils.substringBefore(string, "45"));// 截取45之前的
System.out.println(StringUtils.substringBefore(string, "_"));// 截取第一个_之前的
System.out.println(StringUtils.substringBeforeLast(string, "_"));// 截取最后一个_之前的
System.out.println(StringUtils.substringAfter(string, "_"));// 截取第一个_之后的
System.out.println(StringUtils.substringAfterLast(string, "_"));// 截取最后一个_之后的
System.out.println(StringUtils.substringBetween("1234565432123456", "2", "6"));// 截取两个之间的(都找的是第一个)

结果:

false
false
_45_43_ss
123_
123
123_45_43
45_43_ss
ss
345

补充:StringUtils也可以将字符串分割为数组

package cn.xm.exam.test;

import org.apache.commons.lang.StringUtils;

public class test {

    public static void main(String[] args) {
String t = "tttt";
System.out.println(StringUtils.split(t, ","));
}
}

结果:

[Ljava.lang.String;@5a24389c

看过深入理解JVM的都知道上面的[代表是一维数组类型,L代表是引用类型,后面的是String类型

补充:  isNoneBlank可以支持多个参数,甚至String数组,用来判断数组里的每一个字符串都是isNotBlank的。

commons-lang3-3.2.jar中的常用工具类的使用

补充:StringUtils可以获取指定字符出现的次数

StringUtils.countMatches(str, ":")

补充:StringUtils可以第N次某字符串出现的位置

StringUtils.ordinalIndexOf(str, ":", 2)

补充:StringUtils可以获取指定字符串之间的字符串,并自动截取为字符串数组

String[] substringBetweens = StringUtils.substringsBetween(str, ":", ":");

补充:StringUtils可以获取指定字符串之间的字符串(只取满足条件的前两个)

StringUtils.substringBetween(str2, "/")

补充:Stringutils可以左截取和右截取

        // 左右截取
System.out.println(StringUtils.left("abc", 2));
System.out.println(StringUtils.right("abc", 2));

结果:

ab
bc

查看源码:

    public static String left(String str, int len) {
if (str == null) {
return null;
}
if (len < 0) {
return EMPTY;
}
if (str.length() <= len) {
return str;
}
return str.substring(0, len);
} public static String right(String str, int len) {
if (str == null) {
return null;
}
if (len < 0) {
return EMPTY;
}
if (str.length() <= len) {
return str;
}
return str.substring(str.length() - len);
}

补充:StringUtils可以左右填充指定字符串

        // 左添加(默认添加空格)
String leftPad = StringUtils.leftPad("2", 2);
System.out.println(leftPad);
String leftPad2 = StringUtils.leftPad("12", 3, "0");
System.out.println(leftPad2); // 右添加(默认添加空格)
String rightPad = StringUtils.rightPad("2", 2);
System.out.println(rightPad);
String rightPad2 = StringUtils.rightPad("12", 3, "0");
System.out.println(rightPad2);

结果:

2
012
2
120

补充:StringUtils可以忽略大小写判断equals以及contains等

import org.apache.commons.lang3.StringUtils;

public class Plaintest {

    public static void main(String[] args) {
boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase("AA", "aa");
System.out.println(equalsIgnoreCase); boolean containsIgnoreCase = StringUtils.containsIgnoreCase("Abc", "BC");
System.out.println(containsIgnoreCase);
}
}

结果:

true
true

2.StringEscapeUtils----------转义字符串的工具类

        //1.防止sql注入------原理是将'替换为''
System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeSql("1' or '1' = '1"));
//2.转义/反转义html
System.out.println( org.apache.commons.lang.StringEscapeUtils.escapeHtml("<a>dddd</a>")); //&lt;a&gt;dddd&lt;/a&gt;
System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeHtml("&lt;a&gt;dddd&lt;/a&gt;")); //<a>dddd</a>
//3.转义/反转义JS
System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeJavaScript("<script>alert('1111')</script>"));
//4.把字符串转为unicode编码
System.out.println(org.apache.commons.lang.StringEscapeUtils.escapeJava("中国"));
System.out.println(org.apache.commons.lang.StringEscapeUtils.unescapeJava("\u4E2D\u56FD"));
//5.转义JSON
System.out.println(org.apache.commons.lang3.StringEscapeUtils.escapeJson("{name:'qlq'}"));

结果:

1'' or ''1'' = ''1
&lt;a&gt;dddd&lt;/a&gt;
<a>dddd</a>
<script>alert(\'1111\')<\/script>
\u4E2D\u56FD
中国
{name:'qlq'}

补充:关于SQL注入做进一步解释。上面的防止SQL注入一般是对传入的条件进行转义,不可以对整条SQL进行转义,整条转义SQL执行的时候会报错。防止恶意SQL。例如:

万能用户名和密码如下:

        String username = "1' or '1' = '1";
String password = "admin' or '1' = '1";

分别做转义和不做转义的情况如下:

        String sql = "select * from user where username = '" + username + "' and password = '" + password + "'";
System.out.println(sql); String sql2 = "select * from user where username = '" + StringEscapeUtils.escapeSql(username)
+ "' and password = '" + StringEscapeUtils.escapeSql(password) + "'";
System.out.println(sql2);

结果:(转义之后可以防止SQL注入,是将' 替换为 '' (一个单引号替换为两个连着的单引号。将''识别为普通的字符串而不是结束符,也就是转义后的username和password整体作为参数))

select * from user where username = '1' or '1' = '1' and password = 'admin' or '1' = '1'
select * from user where username = '1'' or ''1'' = ''1' and password = 'admin'' or ''1'' = ''1'

3.NumberUtils--------字符串转数据或者判断字符串是否是数字常用工具类

    /**
* NumberUtils
*/
public static void test3(){
System.out.println(NumberUtils.isNumber("231232.8"));//true---判断是否是数字
System.out.println(NumberUtils.isDigits("2312332.5"));//false,判断是否是整数
System.out.println(NumberUtils.toDouble(null));//如果传的值不正确返回一个默认值,字符串转double,传的不正确会返回默认值
System.out.println(NumberUtils.createBigDecimal("333333"));//字符串转bigdecimal
}

4.BooleanUtils------------判断Boolean类型工具类

    /**
* BooleanUtils
*/
public static void test4(){
System.out.println(BooleanUtils.isFalse(true));//false
System.out.println(BooleanUtils.toBoolean("yes"));//true
System.out.println(BooleanUtils.toBooleanObject(0));//false
System.out.println(BooleanUtils.toStringYesNo(false));//no
System.out.println(BooleanUtils.toBooleanObject("ok", "ok", "error", "null"));//true-----第一个参数是需要验证的字符串,第二个是返回true的值,第三个是返回false的值,第四个是返回null的值
}

5.SystemUtils----获取系统信息(原理都是调用System.getProperty())

    /**
* SystemUtils
*/
public static void test5(){
System.out.println(SystemUtils.getJavaHome());
System.out.println(SystemUtils.getJavaIoTmpDir());
System.out.println(SystemUtils.getUserDir());
System.out.println(SystemUtils.getUserHome());
System.out.println(SystemUtils.JAVA_VERSION);
System.out.println(SystemUtils.OS_NAME);
System.out.println(SystemUtils.USER_TIMEZONE);
}

6.DateUtils和DateFormatUtils可以实现字符串转date与date转字符串,date比较先后问题

  DateUtils也可以判断是否是同一天等操作。

package zd.dms.test;

import java.text.ParseException;
import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils; public class PlainTest {
public static void main(String[] args) {
// DateFormatUtils----date转字符串
Date date = new Date();
System.out.println(DateFormatUtils.format(date, "yyyy-MM-dd hh:mm:ss"));// 小写的是12小时制
System.out.println(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));// 大写的HH是24小时制 // DateUtils ---加减指定的天数(也可以加减秒、小时等操作)
Date addDays = DateUtils.addDays(date, 2);
System.out.println(DateFormatUtils.format(addDays, "yyyy-MM-dd HH:mm:ss"));
Date addDays2 = DateUtils.addDays(date, -2);
System.out.println(DateFormatUtils.format(addDays2, "yyyy-MM-dd HH:mm:ss")); // 原生日期判断日期先后顺序
System.out.println(addDays2.after(addDays));
System.out.println(addDays2.before(addDays)); // DateUtils---字符串转date
String strDate = "2018-11-01 19:23:44";
try {
Date parseDateStrictly = DateUtils.parseDateStrictly(strDate, "yyyy-MM-dd HH:mm:ss");
Date parseDate = DateUtils.parseDate(strDate, "yyyy-MM-dd HH:mm:ss");
System.out.println(parseDateStrictly);
System.out.println(parseDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

结果:

2018-11-02 07:53:50
2018-11-02 19:53:50
2018-11-04 19:53:50
2018-10-31 19:53:50
false
true
Thu Nov 01 19:23:44 CST 2018
Thu Nov 01 19:23:44 CST 2018

7.StopWatch提供秒表的计时,暂停等功能

package cn.xm.exam.test;

import org.apache.commons.lang.time.StopWatch;

public class test implements AInterface, BInterface {
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start(); try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} stopWatch.stop();
System.out.println(stopWatch.getStartTime());// 获取开始时间
System.out.println(stopWatch.getTime());// 获取总的执行时间--单位是毫秒
}
}

结果:

1541754863180
5001

8.以Range结尾的类主要提供一些范围的操作,包括判断某些字符,数字等是否在这个范围以内

        IntRange intRange = new IntRange(1, 5);
System.out.println(intRange.getMaximumInteger());
System.out.println(intRange.getMinimumInteger());
System.out.println(intRange.containsInteger(6));
System.out.println(intRange.containsDouble(3));

结果:

5
1
false
true

9.ArrayUtils操作数组,功能强大,可以合并,判断是否包含等操作

package cn.xm.exam.test;

import org.apache.commons.lang.ArrayUtils;

public class test implements AInterface, BInterface {
public static void main(String[] args) {
int array[] = { 1, 5, 5, 7 };
System.out.println(array); // 增加元素
array = ArrayUtils.add(array, 9);
System.out.println(ArrayUtils.toString(array)); // 删除元素
array = ArrayUtils.remove(array, 3);
System.out.println(ArrayUtils.toString(array)); // 反转数组
ArrayUtils.reverse(array);
System.out.println(ArrayUtils.toString(array)); // 查询数组索引
System.out.println(ArrayUtils.indexOf(array, 5)); // 判断数组中是否包含指定值
System.out.println(ArrayUtils.contains(array, 5)); // 合并数组
array = ArrayUtils.addAll(array, new int[] { 1, 5, 6 });
System.out.println(ArrayUtils.toString(array));
}
}

结果:

[I@3cf5b814
{1,5,5,7,9}
{1,5,5,9}
{9,5,5,1}
1
true
{9,5,5,1,1,5,6}

补充:ArrayUtils可以将包装类型的数组转变为基本类型的数组。

package cn.xm.exam.test;

import org.apache.commons.lang.ArrayUtils;

public class test {
public static void main(String[] args) {
Integer integer[] = new Integer[] { 0, 1, 2 };
System.out.println(integer.getClass()); int[] primitive = ArrayUtils.toPrimitive(integer);
System.out.println(primitive.getClass());
}
}

结果:

class [Ljava.lang.Integer;
class [I

看过JVM的知道上面第一个代表是包装类型一维数组,而且是Integer包装类。L代表引用类型,[代表一维。

          第二个代表是基本数据类型int类型的一维数组。

补充:ArrayUtils也可以判断数组是否为null或者数组大小是否为0

    /**
* <p>Checks if an array of Objects is empty or <code>null</code>.</p>
*
* @param array the array to test
* @return <code>true</code> if the array is empty or <code>null</code>
* @since 2.1
*/
public static boolean isEmpty(Object[] array) {
return array == null || array.length == 0;
}

ArrayUtils结合java.lang.reflect.Array工具类,可以满足数组的基本操作,Array的方法如下:

commons-lang3-3.2.jar中的常用工具类的使用

补充:ArrayUtils.remove()是根据下标移除,也可以移除元素:从该数组中删除第一次出现的指定元素,返回一个新的数组。

        int array[] = { 1, 5, 5, 7 };
System.out.println(ArrayUtils.toString(array)); // 删除元素
array = ArrayUtils.removeElement(array, 5);
System.out.println(ArrayUtils.toString(array));

结果:

{1,5,5,7}
{1,5,7}

上面是删除第一个出现的元素,如果需要删除所有的,可以用:

        int array[] = { 1, 5, 5, 7 };
System.out.println(ArrayUtils.toString(array)); // 删除元素
while (ArrayUtils.contains(array, 5)) {
array = ArrayUtils.removeElement(array, 5);
}
System.out.println(ArrayUtils.toString(array));

结果:

{1,5,5,7}
{1,7}

8.  反射工具类的使用

一个普通的java:

package cn.xm.exam.test.p1;

public class Person {
private String name; public static void staticMet(String t) {
System.out.println(t);
} public Person(String name) {
this.name = name;
} public String call(String string) {
System.out.println(name);
System.out.println(string);
return string;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "test [name=" + name + "]";
}
}

反射工具类操作:

package cn.xm.exam.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import org.apache.commons.lang.reflect.ConstructorUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.apache.commons.lang.reflect.MethodUtils; import cn.xm.exam.test.p1.Person; public class test { public static void main(String[] args) throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
// ConstructorUtils工具类的使用
Constructor accessibleConstructor = ConstructorUtils.getAccessibleConstructor(Person.class, String.class);
Person newInstance = (Person) accessibleConstructor.newInstance("test");
System.out.println(newInstance.getClass());
System.out.println(newInstance); // MethodUtils的使用
Method accessibleMethod = MethodUtils.getAccessibleMethod(Person.class, "call", String.class);
Object invoke = accessibleMethod.invoke(newInstance, "参数");
System.out.println(invoke);
// 调用静态方法
MethodUtils.invokeStaticMethod(Person.class, "staticMet", "静态方法"); // FieldUtils 暴力获取私有变量(第三个参数表示是否强制获取)---反射方法修改元素的值
Field field = FieldUtils.getField(Person.class, "name", true);
field.setAccessible(true);
System.out.println(field.getType());
field.set(newInstance, "修改后的值");
System.out.println(newInstance.getName());
}
}

结果:

class cn.xm.exam.test.p1.Person
test [name=test]
test
参数
参数
静态方法
class java.lang.String
修改后的值

9.  EqualsBuilder 可以用于拼接多个条件进行equals比较

例如:

        EqualsBuilder equalsBuilder = new EqualsBuilder();
Integer integer1 = new Integer(1);
Integer integer2 = new Integer(1); String string1 = "111";
String string2 = "111";
equalsBuilder.append(integer1, integer2);
equalsBuilder.append(string1, string2);
System.out.println(equalsBuilder.isEquals());

结果:

true

 查看源码:(append的时候判断两个元素是否相等,如果equals已经等于false就直接返回)

    /**
* <p>Test if two <code>Object</code>s are equal using their
* <code>equals</code> method.</p>
*
* @param lhs the left hand object
* @param rhs the right hand object
* @return EqualsBuilder - used to chain calls.
*/
public EqualsBuilder append(Object lhs, Object rhs) {
if (isEquals == false) {
return this;
}
if (lhs == rhs) {
return this;
}
if (lhs == null || rhs == null) {
this.setEquals(false);
return this;
}
Class lhsClass = lhs.getClass();
if (!lhsClass.isArray()) {
// The simple case, not an array, just test the element
isEquals = lhs.equals(rhs);
} else if (lhs.getClass() != rhs.getClass()) {
// Here when we compare different dimensions, for example: a boolean[][] to a boolean[]
this.setEquals(false);
}
// 'Switch' on type of array, to dispatch to the correct handler
// This handles multi dimensional arrays of the same depth
else if (lhs instanceof long[]) {
append((long[]) lhs, (long[]) rhs);
} else if (lhs instanceof int[]) {
append((int[]) lhs, (int[]) rhs);
} else if (lhs instanceof short[]) {
append((short[]) lhs, (short[]) rhs);
} else if (lhs instanceof char[]) {
append((char[]) lhs, (char[]) rhs);
} else if (lhs instanceof byte[]) {
append((byte[]) lhs, (byte[]) rhs);
} else if (lhs instanceof double[]) {
append((double[]) lhs, (double[]) rhs);
} else if (lhs instanceof float[]) {
append((float[]) lhs, (float[]) rhs);
} else if (lhs instanceof boolean[]) {
append((boolean[]) lhs, (boolean[]) rhs);
} else {
// Not an array of primitives
append((Object[]) lhs, (Object[]) rhs);
}
return this;
}

补充:利用EqualsBuilder和HashCodeBuilder进行重写equals方法和hasCode方法:

package cn.qlq;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder; public class User implements Cloneable { private int age;
private String name, sex; @Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
} if (obj == this) {
return true;
} if (!(obj instanceof User)) {
return false;
} final User tmpUser = (User) obj;
return new EqualsBuilder().appendSuper(super.equals(obj)).append(name, tmpUser.getName()).isEquals();
} @Override
public int hashCode() {
return new HashCodeBuilder().append(name).toHashCode();
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "User [age=" + age + ", name=" + name + ", sex=" + sex + "]";
} }

10.  RandomStringUtils可用于生成随机数和随机字符串

        // 第一个参数表示生成位数,第二个表示是否生成字母,第三个表示是否生成数字
System.out.println(RandomStringUtils.random(15, true, false)); // 长度、起始值、结束值、是否使用字母、是否生成数字
System.out.println(RandomStringUtils.random(15, 5, 129, true, false)); System.out.println(RandomStringUtils.random(22)); // 从指定字符串随机生成
System.out.println(RandomStringUtils.random(15, "abcdefgABCDEFG123456789")); // 从字母中抽取
System.out.println(RandomStringUtils.randomAlphabetic(15)); // 从数字抽取
System.out.println(RandomStringUtils.randomNumeric(15)); // ASCII between 32 and 126 =内部调用(random(count, 32, 127, false,false);)
System.out.println(RandomStringUtils.randomAscii(15));

结果:

JDibosuEOUepHtO
LXrzlRaANphURKS
ဧ큵䳵᩠K훸쟌ᚪ惥㈤ꃣ嚶爆䴄毟鰯韭;䡶ꑉ凷訩
5F5D919d77fEEA9
oTmXgAbiZWFUDRc
843164814767664
p(_xsQIp/&<Jc$Z

11.RandomUtils可用于生成一定范围内整数、float、boolean等值。最新版的commons-lang包还可以提供上限、下限

    public static void main(String[] args) {
List<Object> result = new ArrayList<>();
for (int i = 0; i < 30; i++) {
result.add(RandomUtils.nextInt());
}
System.out.println(StringUtils.join(result, ",")); result = new ArrayList<>();
for (int i = 0; i < 30; i++) {
result.add(RandomUtils.nextInt(20));
}
System.out.println(StringUtils.join(result, ",")); result = new ArrayList<>();
for (int i = 0; i < 30; i++) {
result.add(org.apache.commons.lang3.RandomUtils.nextInt(0, 10));
}
System.out.println(StringUtils.join(result, ",")); result = new ArrayList<>();
for (int i = 0; i < 30; i++) {
result.add(org.apache.commons.lang3.RandomUtils.nextLong(5L, 6L));
}
System.out.println(StringUtils.join(result, ",")); result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
result.add(org.apache.commons.lang3.RandomUtils.nextFloat(0.5F, 0.6F));
}
System.out.println(StringUtils.join(result, ","));
}

结果:

391357754,492392032,492524087,666171631,473008086,2089602614,1303315335,494646254,1863562131,182849529,207273461,1857831948,1197203156,864149196,956426242,1263147526,2070274937,931371426,478106765,838690870,723564037,100543521,319440652,1438255942,1495754097,1537242017,1161118057,534187007,957222284,553366099
2,7,5,10,3,1,19,1,19,11,7,13,10,14,9,2,10,14,1,9,8,1,1,8,3,13,9,18,4,6
2,5,7,9,5,1,3,6,7,7,3,5,3,7,3,6,8,4,2,9,8,3,6,5,9,7,1,9,9,4
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
0.52966917,0.52869964,0.58883214,0.53288007,0.5808929,0.56509304,0.5022589,0.53946894,0.5280939,0.5391899

========commons-collections包中的常用的工具类==

1. CollectionUtils工具类用于操作集合,  isEmpty () 方法最有用   (commons-collections包中的类)

package cn.xm.exam.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import org.apache.commons.collections.CollectionUtils; public class test {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2"); List<String> list1 = new ArrayList<String>();
list1.add("str1");
list1.add("str21"); // 判断是否有任何一个相同的元素
System.out.println(CollectionUtils.containsAny(list, list1)); // 求并集(自动去重)
List<String> list3 = (List<String>) CollectionUtils.union(list, list1);
System.out.println(list3); // 求交集(两个集合中都有的元素)
Collection intersection = CollectionUtils.intersection(list, list1);
System.out.println("intersection->" + intersection); // 求差集(并集去掉交集,也就是list中有list1中没有,list1中有list中没有)
Collection intersection1 = CollectionUtils.disjunction(list, list1);
System.out.println("intersection1->" + intersection1); // 获取一个同步的集合
Collection synchronizedCollection = CollectionUtils.synchronizedCollection(list); // 验证集合是否为null或者集合的大小是否为0,同理有isNouEmpty方法
List list4 = null;
List list5 = new ArrayList<>();
System.out.println(CollectionUtils.isEmpty(list4));
System.out.println(CollectionUtils.isEmpty(list5));
}
}

结果:

true
[str2, str21, str1]
intersection->[str1]
intersection1->[str2, str21]
true
true

补充:此工具类还可以向集合中加数组元素

        List<String> list = new ArrayList<>();
String s[] = { "1", "2" };
CollectionUtils.addAll(list, s);
list.add("3");
System.out.println(list);

结果:

[1, 2, 3]

2.   MapUtils工具类,isEmpty最有用(commons-collections包中的类)

  可以用于map判断null和size为0,也可以直接获取map中的值为指定类型,没有的返回null

package cn.xm.exam.test;

import java.util.HashMap;
import java.util.Map; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.NumberUtils; import ognl.MapElementsAccessor; public class test {
public static void main(String[] args) {
Map map = null;
Map map2 = new HashMap();
Map map3 = new HashMap<>();
map3.put("xxx", "xxx");
// 检验为empty可以验证null和size为0的情况
System.out.println(MapUtils.isEmpty(map));
System.out.println(MapUtils.isEmpty(map2));
System.out.println(MapUtils.isEmpty(map3)); String string = MapUtils.getString(map3, "eee");
String string2 = MapUtils.getString(map3, "xxx");
Integer integer = MapUtils.getInteger(map3, "xxx");
System.out.println("string->" + string);
System.out.println("string2->" + string2);
System.out.println("integer->" + integer);
System.out.println(integer == null);
}
}

结果:

true
true
false
INFO: Exception: java.text.ParseException: Unparseable number: "xxx"
string->null
string2->xxx
integer->null
true

 MapUtils.isEmpty根踪源码:

    public static boolean isEmpty(Map map) {
return (map == null || map.isEmpty());
}
map.isEmpty()代码查看hashmap:
    public boolean isEmpty() {
return size == 0;
}

补充:MapUtils也可以获取值作为String,获取不到取默认值:

        //获取字符串,如果获取不到可以返回一个默认值
String string3 = MapUtils.getString(map3, "eee","没有值");

查看源码:

    /**
* Looks up the given key in the given map, converting the result into
* a string, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return the value in the map as a string, or defaultValue if the
* original value is null, the map is null or the string conversion
* fails
*/
public static String getString( Map map, Object key, String defaultValue ) {
String answer = getString( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}

补充:网上总结的常见工具类的使用

一. org.apache.commons.io.IOUtils

closeQuietly:关闭一个IO流、socket、或者selector且不抛出异常,通常放在finally块
toString:转换IO流、 Uri、 byte[]为String
copy:IO流数据复制,从输入流写到输出流中,最大支持2GB
toByteArray:从输入流、URI获取byte[]
write:把字节. 字符等写入输出流
toInputStream:把字符转换为输入流
readLines:从输入流中读取多行数据,返回List<String>
copyLarge:同copy,支持2GB以上数据的复制
lineIterator:从输入流返回一个迭代器,根据参数要求读取的数据量,全部读取,如果数据不够,则失败

二. org.apache.commons.io.FileUtils

deleteDirectory:删除文件夹
readFileToString:以字符形式读取文件内容
deleteQueitly:删除文件或文件夹且不会抛出异常
copyFile:复制文件
writeStringToFile:把字符写到目标文件,如果文件不存在,则创建
forceMkdir:强制创建文件夹,如果该文件夹父级目录不存在,则创建父级
write:把字符写到指定文件中
listFiles:列举某个目录下的文件(根据过滤器)
copyDirectory:复制文件夹
forceDelete:强制删除文件

三. org.apache.commons.lang.StringUtils

isBlank:字符串是否为空 (trim后判断)
isEmpty:字符串是否为空 (不trim并判断)
equals:字符串是否相等
join:合并数组为单一字符串,可传分隔符
split:分割字符串
EMPTY:返回空字符串
trimToNull:trim后为空字符串则转换为null
replace:替换字符串

四. org.apache.http.util.EntityUtils

toString:把Entity转换为字符串
consume:确保Entity中的内容全部被消费。可以看到源码里又一次消费了Entity的内容,假如用户没有消费,那调用Entity时候将会把它消费掉
toByteArray:把Entity转换为字节流
consumeQuietly:和consume一样,但不抛异常
getContentCharset:获取内容的编码

五. org.apache.commons.lang3.StringUtils

isBlank:字符串是否为空 (trim后判断)
isEmpty:字符串是否为空 (不trim并判断)
equals:字符串是否相等
join:合并数组为单一字符串,可传分隔符
split:分割字符串
EMPTY:返回空字符串
replace:替换字符串
capitalize:首字符大写

六. org.apache.commons.io.FilenameUtils

getExtension:返回文件后缀名
getBaseName:返回文件名,不包含后缀名
getName:返回文件全名
concat:按命令行风格组合文件路径(详见方法注释)
removeExtension:删除后缀名
normalize:使路径正常化
wildcardMatch:匹配通配符
seperatorToUnix:路径分隔符改成unix系统格式的,即/
getFullPath:获取文件路径,不包括文件名
isExtension:检查文件后缀名是不是传入参数(List<String>)中的一个

七. org.springframework.util.StringUtils

hasText:检查字符串中是否包含文本
hasLength:检测字符串是否长度大于0
isEmpty:检测字符串是否为空(若传入为对象,则判断对象是否为null)
commaDelimitedStringToArray:逗号分隔的String转换为数组
collectionToDelimitedString:把集合转为CSV格式字符串
replace 替换字符串
delimitedListToStringArray:相当于split
uncapitalize:首字母小写
collectionToDelimitedCommaString:把集合转为CSV格式字符串
tokenizeToStringArray:和split基本一样,但能自动去掉空白的单词

八. org.apache.commons.lang.ArrayUtils

contains:是否包含某字符串
addAll:添加整个数组
clone:克隆一个数组
isEmpty:是否空数组
add:向数组添加元素
subarray:截取数组
indexOf:查找某个元素的下标
isEquals:比较数组是否相等
toObject:基础类型数据数组转换为对应的Object数组

九. org.apache.commons.lang.StringEscapeUtils

参考十五:
org.apache.commons.lang3.StringEscapeUtils

十. org.apache.http.client.utils.URLEncodedUtils

format:格式化参数,返回一个HTTP POST或者HTTP PUT可用application/x-www-form-urlencoded字符串
parse:把String或者URI等转换为List<NameValuePair>

十一. org.apache.commons.codec.digest.DigestUtils

md5Hex:MD5加密,返回32位字符串
sha1Hex:SHA-1加密
sha256Hex:SHA-256加密
sha512Hex:SHA-512加密
md5:MD5加密,返回16位字符串

十二. org.apache.commons.collections.CollectionUtils

isEmpty:是否为空
select:根据条件筛选集合元素
transform:根据指定方法处理集合元素,类似List的map()
filter:过滤元素,雷瑟List的filter()
find:基本和select一样
collect:和transform 差不多一样,但是返回新数组
forAllDo:调用每个元素的指定方法
isEqualCollection:判断两个集合是否一致

十三. org.apache.commons.lang3.ArrayUtils

contains:是否包含某个字符串
addAll:添加整个数组
clone:克隆一个数组
isEmpty:是否空数组
add:向数组添加元素
subarray:截取数组
indexOf:查找某个元素的下标
isEquals:比较数组是否相等
toObject:基础类型数据数组转换为对应的Object数组

十四. org.apache.commons.beanutils.PropertyUtils

getProperty:获取对象属性值
setProperty:设置对象属性值
getPropertyDiscriptor:获取属性描述器
isReadable:检查属性是否可访问
copyProperties:复制属性值,从一个对象到另一个对象
getPropertyDiscriptors:获取所有属性描述器
isWriteable:检查属性是否可写
getPropertyType:获取对象属性类型

十五. org.apache.commons.lang3.StringEscapeUtils

unescapeHtml4:转义html
escapeHtml4:反转义html
escapeXml:转义xml
unescapeXml:反转义xml
escapeJava:转义unicode编码
escapeEcmaScript:转义EcmaScript字符
unescapeJava:反转义unicode编码
escapeJson:转义json字符
escapeXml10:转义Xml10
这个现在已经废弃了,建议使用commons-text包里面的方法。

十六. org.apache.commons.beanutils.BeanUtils

copyPeoperties:复制属性值,从一个对象到另一个对象
getProperty:获取对象属性值
setProperty:设置对象属性值
populate:根据Map给属性复制
copyPeoperty:复制单个值,从一个对象到另一个对象
cloneBean:克隆bean实例

  现在你只要了解了以上16种最流行的工具类方法,你就不必要再自己写工具类了,不必重复造*。大部分工具类方法通过其名字就能明白其用途,如果不清楚的,可以看下别人是怎么用的,或者去网上查询其用法。
  另外,工具类,根据阿里开发手册,包名如果要使用util不能带s,工具类命名为 XxxUtils(参考spring命名)