๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ์ƒํ™œ/Java

[Java] List ๋‚˜๋ˆ„๊ธฐ

by cocococo331 2022. 5. 23.

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);
}

'๊ฐœ๋ฐœ์ƒํ™œ > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

char to int  (0) 2023.05.10
ObjectMapper ์‚ฌ์šฉ List Mapping  (0) 2022.11.28
[Java] Null String Casting  (0) 2022.04.29
[Java] Map ์ œ๊ณต ๋ฉ”์„œ๋“œ๋“ค ์ •๋ฆฌ  (0) 2022.04.26
[JAVA] String ์ค‘๋ณต์ œ๊ฑฐ Stream  (0) 2022.03.22