Java에서 목록을 `n` 크기의 하위 목록으로 분할
이 게시물은 목록을 크기의 하위 목록으로 분할하는 방법에 대해 설명합니다. n
Java에서. 최종 목록은 다음보다 작을 수 있습니다. n
목록의 크기에 따라.
1. Guava바 사용하기
Guava 라이브러리를 사용하면 다음을 사용할 수 있습니다. Lists.partition() 목록을 지정된 크기의 연속적인 하위 목록으로 분할하는 방법입니다. 다음은 이 방법의 사용법을 보여주는 간단한 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.Lists; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; List<List<Integer>> partition = Lists.partition(collection, partitionSize); System.out.println(partition); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
2. Apache Commons Collections 사용하기
Guava바와 비슷한 Lists.partition()
메소드, Apache Commons Collections ListUtils
수업 제안 partition()
유사한 기능을 제공하는 방법.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.collections4.ListUtils; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; List<List<Integer>> partitions = ListUtils.partition(collection, partitionSize); System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
3. 사용 List.subList()
방법
타사 라이브러리를 선호하지 않는 경우 이 작업에 대한 고유한 루틴을 작성할 수 있습니다. 다음 솔루션은 일반 for 루프를 사용하여 목록을 반복하고 List.subList()
시작 인덱스와 끝 인덱스 사이의 파티션을 가져오는 방법입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; List<List<Integer>> partitions = new ArrayList<>(); for (int i = 0; i < collection.size(); i += partitionSize) { partitions.add(collection.subList(i, Math.min(i + partitionSize, collection.size()))); } System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
이것은 Java 8 Stream API를 사용하는 다음과 동일합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { private static <T> List<List<T>> partition(List<T> collection, int partitionSize) { return IntStream.rangeClosed(0, (collection.size() - 1) / partitionSize) .mapToObj(i -> collection.subList(i * partitionSize, Math.min((i + 1) * partitionSize, collection.size()))) .collect(Collectors.toList()); } public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; List<List<Integer>> partitions = partition(collection, partitionSize); System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
마지막으로 Java 9에서는 다음을 사용하여 코드를 단순화할 수 있습니다. IntStream.iterate()
술어를 취하는 메소드.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { private static <T> List<List<T>> partition(List<T> collection, int partitionSize) { return IntStream.iterate(0, i -> i < collection.size(), i -> i + partitionSize) .mapToObj(i -> collection.subList(i, Math.min(i + partitionSize, collection.size()))) .collect(Collectors.toList()); } public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; List<List<Integer>> partitions = partition(collection, partitionSize); System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
4. 사용 Collectors.groupingBy()
방법
Stream API를 사용하여 Java에서 목록을 분할하는 더 많은 방법이 있습니다. 다음 솔루션은 Collectors.groupingBy()
목록 요소에 대해 "그룹화 기준" 작업과 동일한 작업을 수행합니다. 분류 함수에 따라 요소를 그룹화하고 결과를 맵에 반환합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { private static <T> Collection<List<T>> partition(List<T> collection, int n) { return IntStream.range(0, collection.size()).boxed() .collect(Collectors.groupingBy(i -> i / n, Collectors.mapping(collection::get, Collectors.toList()))) .values(); } public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; Collection<List<Integer>> partitions = partition(collection, partitionSize); System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
코드는 다음과 같은 변경 가능한 카운터를 사용하여 단순화할 수 있습니다. AtomicInteger
수업:
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 |
import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { private static <T> Collection<List<T>> partition(List<T> collection, int n) { AtomicInteger counter = new AtomicInteger(); return collection.stream() .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / n)) .values(); } public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 15) .boxed().collect(Collectors.toList()); int partitionSize = 4; Collection<List<Integer>> partitions = partition(collection, partitionSize); System.out.println(partitions); } } |
결과:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15]]
그것은 목록을 크기의 하위 목록으로 나누는 것입니다. n
Java에서.