Introduction to the ObjectUtils class
Methods:
allNotNull(Object...values) | Checks that all objects are not null |
allNull(Object...values) | Checks that all objects are null |
anyNotNull(Object...values) | Checks that at least one object is not null |
anyNull(Object... values) | Checks that at least one object is null |
clone(T obj) | Clones an object |
cloneIfPossible(T obj) | Clones an object or returns the original |
compare(T c1, T c2) | Compares objects |
defaultIfNull(T object, T defaultValue) | Returns the default object if object is null |
equals(Object object1, Object object2) | Compares two objects |
notEqual(Object object1, Object object2) | Check if two objects are not equal |
firstNonNull(T...values) | Returns the first object that is not null |
getFirstNonNull(Supplier |
Returns the first object that is not null |
getIfNull(T object, Supplier |
Returns the given object if it is not null, otherwise returns the Supplier.get() value of the passed Supplier |
hashCode(obj) | Calculates the hashCode for an object |
hashCodeMulti(Object... objects) | Calculates hashCode for a group of objects |
isEmpty(Object object) | Checks if an object is empty or null |
isNotEmpty(Object object) | Checks if an object is not empty or null |
requireNonEmpty(T obj) | Checks if an object is not null, otherwise throws an exception |
requireNonEmpty(T obj, String message) | Checks if an object is not null, otherwise throws an exception |
identityToString(Object object) | Returns a string for an object |
toString(Object obj) | Returns a string for an object |
toString(Object obj, String nullStr) | Returns a string for an object |
toString(Object obj, Supplier |
Returns a string for an object |
Let's look at one method from each group. I hope you will use them often, because they are very convenient and allow you to avoid unnecessary code.
ObjectUtils.compare()
The method compares objects in the same way as comparator: greater than, less than or equal to. It can be used to sort objects.
The method signature looks like this:
public static <T extends Comparable<? super T>> int compare(final T c1, final T c2);
public static <T extends Comparable<? super T>> int compare(final T c1, final T c2, final boolean nullGreater);
If the third parameter ( nullGreater ) is true , then null will always be considered greater than non- null . The method returns positive if c1> c2, negative if c1<c2, and 0 if c1 == c2.
Example:
String firstValue = "codeGym";
String secondValue = "codeGym";
System.out.print(ObjectUtils.compare(firstValue, secondValue));
System.out.println();
firstValue = "codeGym";
secondValue = null;
System.out.print(ObjectUtils.compare(firstValue, secondValue));
System.out.println();
firstValue = "";
secondValue = "codeGym";
System.out.print(ObjectUtils.compare(firstValue, secondValue));
System.out.println();
The program will display the result:
0
1
-8
ObjectUtils.isNotEmpty()
The isNotEmpty() method checks that the object passed to it is neither empty nor null .
Method signature:
public static boolean isNotEmpty(final Object object)
Example:
List<String> values = new ArrayList<>();
System.out.println(ObjectUtils.isNotEmpty(values));
values.add("codeGym");
System.out.println(ObjectUtils.isNotEmpty(values));
values = null;
System.out.println(ObjectUtils.isNotEmpty(values));
The result will be displayed on the screen:
false
true
false
java.util.Objects
The Java developers really liked the idea of ObjectUtils , so in JDK 7 they added their own:
isNull(Objectobj) | Checks if an object is null |
nonNull(Object obj) | Checks if an object is not null |
toString(Objecto) | Converts an object to a string |
toString(Objecto, String nullDefault) | Converts an object to a string |
boolean equals(Object a,Object b) | Compares objects |
boolean deepEquals(Object a,Object b) | Compares objects |
T requireNonNull(T obj) | Checks if the passed parameter is not null |
T requireNonNull(T obj,String message) | Checks if the passed parameter is not null |
int hashCode(Object o) | Calculates the hashCode for an object |
int hash(Object...values) | Calculates hashCode for a group of objects |
int compare(T a,T b,Comparator c) | Compares objects |
Since the java.util.Objects class is part of the JDK, it is recommended that you use it in your code.
It is important to note that when you read someone else's code, you will most likely come across options from ObjectUtils , this often happens in open-source. Here you can see how they differ.
GO TO FULL VERSION