1、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);
}
}
2、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);
}
}
3、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");
}
}
4、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);
}
5、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");
}
}
6、NumberUtils
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);
}
}
7、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
");
}
}
8、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);
}
}