Apache Common

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
Email 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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容