κ°λ°μν/Java
[Java] List λλκΈ°
cocococo331
2022. 5. 23. 21:03
As-Is νλ€κ² λλλ λ² (subList)
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
for(int i = 0; i<10000; i++) {
integerList.add(i);
}
int size = integerList.size();
int page = size / 100 + 1;
for (int i = 0; i < page; i++) {
int startIndex = i * 100;
int endIndex = i == page - 1 ? size : (i + 1) * 100;
List<Integer> subList = integerList.subList(startIndex, endIndex);
}
}
- List<E> subList(int fromIndex, int toIndex); μ¬μ©
- λ©λͺ¨λ¦¬ μ΄μλ, λΆλͺ¨Listκ° λ°λμ λ°λΌ SubListκ° μν₯μ λ°μ Exceptionμ λ±μ μ μκΈ° λλ¬Έμ newArrayList<>(integerList.subList(startIndex, endIndex)); // μλ‘ μμ±νλ κ²μ κΆμ₯
To-Be κ°λ¨νκ² λλλ λ² : Guava Library μ¬μ© (Lists.partition)
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>();
for(int i = 0; i<10000; i++) {
integerList.add(i);
}
List<List<Integer>> lists = Lists.partition(integerList, 100);
}