lshboo 发表于 2017-1-9 12:08:13

Apache的common下常用的工具类

  场景:
  1.使用list的时候需要判断是否为null,同时是否为空
  2.使用String的时候我们需要判断是否为null,同时是否为空
  3.随机数,随机字符串
  代码:

String t=null;
if(t==null ||"".equals(t)){
//do
}
   list的操作
  

List<String> list=null;
if(list==null ||list.isEmpty()){
//do
}
   
  方案:
  我们可以采用apache下的common进行常用的操作

import org.apache.commons.collections.*;
import org.apache.commons.lang.*;
   对字符串的操作:

private static void stringTest() {
String str = null;
System.out.println(StringUtils.isEmpty(str));
str = "";
System.out.println(StringUtils.isEmpty(str));
System.out.println(StringUtils.trim(null));
System.out.println(StringUtils.split(null, "&|"));
System.out.println(ArrayUtils.toString(StringUtils.split("id=4&type=1", "&|=")));
}
   对collection的操作:

private static void collectionTest() {
System.out.println(CollectionUtils.isEmpty(null));
System.out.println(CollectionUtils.isEmpty(ListUtils.EMPTY_LIST));
}
   对number的操作:

private static void numberTest() {
System.out.println(NumberUtils.isDigits(null));
System.out.println(NumberUtils.isDigits("00052"));
System.out.println(NumberUtils.isDigits("000.52"));
System.out.println(NumberUtils.isNumber("0.0052"));
System.out.println(NumberUtils.isNumber(null));
}
   对random的操作:

private static void randomTest() {
System.out.println(RandomUtils.nextInt(10));
System.out.println(RandomUtils.nextBoolean());
System.out.println(RandomUtils.nextLong());
}
   对randomstring的操作:

private static void randomStringTest() {
System.out.println(RandomStringUtils.randomAlphabetic(100));
System.out.println(RandomStringUtils.random(10, "abc"));
}
   
页: [1]
查看完整版本: Apache的common下常用的工具类