Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java return an empty list

I have my Java method as below;

public List<Lookup> findAll(String lang) {     Query query = entityManager.createNamedQuery("Lookup.findAll");     if (isValidLang(lang)) {         query.setParameter("lang", lang);         return query.getResultList();     } else {         //return empty list     } } 

Now the method returns List for valid matches of lang.

But if that is not the case, I want to return an empty list. My question is how do I update the code & what is the best way to return an empty list so that the code does not fail ?

like image 630
copenndthagen Avatar asked Feb 09 '15 09:02

copenndthagen


People also ask

How do you return an empty list in Java?

emptyList() returns an immutable list, i.e., a list to which you cannot add elements if you want to perform any operation on your list, then create new instance of list and return it. if (isValidLang(lang)) { query. setParameter("lang", lang); return query.

How do I return a blank list?

The emptyList() method returns an empty immutable list.

How do you return an empty ArrayList in Java?

Method 1: Using clear() method as the clear() method of ArrayList in Java is used to remove all the elements from an ArrayList. The ArrayList will be completely empty after this call returns.

How do you return an empty array in Java 8?

Return an Empty Array Using new int[0] in Java To return an empty array from a function, we can create a new array with a zero size. In the example below, we create a function returnEmptyArray() that returns an array of int . We return new int[0] that is an empty array of int .


2 Answers

This should do it:

} else {      return Collections.emptyList(); } 

Notes:

  1. As of java 9, besides Collections.emptyList() there is also List.of().
  2. This construct is the most efficient one, because it reuses an existing instance of an empty list, so each time you ask for an empty list, no new list gets created. (Of any generic type.) However, object allocation in Java is very inexpensive, so this should not really be a concern.
  3. As others have pointed out, this list returned by Collections.emptyList() and List.of() is secretly immutable. By "secretly" I mean that it exposes mutation methods, but if you make the mistake of invoking any of those methods, you will get an exception. In other, better languages than Java, (for example, in Scala,) there exist immutable / unmodifiable collections, but Java does not have such a thing "out of the box". So, if you are sticking with Java in 2020, you are accepting the possibility that not all of your collections can be written to, despite the fact that they all look as if they can be written to.
  4. In general, functions are meant to return immutable entities, so if you are invoking a function which returns a collection and then you modify this collection, you are doing it wrong. If you really need such a construct, you should instead write a function which populates a mutable list that you pass to it as a parameter, so it is evident that you, as the owner of the mutable list, are free to further modify the list after the function returns.
like image 182
Mike Nakis Avatar answered Sep 29 '22 04:09

Mike Nakis


Collections.emptyList() returns an immutable list, i.e., a list to which you cannot add elements if you want to perform any operation on your list, then create new instance of list and return it.

if (isValidLang(lang)) {         query.setParameter("lang", lang);         return query.getResultList();     } else {         return new ArrayList<Lookup>();  // return array list instance.     } 
like image 39
atish shimpi Avatar answered Sep 29 '22 05:09

atish shimpi