0% found this document useful (0 votes)
20 views

Java Objects Class Util

The java.util.Objects class provides utility methods for handling objects, especially related to null handling and equality comparisons. It contains methods like equals() to compare objects, hash() to generate hash codes, requireNonNull() to throw exceptions on null values, and deepEquals() for deep comparisons of nested objects. The class is commonly used for null checks, hash generation, comparisons, and string representations of objects.

Uploaded by

sanjup0206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Objects Class Util

The java.util.Objects class provides utility methods for handling objects, especially related to null handling and equality comparisons. It contains methods like equals() to compare objects, hash() to generate hash codes, requireNonNull() to throw exceptions on null values, and deepEquals() for deep comparisons of nested objects. The class is commonly used for null checks, hash generation, comparisons, and string representations of objects.

Uploaded by

sanjup0206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

java.util.

Objects
The java.util.Objects 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


java.util.Objects 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 = Objects.equals(str1, str2);
System.out.println("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 = Objects.hash("Alice", 30, 175.5);
System.out.println("Generated hash code: " + hash);
compare(T a, T b,
Comparator<? super T>
c)
Compares two objects using a specified comparator.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"
Comparator<String> lengthComparator =
Comparator.comparing(String::length);
int result = Objects.compare(names.get(0), names.get(1),
lengthComparator);
System.out.println("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 = Objects.requireNonNullElse(input, defaultText
isNull(Object obj)
&
nonNull(Object obj)
Checks if an object is null or not null, respectively.
String name = null;
if (Objects.isNull(name)) {
System.out.println("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 = Objects.toString(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 = Objects.deepEquals(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 = Objects.deepHashCode(array);
System.out.println("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 = Objects.deepToString(matrix);
System.out.println("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 = Objects.requireNonNull(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;
Objects.checkIndex(index, array.length);
// 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;
Objects.checkFromToIndex(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;
Objects.checkFromIndexSize(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 = Objects.requireNonNull(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 (Objects.isNull(name)) {
System.out.println("Name is null");
}

You might also like