Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any benefit to use methods of Objects.java? [duplicate]

Tags:

java

I examined methods of Objects.java, but i couldn't find too much useful sides of that methods. For Example the code that will work when i use Objects.isNull :

public static boolean isNull(Object obj) {
    return obj == null;
}

There are the two ways for checking nullity of two objects :

if(o == null)
if(Objects.isNull(o))

So there are not so many differences between them. Another example the code that will work i use Objects.toString

public static String toString(Object o) {
    return String.valueOf(o);
}

When i use it It calls toString of object at background.(With only one difference it writes "null", if the object is null because it uses String.valueOf()

And Objects.equals :

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

It will makes null check in every check(without knowing it is necessary or not.)

Am i wrong? If i am, why should i use that methods and other methods of Objects.java?

EDIT

I did not asked this question only for Objects.isNull and Objects.nonNull, i want to know purpose, usability(except for lambdas also) and benefits of Objects class and its methods. But in javadoc is written that only for Objects.isNull and Objects.nonNull have purpose to use with lambdas(as predicate filter(Objects::isNull)). I want to know others as well.

like image 964
SherlockHomeless Avatar asked Apr 02 '18 20:04

SherlockHomeless


People also ask

Why are duplicates not allowed in sets?

The meaning of "sets do not allow duplicate values" is that when you add a duplicate to a set, the duplicate is ignored, and the set remains unchanged. This does not lead to compile or runtime errors: duplicates are silently ignored. Set is implemented like that to avoid duplication.

Can set have duplicate objects in Java?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

What is duplicate object in Java?

lang. String objects a and b are duplicates when a != b && a. equals(b) . In other words, there are two (or more) separate strings with the same contents in the JVM memory.

Can ArrayList store duplicate?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.


3 Answers

Objects.isNull(), and the more useful Objects.nonNull(), exist for the purpose of being used in lambda expressions. Objects.toString() was introduced for null safety (as pointed out by @davidxxx), but is also very useful in lambdas.

For example, list.stream().filter(Objects::nonNull).map(Objects::toString) will give you a Stream<String> with the results of calling toString() on all the elements in list that are not null.

Objects.equals() is useful precisely when you know that the objects you're comparing might be null, as it saves you some typing.

like image 111
Aasmund Eldhuset Avatar answered Oct 19 '22 07:10

Aasmund Eldhuset


You seem to be asking 3 separate questions, so I'll address them separately:

  1. isNull() and its companion nonNull() were added in Java 8 to be used as method references, similarly to Integer.sum() and Boolean.logicalOr(). For example:

    // Print only non-null elements
    list.stream()
        .filter(Objects::nonNull)
        .forEach(System.out::println);
    
  2. I don't see any advantage in calling Objects.toString() over String.valueOf(). Maybe it was included for uniformity with the other null-safe helpers.

  3. If you know the objects are non-null, go ahead and use Object.equals(). Objects.equals() is meant to be used when they might both be null.

like image 21
shmosel Avatar answered Oct 19 '22 06:10

shmosel


In some cases some of these methods don't bring a "great" value and you can suitably ignore them.

1) But as you manipulate classes that miss some checks (check null to prevent NullPointerException) or "optimization" (check first reference equality in equals() for example) , using these Objects methods allow to not be hurt by these and so to keep your client code robust without writing directly all these checks.

2) Another interesting use is for lambda body as you want to use a method reference

3) At last, it allows to make homogeneous the way to perform these very common processings.

These 3 processing rely on 3 different ways :

String.valueOf(o);
if(o == null){...} 
a.equals(b);

While these rely on a single way : utility methods defined in Objects.

Objects.toString(o);
if(Objects.isNull(o)){...}
if(Objects.equals(a, b)){...}
like image 35
davidxxx Avatar answered Oct 19 '22 07:10

davidxxx