Home » openjdk-7 » java » util » [javadoc | source]
java.util
public final class: Objects [javadoc | source]
java.lang.Object
   java.util.Objects
This class consists of {@code static} utility methods for operating on objects. These utilities include {@code null}-safe or {@code null}-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
Method from java.util.Objects Summary:
compare,   deepEquals,   equals,   hash,   hashCode,   requireNonNull,   requireNonNull,   toString,   toString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.util.Objects Detail:
 public static int compare(T a,
    T b,
    Comparator<? super T> c) 
    Returns 0 if the arguments are identical and {@code c.compare(a, b)} otherwise. Consequently, if both arguments are {@code null} 0 is returned.

    Note that if one of the arguments is {@code null}, a {@code NullPointerException} may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for {@code null} values.

 public static boolean deepEquals(Object a,
    Object b) 
    Returns {@code true} if the arguments are deeply equal to each other and {@code false} otherwise. Two {@code null} values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.
 public static boolean equals(Object a,
    Object b) 
    Returns {@code true} if the arguments are equal to each other and {@code false} otherwise. Consequently, if both arguments are {@code null}, {@code true} is returned and if exactly one argument is {@code null}, {@code false} is returned. Otherwise, equality is determined by using the equals method of the first argument.
 public static int hash(Object values) 
    Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays#hashCode(Object[]) .

    This method is useful for implementing Object#hashCode() on objects containing multiple fields. For example, if an object that has three fields, {@code x}, {@code y}, and {@code z}, one could write:

    @Override public int hashCode() {
        return Objects.hash(x, y, z);
    }
    
    Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling #hashCode(Object) .
 public static int hashCode(Object o) 
    Returns the hash code of a non-{@code null} argument and 0 for a {@code null} argument.
 public static T requireNonNull(T obj) 
    Checks that the specified object reference is not {@code null}. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
    public Foo(Bar bar) {
        this.bar = Objects.requireNonNull(bar);
    }
    
 public static T requireNonNull(T obj,
    String message) 
    Checks that the specified object reference is not {@code null} and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:
    public Foo(Bar bar, Baz baz) {
        this.bar = Objects.requireNonNull(bar, "bar must not be null");
        this.baz = Objects.requireNonNull(baz, "baz must not be null");
    }
    
 public static String toString(Object o) 
    Returns the result of calling {@code toString} for a non-{@code null} argument and {@code "null"} for a {@code null} argument.
 public static String toString(Object o,
    String nullDefault) 
    Returns the result of calling {@code toString} on the first argument if the first argument is not {@code null} and returns the second argument otherwise.