IT 인터넷/소나큐브
[소나큐브] Collection.isEmpty() should be used to test for emptiness
바다소년3904
2022. 10. 12. 17:47
728x90
[오류내용]
Use isEmpty() to check whether the collection is empty or not.
Collection.isEmpty() should be used to test for emptiness
[해결방안]
로직상으로는 위와 같이 체크해도 문제는 없었으나 size() 로 체크할 경우 시간비용이 많이 든다고 한다.
ArrayList 의 isEmpty() 를 사용해보았으나 list 값이 null 일 경우 java.lang.NullPointerException 이 발생했다.
아파치에서 제공해주는 모듈(org.apache.commons.collections.CollectionUtils)을 사용해서 테스트하니 list 가 null, list.size == 0 일 경우 모두 체크해주었다.
수정된 코드는 아래와 같이 하면 된다.
if( !CollectionUtils.isEmpty(list) ) {
~~~~~~~~~~~
}
728x90
반응형