public final class ObjectUtil extends Object
Object
工具类.
ObjectUtils.equals(Object, Object)
支持两个值都是null的情况Objects.equals(Object, Object)
(since jdk1.7) 也支持两个值都是null的情况
ObjectUtils
,
Objects
Modifier and Type | Method and Description |
---|---|
static <T> T |
defaultIfNullOrEmpty(T object,
T defaultValue)
如果
object 是null或者empty,返回默认值 defaultValue . |
static boolean |
isArray(Object object)
判断指定的对象
object 是否是数组. |
static boolean |
isPrimitiveArray(Object object)
判断指定的对象
object 是否是原生类型数组. |
public static <T> T defaultIfNullOrEmpty(T object, T defaultValue)
object
是null或者empty,返回默认值 defaultValue
.
ObjectUtil.defaultIfNullOrEmpty(null, null) = null ObjectUtil.defaultIfNullOrEmpty(null, "") = "" ObjectUtil.defaultIfNullOrEmpty(null, "zz") = "zz" ObjectUtil.defaultIfNullOrEmpty("abc", *) = "abc" ObjectUtil.defaultIfNullOrEmpty(Boolean.TRUE, *) = Boolean.TRUE
- 使用该方法,可以简化你的代码
- 如果使用 import static 的特性,代码会更加简洁
- 如果你只需要判断 null的场景,你可以使用
ObjectUtils.defaultIfNull(Object, Object)
if (isNotNullOrEmpty(defaultReturnResult.getReturnObject())){ return (String) defaultReturnResult.getReturnObject(); }else{ return "redirect:/"; }可以重构成:return ObjectUtil.defaultIfNullOrEmpty((String) defaultReturnResult.getReturnObject(), "redirect:/");
private void putItemToMap(Map可以重构成:<String, List<Item>>
map,String categoryName,Item item){ List<Item>
itemList = map.get(categoryName); if (isNullOrEmpty(itemList)){ itemList = new ArrayList<>
(); } itemList.add(item); map.put(categoryName, itemList); }private void putItemToMap(Map当然对于上面的case,你还可以直接调用<String, List<Item>>
map,String categoryName,Item item){ List<Item>
itemList = ObjectUtil.defaultIfNullOrEmpty(map.get(categoryName), new ArrayList<Item>
()); itemList.add(item); map.put(categoryName, itemList); }MapUtil.putMultiValue(java.util.Map, Object, Object)
T
- the type of the objectobject
- the Object
to test, 可以是 null
or emptydefaultValue
- the default value to return, 可以是 null
or emptyobject
是null或者empty,返回 defaultValue
,否则返回 object
ObjectUtils.defaultIfNull(Object, Object)
,
ListUtils.defaultIfNull(java.util.List, java.util.List)
public static boolean isArray(Object object)
object
是否是数组.
- 支持判断原始类型数组
primitive
和包装类型数组
int[] i = {}; ObjectUtil.isArray(i); =true ObjectUtil.isArray(new int[] { 1, 2, 3 }); =true ObjectUtil.isArray(new Integer[0]); =true ObjectUtil.isArray(new String[0]); =true
instanceof
和 Class.isArray()
的区别:通常使用
instanceof
操作符去判断一个对象object
是否是数组array
.
在JVM层次,instanceof
操作符 translates to a specific "instanceof" byte code, which is highly optimized in most JVM implementations.
而反射的方法(getClass().isArray()) is compiled to two separate "invokevirtual" instructions.
The more generic optimizations applied by the JVM to these may not be as fast as the hand-tuned optimizations inherent in the "instanceof" instruction.
有两种特殊情况: null references 和 primitive arrays.
instanceof
getClass().isArray()
null reference false NullPointerException 原始类型数组primitive array false true
object
- the objectobject
是null,抛出 NullPointerException
public static boolean isPrimitiveArray(Object object)
object
是否是原生类型数组.
ObjectUtil.isPrimitiveArray(1) = false ObjectUtil.isPrimitiveArray(1L) = false ObjectUtil.isPrimitiveArray("1") = false ObjectUtil.isPrimitiveArray(new int[] {}) = true ObjectUtil.isPrimitiveArray(new int[] { 1, 2 }) = true ObjectUtil.isPrimitiveArray(new byte[] { 1, 2 }) = true ObjectUtil.isPrimitiveArray(new String[] { "1", "2" }) = false
object
- the objectobject
是null,抛出 NullPointerException
Copyright © 2008-2019 by feilong