1
public List<Person> selectAllPeople() {
    return List.of(new Person(UUID.randomUUID(), "From Postgres DB"));
}

What other alternative I can use other than List.of to generate same output?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80

1 Answers1

4

You can use Collections.singletonList.

Returns an immutable list containing only the specified object. The returned list is serializable.

return Collections.singletonList(new Person(UUID.randomUUID(), "From Postgres DB"));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • You should be careful. There are subtle differences, e.g. https://ideone.com/Vbbgdo – Michael Jan 11 '21 at 15:57
  • @Michael so immutable != unmodifiable. Looks like you can just wrap the return value with a `unmodifiableList` call. e.g. `return Collections.unmodifiableList(Collections.singletonList(new Person(UUID.randomUUID(), "From Postgres DB")));` – Mr. Polywhirl Jan 11 '21 at 16:10
  • @Michael: why does that happen? Thanks. – Khanna111 Aug 10 '23 at 04:28