0

I have List of object returned from a native query. This list has String, Integer and Date. The variable looks like this:

Object[] result = (Object[])query.getSingleResult();

I need to check whether the list contains null or empty value. ObjectUtils from org.apache.commons.lang3.ObjectUtils worked effortlessly but if I were to do this without the library, how to achieve same result?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Muti
  • 58
  • 8
  • 2
    Why don't you checkout the code of ObjectUtils? – Simon Martinelli Oct 18 '22 at 12:05
  • It doesn't make sense to write "The list contains empty values"; nonethtless you can check null values with a loop statement: `for (Object obj : result) if (obj == null) return true; return false;` – 0009laH Oct 18 '22 at 12:10
  • @0009laH empty value for String item. Say the list value {"", 123} expected result is **true** which means the list contains empty value. – Muti Oct 18 '22 at 12:13
  • OK I understand :) In this case you can adapt the previous loop by checking `obj instanceof String and obj == ""` – 0009laH Oct 18 '22 at 12:16
  • Does this answer your question? [How do I determine whether an array contains a particular value in Java?](https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java) – Tom Oct 18 '22 at 12:20
  • 1
    @0009laH `obj.equals("")` would be more appropriate in Java. – Rogue Oct 18 '22 at 12:21
  • You're right @SimonMartinelli! I only checked on the imported class which doesn't show the code clearly., gonna check the Github. – Muti Oct 18 '22 at 12:23

2 Answers2

0

There is no canonical definition of 'empty value' for either Integer or Date.

You just program what you mean, and 'empty' is not a valid answer to the question 'what do you mean'.

For example: "Empty strings, the 0 integer, and sentinel instant value with epochmillis 0 (Date is a lie. It does not represent dates; it represents instants, and badly at that; use java.time.Instant instead normally)".

Then, you just.. program that:

for (Object obj : result) {
  if (obj == null) return false;
  if (obj instanceof Date) {
    if (((Date) obj).getTime() == 0) return false;
  } else if (obj instanceof String) {
    if (((String) obj).isEmpty()) return false;
  } else if (obj instanceof Integer) {
    if (((Integer) obj).intValue() == 0) return false;
  } else throw new IllegalStateException("Unexpected type: " + obj.getClass());

  return true;
}

It's an if for every type because there is no such thing as CouldBeEmpty as an interface that all these types implement. Said differently, "an empty Date" isn't a thing.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

This should work. I tried it with a mock array, but I don't know if it is exactly the same as yours:

   public static void main(String[] args) {
          Object result[] = new Object[5];

        result[0] = 1;
        result[1] = "";
        result[3] = 5;
        result[4] = LocalDate.now();
        result[5] = "string";

        boolean listcontainsnullorempty = false;
        for(var obj: result){
            if(obj == null || obj.equals("")){
                listcontainsnullorempty = true;
            }
        }
        System.out.println(listcontainsnullorempty);
    }

depending on your list, you might need to add || obj.isEmpty() in the if-clause.

Yannick Mussche
  • 316
  • 2
  • 12