public List<String> foo(int a){
if(a == 0){
return null;
}
...
return list;
}
public void main(){
List<String> list = foo();
if(list == null){ // null처리 로직 추가
...
}
list.stream
.map(...)
.collect(Collectors.toList());
}
public List<String> foo(int a){
if(a == 0){
return Collections.emptyList(); //빈 컬렉션을 매번 생성하지 않았다!
}
...
return list;
}
public void main(){
List<String> list = foo();
list.stream
.map(...)
.collect(Collectors.toList());
}
null을 반환하는 API는 사용하기 어렵고 오류 처리 코드도 늘어난다. 그렇다고 성능이 좋은것도 아니다.