title: Optional<List<T>> for Empty List in Repository Layer Returns Optional.empty . How do I change it to return Optional[[]]
body: <p>I have a JPA Query like this.</p>
  
  <p>PersonRepository.java</p>
  
  <pre><code>public Optional&lt;List&lt;PersonEntity&gt;&gt; findByStatus(int status);
  </code></pre>
  
  <p>PersonService.java</p>
  
  <pre><code>System.out.println(“Optional value is ”+findByStatus(1));
  </code></pre>
  
  <p>The output is <code>Optional value is Optional.empty</code></p>
  
  <p>Now I changed my query 
  PersonRepository.java</p>
  
  <pre><code>public List&lt;PersonEntity&gt; findByStatus(int status);
  </code></pre>
  
  <p>PersonService.java</p>
  
  <pre><code>Optional&lt;List&lt;PersonEntity&gt;&gt; optional = Optional.of(findByStatus(1));
  System.out.println("Optional value is "+optional);
  </code></pre>
  
  <p>The output is   <code>Optional value is Optional[[]]</code></p>
  
  <p>In my database, there is no value for status 1 . I want my output as <code>Optional[[]]</code> for the 1st query. How do I achieve this?</p>
  
  <p>Now, I want to achieve this because, whenever an <code>optional.get()</code> throws a <code>NoSuchElementException</code>, I will handle it using my Exception Controller and expose it as 404 to the REST Layer. But, when a <code>List&lt;Object&gt;</code> is fetched, the response is only a empty List, but <code>optional.get()</code> still throws a <code>NoSuchElementException</code>. I want to avoid this. In short, the optional fetch of  Single Entity from Repository throws <code>NoSuchElementException</code> if no value is found which is perfect. But for the optional fetch of a list of Entities which is empty is should be returned as Empty and not throw <code>NoSuchElementException</code> as it is not a 404 error. It just means that the list is empty at present.</p>