概述
源码版本
commons-lang3-3.1.jar
源码介绍
功能介绍
Operations on arrays, primitive arrays (like {@code int[]}) and
primitive 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<Object, Object> toMap(final Object[] array) {
if (array == null) {
return null;
}
// 定义map的初始容量,乘以1.5考虑的是加载因子
final Map<Object, Object> 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> 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);
}
最后
以上就是重要月饼为你收集整理的commons-lang3之ArrayUtils源码解析的全部内容,希望文章能够帮你解决commons-lang3之ArrayUtils源码解析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复