commons工具包很多开源组织都有提供,例如google,spring,apache都有各自的工具包,有众多的选择,但最终的目的只是为了方便我们程序的开发和维护,简化我们编写一些常用的逻辑,提升我们开发的效率
概述
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表
Components | Description | Latest Version | Released |
---|---|---|---|
BCEL | Byte Code Engineering Library - analyze, create, and manipulate Java class files | 6.2 | 2017-11-08 |
BeanUtils | Easy-to-use wrappers around the Java reflection and introspection APIs. | 1.9.3 | 2016-09-26 |
BSF | Bean Scripting Framework - interface to scripting languages, including JSR-223 | 3.1 | 2010-06-24 |
Chain | Chain of Responsibility pattern implemention. | 1.2 | 2008-06-02 |
CLI | Command Line arguments parser. | 1.4 | 2017-03-09 |
Codec | General encoding/decoding algorithms (for example phonetic, base64, URL). | 1.11 | 2017-10-20 |
Collections | Extends or augments the Java Collections Framework. | 4.2 | 2018-07-11 |
Compress | Defines an API for working with tar, zip and bzip2 files. | 1.18 | 2018-08-16 |
Configuration | Reading of configuration/preferences files in various formats. | 2.4 | 2018-10-29 |
Crypto | A cryptographic library optimized with AES-NI wrapping Openssl or JCE algorithm implementations. | 1.0.0 | 2016-07-22 |
CSV | Component for reading and writing comma separated value files. | 1.6 | 2018-09-25 |
Daemon | Alternative invocation mechanism for unix-daemon-like java code. | 1.0.15 | 2013-04-03 |
DBCP | Database connection pooling services. | 2.5.0 | 2018-07-16 |
DbUtils | JDBC helper library. | 1.7 | 2017-07-20 |
Digester | XML-to-Java-object mapping utility. | 3.2 | 2011-12-13 |
Library for sending e-mail from Java. | 1.5 | 2017-08-01 | |
Exec | API for dealing with external process execution and environment management in Java. | 1.3 | 2014-11-06 |
FileUpload | File upload capability for your servlets and web applications. | 1.3.3 | 2017-06-13 |
Functor | A functor is a function that can be manipulated as an object, or an object representing a single, generic function. | 1.0 | 2011-??-?? |
Geometry | Space and coordinates. | 1.0 | 2018-??-?? |
Imaging (previously called Sanselan) | A pure-Java image library. | 0.97-incubator | 2009-02-20 |
IO | Collection of I/O utilities. | 2.6 | 2017-10-15 |
JCI | Java Compiler Interface | 1.1 | 2013-10-14 |
JCS | Java Caching System | 2.2,1 | 2018-08-23 |
Jelly | XML based scripting and processing engine. | 1.0.1 | 2017-09-27 |
Jexl | Expression language which extends the Expression Language of the JSTL. | 3.1 | 2017-04-14 |
JXPath | Utilities for manipulating Java Beans using the XPath syntax. | 1.3 | 2008-08-14 |
Lang | Provides extra functionality for classes in java.lang. | 3.8.1 | 2018-09-23 |
Logging | Wrapper around a variety of logging API implementations. | 1.2 | 2014-07-11 |
Math | Lightweight, self-contained mathematics and statistics components. | 3.5 | 2015-04-17 |
Net | Collection of network utilities and protocol implementations. | 3.6 | 2017-02-15 |
Numbers | Number types (complex, quaternion, fraction) and utilities (arrays, combinatorics). | 1.0 | 2017-??-?? |
OGNL | An Object-Graph Navigation Language | 4.0 | 2013-??-?? |
Pool | Generic object pooling component. | 2.6.0 | 2018-07-06 |
Proxy | Library for creating dynamic proxies. | 1.0 | 2008-02-28 |
RDF | Common implementation of RDF 1.1 that could be implemented by systems on the JVM. | 0.3.0-incubating | 2016-11-15 |
RNG | Implementations of random numbers generators. | 1.1 | 2018-08-15 |
SCXML | An implementation of the State Chart XML specification aimed at creating and maintaining a Java SCXML engine.It is capable of executing a state machine defined using a SCXML document, and abstracts out the environment interfaces. | 0.9 | 2008-12-01 |
Statistics | Statistics. | 0.1 | ????-??-?? |
Text | Apache Commons Text is a library focused on algorithms working on strings. | 1.6 | 2018-10-16 |
Validator | Framework to define validators and validation rules in an xml file. | 1.6 | 2017-02-21 |
VFS | Virtual File System component for treating files, FTP, SMB, ZIP and such like as a single logical file system. | 2.2 | 2017-10-06 |
Weaver | Provides an easy way to enhance (weave) compiled bytecode. | 2.0 | 2018-09-07 |
ArrayUtils
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);
反射相关(建议使用BeanUtils)
--- ClassUtils
// 获取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
// 获取参数为String的构造函数
ConstructorUtils.getAccessibleConstructor(Test.class, String.class);
// 执行参数为String的构造函数
Test test = (Test) ConstructorUtils.invokeConstructor(Test.class,
"Hello");
----- MethodUtils
// 调用无参方法
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
// 以下两个方法完全一样,都能获取共有或私有变量,因为第三个参数都设置了不检查
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");
数字操作与转换
/*
* 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);
字符串
好多方法
日期
/*
* 由于Aache的DateUtils和DateFormatUtils并没有Joda强大
*/
// 增加一天
DateUtils.addDays(day1, 1);
// 减少一年
DateUtils.addYears(day1, -1);
// 格式化时间,第三参数为国际化,表示按美国时间显示
DateFormatUtils.format(day1, "yyyy-MM-dd", Locale.UK);
Ref:
https://blog.csdn.net/u013510614/article/details/50481000
网友评论