[Link].
Objects
The [Link] class provides various utility
methods for dealing with objects, especially related to
null handling and equality comparisons.
Here are some commonly used methods from the
[Link] class along with their usage
examples.
equals(Object a,
Object b)
Compares two objects for equality, handling null
values.
String str1 = "Hello";
String str2 = "Hello";
boolean areEqual = [Link](str1, str2);
[Link]("Are str1 and str2 equal? " + areEqual);
// Output: Are str1 and str2 equal? true
hash(Object... values)
Generates a hash code based on the provided values.
int hash = [Link]("Alice", 30, 175.5);
[Link]("Generated hash code: " + hash);
compare(T a, T b,
Comparator<? super T>
c)
Compares two objects using a specified comparator.
List<String> names = [Link]("Alice", "Bob", "Charlie"
Comparator<String> lengthComparator =
[Link](String::length);
int result = [Link]([Link](0), [Link](1),
lengthComparator);
[Link]("Comparison result: " + result);
requireNonNull(T obj)
&
requireNonNull(T obj, String
message)
Throws a NullPointerException if the provided
object is null.
String value = null;
String result = Objects.
requireNonNull(value, "Value cannot be null"
requireNonNullElse(T obj, T
defaultObj)
&
requireNonNullElseGet(T obj,
Supplier<? extends T>
supplier)
Returns the provided object if it's non-null; otherwise,
returns a default value or the result of a supplier
function.
String input = null;
String defaultText = "Default Value";
String result = [Link](input, defaultText
isNull(Object obj)
&
nonNull(Object obj)
Checks if an object is null or not null, respectively.
String name = null;
if ([Link](name)) {
[Link]("Name is null");
}
toString(Object o)
&
toString(Object o, String
nullDefault)
Returns the string representation of the provided
object, with optional handling of null values.
Object obj = null;
String defaultString = "Object is null";
String objString = [Link](obj, defaultString);
deepEquals(Object a,
Object b)
Performs a deep comparison of two arrays or objects,
considering nested arrays/objects.
int[][] array1 = {{1, 2}, {3, 4}};
int[][] array2 = {{1, 2}, {3, 4}};
boolean deepEqual = [Link](array1, array2);
deepHashCode(Object o)
Generates a hash code for an object that might contain
arrays or other objects.
int[][] array = {{1, 2}, {3, 4}};
int deepHash = [Link](array);
[Link]("Deep hash code: " + deepHash);
deepToString(Object o)
Returns a string representation of an object that might
contain arrays or other objects.
Object[][] matrix = {{1, 2}, {3, 4}};
String deepString = [Link](matrix);
[Link]("Deep string: " + deepString);
requireNonNull(T obj,
Supplier<String>
messageSupplier)
Throws a NullPointerException with a custom
message obtained from the provided supplier if the
object is null.
String value = null;
String result = [Link](value, () ->
"Value cannot be null
checkIndex(int index,
int length)
Checks if the given index is within the bounds of the
specified length. Throws an
IndexOutOfBoundsException if not.
int[] array = {1, 2, 3, 4, 5};
int index = 10;
[Link](index, [Link]);
// Throws IndexOutOfBoundsException
checkFromToIndex(int
fromIndex, int
toIndex, int length)
Checks if the given range is valid for a sequence of the
specified length. Throws an
IndexOutOfBoundsException if not.
int fromIndex = 2;
int toIndex = 5;
int length = 4;
[Link](fromIndex, toIndex, length);
// Throws IndexOutOfBoundsException
checkFromIndexSize(int
fromIndex, int size,
int length)
Checks if the given range starting at the specified index
with the specified size is valid for a sequence of the
specified length. Throws an
IndexOutOfBoundsException if not.
int fromIndex = 2;
int size = 5;
int length = 4;
[Link](fromIndex, size, length);
// Throws IndexOutOfBoundsException
requireNonNull(T obj,
Supplier<? extends X>
exceptionSupplier)
Throws a custom exception, obtained from the
provided supplier, if the object is null.
String value = null;
String result = [Link](value, () -> new
IllegalArgumentException("Value cannot be null")
isNull(Object obj) &
nonNull(Object obj)
Checks if an object is null or not null, respectively.
String name = null;
if ([Link](name)) {
[Link]("Name is null");
}