68

Can I add null values to an ArrayList even if it has a generic type parameter?

Eg.

ArrayList<Item> itemList = new ArrayList<Item>();
itemList.add(null);

If so, will

itemsList.size();

return 1 or 0?

If I can add null values to an ArrayList, can I loop through only the indexes that contain items like this?

for(Item i : itemList) {
   //code here
}

Or would the for each loop also loop through the null values in the list?

Eran
  • 387,369
  • 54
  • 702
  • 768
Nick Bishop
  • 907
  • 2
  • 7
  • 6
  • 36
    Yes, 1, no, yes. – khelwood Nov 24 '14 at 14:28
  • https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java Similar to this use null instead of 0 – pramodc84 Dec 03 '18 at 10:42
  • Note that IntelliJ hides those nulls, which can be disabled - https://stackoverflow.com/questions/44430633/intellij-idea-debugger-does-not-show-null-element-in-a-list – Dror Fichman May 04 '22 at 11:04

3 Answers3

70

Yes, you can always use null instead of an object. Just be careful because some methods might throw error.

It would be 1.

Also nulls would be factored in in the for loop, but you could use

for (Item i : itemList) {
    if (i != null) {
       //code here
    }
}
svarog
  • 9,477
  • 4
  • 61
  • 77
deme72
  • 1,113
  • 1
  • 10
  • 13
  • 10
    For example, `List.of(...)` will throw if the supplied value is null. This is dumb -- just putting it out there. It's perfectly legit to have a null value in a list! – Josh M. Jan 27 '22 at 14:03
  • `List.of(...)` will throw exception however `Stream.of(null, "a").toList()` works perfectly fine. – thoredge Nov 08 '22 at 09:04
  • 1
    @JoshM. Weirdly enough, `Collections.singletonList()` does allow `null`, which means that the `of` method is even more "off". Anyway, if you need to create a list of one element that may be `null` then `singletonList` may help. – Maarten Bodewes Dec 13 '22 at 11:22
  • @MaartenBodewes that's ridiculous. Thanks for ruining my morning. :-D – Josh M. Dec 14 '22 at 13:03
35

You can add nulls to the ArrayList, and you will have to check for nulls in the loop:

for(Item i : itemList) {
   if (i != null) {

   }
}

itemsList.size(); would take the null into account.

 List<Integer> list = new ArrayList<Integer>();
 list.add(null);
 list.add (5);
 System.out.println (list.size());
 for (Integer value : list) {
   if (value == null)
       System.out.println ("null value");
   else 
       System.out.println (value);
 }

Output :

2
null value
5
Eran
  • 387,369
  • 54
  • 702
  • 768
3

You could create Util class:

public final class CollectionHelpers {
    public static <T> boolean addNullSafe(List<T> list, T element) {
        if (list == null || element == null) {
            return false;
        }

        return list.add(element);
    }
}

And then use it:

Element element = getElementFromSomeWhere(someParameter);
List<Element> arrayList = new ArrayList<>();
CollectionHelpers.addNullSafe(list, element);
dzenisiy
  • 855
  • 10
  • 32