-1

I have a huge sized java list object as follows :

List<Object[]> res = // repo method that will return query result

I have to remove nulls from the res object.

I tried below methods so far but nothing seemed to work:

1. res.remove(null);
2. res.removeAll(Collections.singleton(null));
3. res.removeAll(null);
4. while(res.remove(null));

How can I remove nulls efficiently as the list size is going to be huge?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
code-geek
  • 441
  • 1
  • 8
  • 22
  • 2
    Please provide a [mcve] - it'll be a lot easier to help you that way. – Jon Skeet Nov 19 '20 at 10:47
  • Thank you very much! I was having another issue too that let to this prblem – code-geek Nov 19 '20 at 11:20
  • In general query results are not guaranteed to be mutable, so you may not be able to do what you are trying. With a mutable list (an `ArrayList` in my case) nos. 2 and 4 work for me (no. 1 removes only the first `null` and no. 3 throws). – Ole V.V. Nov 19 '20 at 12:53
  • Please, *nothing seemed to work* is not very helpful. Please quote the exact error messages you get so we may help you how to solve them. Or if you don’t get any error messages, specify precisely how observed result differs from the expected. [I downvoted because "it's not working" is not helpful](http://idownvotedbecau.se/itsnotworking/). – Ole V.V. Nov 19 '20 at 13:45

1 Answers1

8

Mutable list

You can use List::removeIf with a predicate detecting null items.

List<Object[]> mutableList = new ArrayList<>(Arrays.asList(
    new Object[] {}, 
    null, 
    new Object[] {}));

mutableList.removeIf(Objects::isNull);

Immutable list

In this case you have to use Stream API or a for-loop to find non-null elements and add them to a new list.

List<Object[]> immutableList = Arrays.asList(
    new Object[] {}, 
    null, 
    new Object[] {});

List<Object[]> newList = immutableList.stream()
                                      .filter(Objects::nonNull)
                                      .collect(Collectors.toList());
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • 1
    Very fine answer. And really nitpicking: `Arrays.asList()` doesn’t return a completely immutable list since you may substitute each element in the list (using `set()`). But you are correct, no elements can be removed from the list. – Ole V.V. Nov 19 '20 at 13:48
  • @OleV.V.: Thank's for the note. The `Arrays.asList()` operates on a fixed-length backing array, so no elements can be added/removed but can be replaced, right? – Nikolas Charalambidis Nov 19 '20 at 13:56
  • Exactly right (and I still think it’s a great answer). – Ole V.V. Nov 19 '20 at 14:03
  • 1
    @OleV.V. I have created a "semi-canonical" answer here: https://stackoverflow.com/questions/8559257/remove-null-elements-from-list as long as I miss there a lot of ways or the element removal. I believe that one deserves more attention than this one. Preferably, I'd like to remove this answer in favor of the newer one and only refer to it at the comments for sake of getting rid of the "duplicates". – Nikolas Charalambidis Nov 19 '20 at 14:11
  • 1
    For other readers: [Link to Nikolas’ new and extensive answer](https://stackoverflow.com/a/64910701/5772882) – Ole V.V. Nov 19 '20 at 16:19