博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
commons-lang3之ArrayUtils源码解析
阅读量:4203 次
发布时间:2019-05-26

本文共 2088 字,大约阅读时间需要 6 分钟。

源码版本

commons-lang3-3.1.jar

源码介绍

功能介绍

Operations on arrays, primitive arrays (like {@code int[]}) andprimitive wrapper arrays (like {@code Integer[]}).This class tries to handle {@code null} input gracefully.An exception will not be thrown for a {@code null}array input. However, an Object array that contains a {@code null}element may throw an exception. Each method documents its behaviour.

To map

public static Map
toMap(final Object[] array) {
if (array == null) {
return null; } // 定义map的初始容量,乘以1.5考虑的是加载因子 final Map
map = new HashMap<>((int) (array.length * 1.5)); for (int i = 0; i < array.length; i++) {
final Object object = array[i]; if (object instanceof Map.Entry
) {
final Map.Entry
entry = (Map.Entry
) object; map.put(entry.getKey(), entry.getValue()); } else if (object instanceof Object[]) {
final Object[] entry = (Object[]) object; if (entry.length < 2) {
throw new IllegalArgumentException("Array element " + i + ", '" + object + "', has a length less than 2"); } map.put(entry[0], entry[1]); } else {
throw new IllegalArgumentException("Array element " + i + ", '" + object + "', is neither of type Map.Entry nor an Array"); } } return map;}

Generic array

// JDK5 可变参数(Varargs)public static 
T[] toArray(final T... items) {
return items;}

isEmpty

public static boolean isEmpty(final Object[] array) {
return getLength(array) == 0;}public static int getLength(final Object array) {
if (array == null) {
return 0; } return Array.getLength(array);}

Reverse

public static void reverse(final Object[] array) {
if (array == null) {
return; } reverse(array, 0, array.length);}public static void reverse(final long[] array) {
if (array == null) {
return; } reverse(array, 0, array.length);}

转载地址:http://invli.baihongyu.com/

你可能感兴趣的文章