ObjectUtil

Introduction

In our daily use, there are some methods that are common to all objects. These methods do not distinguish between different types of objects. Hutool has encapsulated these methods as ObjectUtil.

Methods

Default Value

With the help of lambda expressions, ObjectUtil can determine whether a given value is null. If it is not null, it can perform specific logic.

final String dateStr = null;

// If dateStr is null, `Instant.now()` will be called. If it is not null, `DateUtil.parse` will be executed.
Instant result1 = ObjectUtil.defaultIfNull(dateStr,
 () -> DateUtil.parse(dateStr, DatePattern.NORM_DATETIME_PATTERN).toInstant(), Instant.now());

ObjectUtil.equal

Compare whether two objects are equal. They are considered equal if one of the following conditions is met:

  1. obj1 == null && obj2 == null
  2. obj1.equals(obj2)
Object a = null;
Object b = null;

// true
ObjectUtil.equals(a, b);

ObjectUtil.length

Calculate the length of an object. If it is a string, call its length method. If it is a collection, call its size method. If it is an array, call its length attribute. For other iterable objects, traverse to calculate the length.

Supported types include:

  • CharSequence
  • Collection
  • Map
  • Iterator
  • Enumeration
  • Array
int[] array = new int[]{1,2,3,4,5};

// 5
int length = ObjectUtil.length(array);

Map<String, String> map = new HashMap<>();
map.put("a", "a1");
map.put("b", "b1");
map.put("c", "c1");

// 3
length = ObjectUtil.length(map);

ObjectUtil.contains

Whether an object contains a specific element.

Supported object types include:

  • String
  • Collection
  • Map
  • Iterator
  • Enumeration
  • Array
int[] array = new int[]{1,2,3,4,5};

// true
final boolean contains = ObjectUtil.contains(array, 1);

Checking for nullness

  • ObjectUtil.isNull
  • ObjectUtil.isNotNull

Note: This method cannot determine if a field within an object is empty. To check if all fields within a Bean object are empty, use BeanUtil.isEmpty.

Cloning

  • ObjectUtil.clone Clones an object, calling the clone method if the object implements Cloneable, performs a deep clone if it implements Serializable, otherwise returns null.
class Obj extends CloneSupport<Obj> {
 public String doSomeThing() {
 return "OK";
 }
}
Obj obj = new Obj();
Obj obj2 = ObjectUtil.clone(obj);

// OK
obj2.doSomeThing();
  • ObjectUtil.cloneIfPossible Returns a cloned object if cloning is successful, otherwise returns the original object.

  • ObjectUtil.cloneByStream Clones an object by serializing and copying the serialized stream, the object must implement Serializable interface.

Serialization and Deserialization

  • serialize Serialization using JDK serialization.
  • deserialize Deserialization using JDK serialization.

Checking for basic types

ObjectUtil.isBasicType checks whether a type is a basic type, including wrapper types and primitive types.

Wrapper types:

  • Boolean
  • Byte
  • Character
  • Double
  • Float
  • Integer
  • Long
  • Short

Primitive types:

  • boolean
  • byte
  • char
  • double
  • float
  • int
  • long
  • short
int a = 1;

// true
final boolean basicType = ObjectUtil.isBasicType(a);