0
# Object Operations
1
2
Object comparison, validation, cloning, and manipulation utilities through the ObjectUtil class.
3
4
## Capabilities
5
6
### Object Comparison and Equality
7
8
Compare objects with null-safe operations and special handling for different types.
9
10
```java { .api }
11
/**
12
* Check if two objects are equal
13
* @param obj1 first object
14
* @param obj2 second object
15
* @return true if objects are equal
16
*/
17
public static boolean equals(Object obj1, Object obj2);
18
19
/**
20
* Check if two objects are equal (alias for equals)
21
* @param obj1 first object
22
* @param obj2 second object
23
* @return true if objects are equal
24
*/
25
public static boolean equal(Object obj1, Object obj2);
26
27
/**
28
* Check if two objects are not equal
29
* @param obj1 first object
30
* @param obj2 second object
31
* @return true if objects are not equal
32
*/
33
public static boolean notEqual(Object obj1, Object obj2);
34
```
35
36
**Usage Examples:**
37
38
```java
39
import cn.hutool.core.util.ObjectUtil;
40
import java.math.BigDecimal;
41
42
// Basic equality
43
boolean same1 = ObjectUtil.equals("hello", "hello"); // true
44
boolean same2 = ObjectUtil.equals(null, null); // true
45
boolean diff = ObjectUtil.notEqual("hello", "world"); // true
46
47
// BigDecimal special handling
48
BigDecimal bd1 = new BigDecimal("1.0");
49
BigDecimal bd2 = new BigDecimal("1.00");
50
boolean bdEqual = ObjectUtil.equals(bd1, bd2); // true (uses compareTo)
51
```
52
53
### Object Validation and Null Checking
54
55
Validate objects for null, empty, and other conditions.
56
57
```java { .api }
58
/**
59
* Check if object is null
60
* @param obj object to check
61
* @return true if object is null
62
*/
63
public static boolean isNull(Object obj);
64
65
/**
66
* Check if object is not null
67
* @param obj object to check
68
* @return true if object is not null
69
*/
70
public static boolean isNotNull(Object obj);
71
72
/**
73
* Check if object is empty
74
* @param obj object to check
75
* @return true if object is null, empty string, empty collection, or empty array
76
*/
77
public static boolean isEmpty(Object obj);
78
79
/**
80
* Check if object is not empty
81
* @param obj object to check
82
* @return true if object is not empty
83
*/
84
public static boolean isNotEmpty(Object obj);
85
86
/**
87
* Get length of object (string, collection, array, etc.)
88
* @param obj object to measure
89
* @return length of object, or 0 if null
90
*/
91
public static int length(Object obj);
92
93
/**
94
* Check if object contains element
95
* @param obj container object (array, collection, string)
96
* @param element element to find
97
* @return true if container contains element
98
*/
99
public static boolean contains(Object obj, Object element);
100
```
101
102
**Usage Examples:**
103
104
```java
105
import cn.hutool.core.util.ObjectUtil;
106
import java.util.Arrays;
107
import java.util.List;
108
109
// Null checking
110
String str = null;
111
boolean isNull = ObjectUtil.isNull(str); // true
112
boolean isNotNull = ObjectUtil.isNotNull("hello"); // true
113
114
// Empty checking
115
boolean emptyStr = ObjectUtil.isEmpty(""); // true
116
boolean emptyList = ObjectUtil.isEmpty(Arrays.asList()); // true
117
boolean notEmptyArray = ObjectUtil.isNotEmpty(new int[]{1, 2, 3}); // true
118
119
// Length calculation
120
int strLength = ObjectUtil.length("hello"); // 5
121
int arrayLength = ObjectUtil.length(new String[]{"a", "b"}); // 2
122
int nullLength = ObjectUtil.length(null); // 0
123
124
// Contains checking
125
boolean hasElement = ObjectUtil.contains(Arrays.asList("a", "b", "c"), "b"); // true
126
boolean hasChar = ObjectUtil.contains("hello", 'e'); // true
127
```
128
129
### Default Value Handling
130
131
Provide default values when objects are null, empty, or blank.
132
133
```java { .api }
134
/**
135
* Return default value if object is null
136
* @param object object to check
137
* @param defaultValue default value if null
138
* @return object if not null, otherwise defaultValue
139
*/
140
public static <T> T defaultIfNull(T object, T defaultValue);
141
142
/**
143
* Return default value using supplier if object is null
144
* @param source object to check
145
* @param defaultValueSupplier supplier for default value
146
* @return object if not null, otherwise supplied default
147
*/
148
public static <T> T defaultIfNull(T source, Supplier<? extends T> defaultValueSupplier);
149
150
/**
151
* Return default value using function if object is null
152
* @param source object to check
153
* @param defaultValueSupplier function to generate default value
154
* @return object if not null, otherwise generated default
155
*/
156
public static <T> T defaultIfNull(T source, Function<T, ? extends T> defaultValueSupplier);
157
158
/**
159
* Return default value if string is empty
160
* @param str string to check
161
* @param defaultValue default value if empty
162
* @return string if not empty, otherwise defaultValue
163
*/
164
public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultValue);
165
166
/**
167
* Return default value if string is blank (empty or whitespace only)
168
* @param str string to check
169
* @param defaultValue default value if blank
170
* @return string if not blank, otherwise defaultValue
171
*/
172
public static <T extends CharSequence> T defaultIfBlank(T str, T defaultValue);
173
```
174
175
**Usage Examples:**
176
177
```java
178
import cn.hutool.core.util.ObjectUtil;
179
import java.util.function.Supplier;
180
181
// Default for null
182
String name = null;
183
String safeName = ObjectUtil.defaultIfNull(name, "Unknown"); // "Unknown"
184
185
// Default with supplier (lazy evaluation)
186
String config = ObjectUtil.defaultIfNull(null, () -> "default-config"); // "default-config"
187
188
// Default for empty strings
189
String emptyStr = "";
190
String nonEmpty = ObjectUtil.defaultIfEmpty(emptyStr, "default"); // "default"
191
192
// Default for blank strings
193
String blankStr = " ";
194
String nonBlank = ObjectUtil.defaultIfBlank(blankStr, "default"); // "default"
195
```
196
197
### Object Conversion and Transformation
198
199
Handle conversions with default values and transformations.
200
201
```java { .api }
202
/**
203
* Handle object with function, return default if null
204
* @param source source object
205
* @param handle function to process non-null object
206
* @param defaultValue default value if source is null
207
* @return processed result or default value
208
*/
209
public static <T, R> T defaultIfNull(R source, Function<R, ? extends T> handle, T defaultValue);
210
211
/**
212
* Handle string with function, return default if empty
213
* @param str string to process
214
* @param handle function to process non-empty string
215
* @param defaultValue default value if string is empty
216
* @return processed result or default value
217
*/
218
public static <T> T defaultIfEmpty(String str, Function<CharSequence, ? extends T> handle, T defaultValue);
219
```
220
221
### Object Cloning
222
223
Clone objects using various methods with fallback strategies.
224
225
```java { .api }
226
/**
227
* Clone object if it implements Cloneable
228
* @param obj object to clone
229
* @return cloned object
230
*/
231
public static <T> T clone(T obj);
232
233
/**
234
* Clone object if possible, otherwise return original
235
* @param obj object to clone
236
* @return cloned object or original if cloning fails
237
*/
238
public static <T> T cloneIfPossible(T obj);
239
240
/**
241
* Clone object using serialization
242
* @param obj object to clone (must be Serializable)
243
* @return deep cloned object
244
*/
245
public static <T> T cloneByStream(T obj);
246
```
247
248
**Usage Examples:**
249
250
```java
251
import cn.hutool.core.util.ObjectUtil;
252
import java.util.ArrayList;
253
import java.util.List;
254
255
// Clone collections
256
List<String> original = new ArrayList<>();
257
original.add("item1");
258
original.add("item2");
259
260
List<String> cloned = ObjectUtil.clone(original);
261
// Modify cloned without affecting original
262
263
// Safe cloning (won't throw exception)
264
String str = "hello";
265
String clonedStr = ObjectUtil.cloneIfPossible(str); // Returns same string (immutable)
266
267
// Deep cloning using serialization
268
MySerializableClass original = new MySerializableClass();
269
MySerializableClass deepCopy = ObjectUtil.cloneByStream(original);
270
```
271
272
### Hash Code Generation
273
274
Generate hash codes for objects and arrays with null safety.
275
276
```java { .api }
277
/**
278
* Generate hash code for object
279
* @param obj object to hash
280
* @return hash code
281
*/
282
public static int hashCode(Object obj);
283
284
/**
285
* Generate hash code for multiple objects
286
* @param objects objects to hash
287
* @return combined hash code
288
*/
289
public static int hashCode(Object... objects);
290
```
291
292
## Common Types
293
294
```java { .api }
295
// Function interfaces for transformations
296
import java.util.function.Function;
297
import java.util.function.Supplier;
298
299
// Collections and arrays
300
import java.util.Collection;
301
import java.util.Map;
302
import java.lang.reflect.Array;
303
304
// Cloning support
305
interface Cloneable {
306
Object clone() throws CloneNotSupportedException;
307
}
308
309
interface Serializable {
310
// Marker interface for serialization-based cloning
311
}
312
```