0

This returns true in my trite little example:

List<Object> a = Collections.emptyList();

System.out.println(a == Collections.emptyList());

Is there an instance where a previous call to Collections.emptyList() might not equal Collections.emptyList() on a subsequent call? Is the result constant?

The implementation suggests so:

public static final List EMPTY_LIST = new EmptyList<>();

public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}

But how safe is it to rely on it outside of this particular implementation? The documentation doesn't explicitly state that it is constant.

Zhro
  • 2,546
  • 2
  • 29
  • 39

0 Answers0