티스토리 뷰
CollectionUtils.isEmpty()에 대해 알아보겠습니다.
import org.springframework.util.CollectionUtils;
CollectionUtils.isEmpty()는 java Collection(List, Map, Set)의 종류의 값들의 존재 여부를 판단하는 메서드입니다.
List<?> list = new ArrayList();
Set<String> set = new HashSet<>();
Map<String, Object> map = new HashMap<String, Object>();
boolean bListEmpty;
bListEmpty = CollectionUtils.isEmpty(list);
System.out.println("list : " + bListEmpty);
bListEmpty = CollectionUtils.isEmpty(set);
System.out.println("set : " + bListEmpty);
bListEmpty = CollectionUtils.isEmpty(map);
System.out.println("map : " + bListEmpty);
결과 : true
CollectionUtils.class 안에 method들 중 아래와 같이 static method로 선언되어있는 걸 확인해 볼 수 있습니다.
public static boolean isEmpty(@Nullable Collection<?> collection) {
return (collection == null || collection.isEmpty());
}
public static boolean isEmpty(@Nullable Map<?, ?> map) {
return (map == null || map.isEmpty());
}
아래와 같이 1번 코드를 2번 코드처럼 CollectionUtils.isEmpty()를 사용하여 코드를 작성할 수 있습니다.
1. if(list == null || list.isEmpty()){}
2. if(CollectionUtils.isEmpty(list)){}
list에 데이터 값의 존재 여부를 판단할 수 있는 CollectionUtils.isEmpty()에 대해 알아보았습니다.
끝.
728x90
'Backend' 카테고리의 다른 글
| [Error] java.lang.NumberFormatException: For input string: "Y" (0) | 2022.09.27 |
|---|---|
| [Java] StringUtils.equals() (0) | 2022.09.15 |
| [Error] java.sql.SQLSyntaxErrorException: Incorrect integer value: '' for column (0) | 2022.08.26 |
| [Java] contains() (0) | 2022.06.30 |
| [Java] isEmpty() (0) | 2022.06.17 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Python
- Lambda
- for
- Built-in Functions
- Lower
- index
- function
- bool
- Method
- zip
- isalpha
- counter
- permutations
- operators
- If
- Upper
- combinations
- find
- isdigit
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함
