< 목표 >
List<?>에 담긴 아이템을 지정된 크기게 맞춰 각 그룹으로 나누어 담기.
List 안의 아이템들에 파티션 적용하기
예를들어 리스트에 담긴 내용이 1,2,3,4,5,6,7,8,9,10 이고, 3개씩 분할한다면,
[[1,2,3], [4,5,6], [7,8,9], [10]]으로 나누어 담아준다.
1. Guava라이브러리 - Lists.partition()
: 목록을 지정된 크기의 연속적인 하위 목록으로 분할한다
import com.google.common.collect.Lists;
~~~
List<Long> targetList = {targetList};
int partitionSize = 3;
List<List<Long>> partitionedList = Lists.partition(targetList, partitionSize);
2. Apache Commons Collections의 - ListUtils.partition()
: 목록을 지정된 크기의 연속적인 하위 목록으로 분할한다
import org.apach.commons.collections4.ListUtils;
~~
List<Long> targetList = {paramList};
int partitionSize = 3;
List <List<Long>> partitionList = ListUtils.partition(targetList, partitionSize );
3. java Util 의 List.subList()
: 타사 라이브러리를 이용하지 않고도 사용할 수 있다.
subList는 시작 인덱스와 끝 인덱스 사이의 파티션을 가져오는 방법이다.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
~~
List<Long> targetList = {target Data List};
int SCALE = 3;
List<List<Long>> resultList = new ArrayList<>();
for (int i = 0; i < targetList.size(); i+= SCALE) {
if (i % SCALE == 0) {
resultList.add(targetList.subList(i,
i + PAGE_SCALE <= connectNoList.size() ? i + PAGE_SCALE : targetList.size());
}
}
< 참고 >
https://www.techiedelight.com/ko/split-list-into-sub-lists-java/
'공부함 > JAVA Hard' 카테고리의 다른 글
| [JAVA] 모델을 담은 리스트에서 모델 내의 특정 값을 추출하여 하나의 문자열로 만들기 (1) | 2023.11.09 |
|---|---|
| [ JAVA ] java.util.Date 와 java.sql.Date (0) | 2023.09.20 |
| [JAVA] List를 한줄의 문자열로 변경하기 ( join ) (0) | 2023.08.22 |
| [JAVA] 파일 이름 줄이기( substring, lastIndexOf , indexOf ) (0) | 2022.09.29 |