设为首页 收藏本站
查看: 985|回复: 0

[经验分享] Apache Commons Lang

[复制链接]

尚未签到

发表于 2016-12-30 10:14:38 | 显示全部楼层 |阅读模式
ArrayUtils
    public class TestMain {  
public static void main(String[] args) {  
int[] nums1 = { 1, 2, 3, 4, 5, 6 };  
// 通过常量创建新数组  
int[] nums2 = ArrayUtils.EMPTY_INT_ARRAY;  
// 比较两个数组是否相等  
ArrayUtils.isEquals(nums1, nums2);  
// 输出数组,第二参数为数组为空时代替输出  
ArrayUtils.toString(nums1, "array is null");  
// 克隆新数组,注意此拷贝为深拷贝  
int[] nums3 = ArrayUtils.clone(nums1);  
// 截取数组  
ArrayUtils.subarray(nums1, 1, 2);  
// 判断两个数组长度是否相等  
ArrayUtils.isSameLength(nums1, nums2);  
// 判断两个数组类型是否相等,注意int和Integer比较时不相等  
ArrayUtils.isSameType(nums1, nums2);  
// 反转数组  
ArrayUtils.reverse(nums1);  
// 查找数组元素位置  
ArrayUtils.indexOf(nums1, 5);  
// 查找数组元素最后出现位置  
ArrayUtils.lastIndexOf(nums1, 4);  
// 查找元素是否存在数组中  
ArrayUtils.contains(nums1, 2);  
// 将基本数组类型转为包装类型  
Integer[] nums4 = ArrayUtils.toObject(nums1);  
// 判断是否为空,length=0或null都属于  
ArrayUtils.isEmpty(nums1);  
// 并集操作,合并数组  
ArrayUtils.addAll(nums1, nums2);  
// 增加元素,在下标5中插入10,注意此处返回是新数组  
ArrayUtils.add(nums1, 5, 1111);  
// 删除指定位置元素,注意返回新数组,删除元素后面的元素会前移,保持数组有序  
ArrayUtils.remove(nums1, 5);  
// 删除数组中值为10的元素,以值计算不以下标  
ArrayUtils.removeElement(nums1, 10);  
}  
}  

ClassUtils
    public class TestMain {  
public static void main(String[] args) {  
// 获取Test类所有实现的接口  
ClassUtils.getAllInterfaces(Test.class);  
// 获取Test类所有父类  
ClassUtils.getAllSuperclasses(Test.class);  
// 获取Test类所在的包名  
ClassUtils.getPackageName(Test.class);  
// 获取Test类简单类名  
ClassUtils.getShortClassName(Test.class);  
// 判断是否可以转型  
ClassUtils.isAssignable(Test.class, Object.class);  
// 判断是否有内部类  
ClassUtils.isInnerClass(Test.class);  
}  
}  

ConstructorUtils
    public class TestMain {  
public static void main(String[] args) {  
// 获取参数为String的构造函数  
ConstructorUtils.getAccessibleConstructor(Test.class, String.class);  
// 执行参数为String的构造函数  
Test test = (Test) ConstructorUtils.invokeConstructor(Test.class,  
"Hello");  
}  
}  

MethodUtils

public static void main(String[] args) throws NoSuchMethodException,  
IllegalAccessException, InvocationTargetException {  
// 调用无参方法  
Test test = new Test();  
MethodUtils.invokeMethod(test, "publicHello", null);  
// 调用一参方法  
MethodUtils.invokeMethod(test, "publicHello", "Hello");  
// 调用多参方法  
MethodUtils.invokeMethod(test, "publicHello", new Object[] { "100",  
new Integer(10) });  
// 调用静态方法  
MethodUtils.invokeStaticMethod(Test.class, "staticHello", null);  
}  

FieldUtils
    public class TestMain {  
public static void main(String[] args) throws IllegalAccessException {  
Test test = new Test("1", "Ray", "hello");  
// 以下两个方法完全一样,都能获取共有或私有变量,因为第三个参数都设置了不检查  
FieldUtils.getDeclaredField(Test.class, "username", true);  
FieldUtils.getField(Test.class, "username", true);  
// 读取私有或公共变量的值  
FieldUtils.readField(test, "username", true);  
// 读取静态变量  
FieldUtils.readStaticField(Test.class, "STATIC_FIELD");  
// 写入私有或共有变量  
FieldUtils.writeField(test, "username", "RayRay", true);  
// 写入静态变量  
FieldUtils.writeStaticField(Test.class, "STATIC_FIELD", "static_value");  
}  
}  


NumberUtils  and  RandomUtils

    public class TestMain {  
public static void main(String[] args) throws IllegalAccessException {  
String str = "12.7";  
/*
* org.apache.commons.lang.NumberUtils已经被弃用,
* 注意要引入org.apache.commons.lang.math.NumberUtils
*/  
// 判断字符串是否为整数  
NumberUtils.isDigits(str);  
// 判断字符串是否为数字  
NumberUtils.isNumber(str);  
// 获取参数中最大的值,支持传入数组  
NumberUtils.max(10, 20, 30);  
// 获取参数中最小的值,支持传入数组  
NumberUtils.min(10, 20, 30);  
// 将字符串转换为int类型,支持float,long,short等数值类型  
NumberUtils.toInt(str);  
// 通过字符串创建BigDecimal类型 ,支持int,float,long等数值  
NumberUtils.createBigDecimal(str);  

/*
* RandomUtils帮助我们产生随机数,不止是数字类型 ,  
* 连boolean类型都可以通过RandomUtils产生
*/  
RandomUtils.nextBoolean();  
RandomUtils.nextDouble();  
RandomUtils.nextLong();  
// 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数  
RandomUtils.nextInt(1000);  
}  
}  

StringUtils
    public class TestMain {  
public static void main(String[] args) throws IllegalAccessException {  
String str = "Hello World";  
/*
* 由于StringUtils拥有100+的方法,笔者不逐一列举用法,
* 只列举笔者认为常用的或笔者使用过的
*/  
// isEmpty和isBlank的区别在于isEmpty不会忽略空格,  
// " "<--对于这样的字符串isEmpty会认为不是空,  
// 而isBlank会认为是空,isBlank更常用  
StringUtils.isEmpty(str);  
StringUtils.isNotEmpty(str);  
StringUtils.isBlank(str);  
StringUtils.isNotBlank(str);  

// strip      --> 去除两端的aa  
// stripStart --> 去除开始位置的hell  
// stripEnd   --> 去除结尾位置的orld  
StringUtils.strip(str, "aa");  
StringUtils.stripStart(str, "hell");  
StringUtils.stripEnd(str, "orld");  

// 返回字符串在第三次出现A的位置  
StringUtils.ordinalIndexOf(str, "A", 3);  

// 获取str 开始为hello结尾为orld中间的字符串  
// 注意此方法返回字符串      -->substringBetween  
// 注意此方法返回字符串数组(多了个s) --> substringsBetween  
StringUtils.substringBetween(str, "hell", "orld");  
StringUtils.substringsBetween(str, "hell", "orld");  

// 重复字符串,第二种重载形式为在重复中用hahah拼接  
StringUtils.repeat(str, 3);  
StringUtils.repeat(str, "hahah", 2);  

// 统计参数2在字符串中出现的次数  
StringUtils.countMatches(str, "l");  

// 判断字符串是否全小写或大写  
StringUtils.isAllLowerCase(str);  
StringUtils.isAllUpperCase(str);  

// isAlpha        --> 全部由字母组成返回true  
// isNumeric      -->全部由数字组成返回true  
// isAlphanumeric -->全部由字母或数字组成返回true  
// isAlphaSpace   -->全部由字母或空格组成返回true  
// isWhitespace   -->全部由空格组成返回true  
StringUtils.isAlpha(str);  
StringUtils.isNumeric(str);  
StringUtils.isAlphanumeric(str);  
StringUtils.isAlphaSpace(str);  
StringUtils.isWhitespace(str);  

// 缩进字符串,第二参数最低为 4,要包含...  
// 现在Hello World输出为H...  
StringUtils.abbreviate(str, 4);  

// 首字母大写或小写  
StringUtils.capitalize(str);  
StringUtils.uncapitalize(str);  

// 将字符串数组转变为一个字符串,通过","拼接,支持传入iterator和collection  
StringUtils.join(new String[] { "Hello", "World" }, ",");  

/*
* 经常性要把后台的字符串传递到前提作为html代码进行解释,
* 可以使用以下方法进行转换,以下方法输出为
* <p>Hello</p>
*/  
StringEscapeUtils.escapeHtml("Hello  
");  
}  
}  

DateUtils and DateFormatUtils
    public class TestMain {  
public static void main(String[] args) throws IllegalAccessException {  
Date day1 = new Date();  
/*
* 由于Aache的DateUtils和DateFormatUtils并没有Joda强大,
*  所以在这里只作简单的示例
*/  
// 增加一天  
DateUtils.addDays(day1, 1);  
// 减少一年  
DateUtils.addYears(day1, -1);  
// 格式化时间,第三参数为国际化,表示按美国时间显示  
DateFormatUtils.format(day1, "yyyy-MM-dd", Locale.UK);  
}  
}  

来源于:http://ray-yui.iyunv.com/blog/1958319

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-321492-1-1.html 上篇帖子: apache整合svn 下篇帖子: Apache Shiro 授权
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表