JAVA工具类ObjectUtils.Null

it2025-09-09  9

一、ObjectUtils.Null类作为一个空占位符,其中null具有另外一个含义。

例如在HashMap中的HashMap.get(java.lang.Object) 方法返回null如果这个Map包含null(也就是有一个key对应的值为null),或者这里就不存在对应的key,这个空的占位符可以区分这两种情况。

如下代码HashMap中存储有一个字段username值为null

Map<String, Object> map = new HashMap<String, Object>(); map.put("username", null); System.out.println(map.get("username")); System.out.println(map.get("password"));这里写代码片

输出结果是:

null null

根据如上的结果我们是没有办法区分Map中是否存在字段username、password的,要想区分Map中存在的字段username值为null和不存在字段password的情况我们可以使用空占位符的方法:

map.put("username", ObjectUtils.NULL); System.out.println(ObjectUtils.NULL == map.get("username")); System.out.println(ObjectUtils.NULL == map.get("username1")); System.out.println(null == map.get("username1"));

输出的结果是:

true false true
最新回复(0)