티스토리 뷰

Java

[Spring] CollectionUtils.isEmpty()

seungwonlee 2022. 6. 20. 18:20

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

'Java' 카테고리의 다른 글

[Java] distinct()  (0) 2022.12.15
[Java] Iterator()  (0) 2022.11.02
[Java] StringUtils.equals()  (0) 2022.09.15
[Java] contains()  (0) 2022.06.30
[Java] isEmpty()  (0) 2022.06.17
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
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
31
글 보관함