Item 54. Empty Collection
class Example {
private final List<String> list = new ArrayList<>();
public List<String> getList() {
return list.isEmpty()
? null
: new ArrayList<>(list);
}
public static void main(String[] args) {
Example example = new Example();
List<String> list = example.getList();
if (list != null) {
System.out.println(list.size());
}
}
}Last updated