14

I think there has to be a better way of doing this.

I have a call to a function that returns an ArrayList.

If ArrayList only returns 10 null items (the default), is there a way of checking this without iterating through all 10 items to see if they are null?

5
  • can you please explain what do you mean by ArrayList only returns 10 null items Commented Jan 4, 2013 at 18:11
  • What do you mean by "the default"? If it means that you haven't added anything to it then just use .isEmtpy() method. Commented Jan 4, 2013 at 18:11
  • why not use the isEmpty() method? Commented Jan 4, 2013 at 18:12
  • 9
    What's the use of adding null items to the ArrayList? Commented Jan 4, 2013 at 18:12
  • I wonder if he is confused by the documentation for the default constructor for ArrayList(). It allocates space for 10 items, but it doesn't place any items, nor nulls, into that space. isEmpty() would be the correct method. Commented Jan 4, 2013 at 21:43

8 Answers 8

32

As of Java 8, you can use Streams like this:

boolean nullsOnly = list.stream().noneMatch(Objects::nonNull);

Which is equal to:

boolean nullsOnly = list.stream().allMatch(x -> x == null)

And here you can see the more general way of testing that any list matches a given Predicate:

list.stream().allMatch(x -> /* Some testing function. */)
Sign up to request clarification or add additional context in comments.

2 Comments

Didn't know about "noneMatch" or "allMatch" until now. Thanks for that.
Used your answer then Intellij suggested to replace to list.stream().allMatch(Objects::isNull)
8

Generally, no; there is no other way to tell that an arbitrary ArrayList contains ten instances of null than to loop over it and make sure each element is null. You can forgo this, of course, if the size() of the list isn't equal to ten.

Comments

3
List<Object> arrayList = new ArrayList<Object>();

arrayList.isEmpty();
// first call returns true    

arrayList.add(null);
// second call returns false    
Object obj = new Object();

arrayList.add(obj);

arrayList.isEmpty();
// third call returns false

Is that what you wanted?

Multiple addition of nulls increments the size by one, but there isn't a concrete "easy way" of finding the number of null objects unless you loop the array list.

2 Comments

It's not what he's wanted. His list contains ten null entries. His point of view is empty, but isEmpty would return false.
Updated my answer to be a little more detailed.
2

Are you trying to check if the ArrayList is empty? (i.e. myArrayList.isEmpty()) or are you trying to check if the ArrayList contains only null references? If you're trying to check if all entries are null then you will need to check each entry...

boolean isListOfNulls(List myList){
    for(Object o: myList)
        if(!(o == null))
            return false;
    return true;
}

3 Comments

Because that's where my cursor was when I typed ! :). Either works. On a side note, now I feel the need to benchmark that and see if it makes a difference.
Sure, both work, but one is very easy to read, the other one needs to be double checked when reading ;)
@Mike They almost certainly don't make a difference as they are exactly the same (it is even mentioned somewhere in the JLS).
1

try with the stream API.

boolean haveOnlyNulls = arraylist.stream().noneMatch(object -> object != null);

Comments

1

The best way to check is by using boolean allMatch(Predicate<? super T> predicate); This Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.

 boolean hasAllNulls = list.stream().allMatch(Objects::isNull)

Comments

0

Change the default to an empty ArrayList and use isEmpty(). The present default is senseless.

Comments

0

If you want to cover many possibilities, here is one more. Not necessarily the best.

Set

A Set in Java eliminates duplicates. This includes the value null, only one allowed.

So make a Set of your List. If no values, the set is empty. If only nulls were present in list, then set has only one element — so test if it is a null. If multiple elements in set, then we know they original list has some non-null values.

Sample data:

List < String > list = new ArrayList <> ( );
list.add ( null );
list.add ( null );
list.add ( null );

Logic:

Set < String > set = new HashSet <> ( list );  // Make a `Set` of our `List`. Duplicates ignored, including multiple `null` values.
set.remove ( null );  // If list were all nulls, we have a single null element in our set — so remove it, if it exits.
boolean emptyOrAllNulls = set.isEmpty ( );

emptyOrAllNulls = true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.