본문 바로가기
Java/기본 문법

Collection 주요 기능 - List

by 정구정구 2026. 6. 11.

정렬 (Sort)

기본 정렬

    List<Integer> numbers = Arrays.asList(5, 2, 8, 1, 9);
    
    // 숫자 오름차순 정렬
    Collections.sort(numbers);
    // numbers = [1,2,5,8,9]
    
    // 숫자 내림차순 정렬
    Collections.sort(numbers, Collections.reverseOrder());
    // numbers = [9,8,5,2,1]
    List<String> words = Arrays.asList("banana", "apple", "cherry");
    
    // 문자 정렬
    Collections.sort(words);
    // words = ["apple","banana","cherry"]

중첩 정렬

    List<List<Integer>> nestedList = new ArrayList<>();
    nestedList.add(Arrays.asList(3, 5, 2));
    nestedList.add(Arrays.asList(1, 2, 8));
    nestedList.add(Arrays.asList(4, 7, 6, 9));
        
    // 첫 번째 요소 기준 정렬
    nestedList.sort(Comparator.comparing(list -> list.get(0)));
    // nestedList = [{1,2,8}, {3,5,2}, {4,7,6,9}]

    // 리스트 합계 기준 정렬
    nestedList.sort(Comparator.comparing(list -> list.stream().mapToInt(Integer::intValue).sum()));
    // nestedList = [{4,7,6,9}, {1,2,8}, {3,5,2}]

객체 정렬

    static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() { return name; }
        public int getAge() { return age; }

        @Override
        public String toString() {
            return name + "(" + age + ")";
        }
    }
    List<Person> people = Arrays.asList(
        new Person("Kim", 25),
        new Person("Lee", 30),
        new Person("Park", 20)
    );        
        
    // 나이 기준 정렬
    people.sort(Comparator.comparing(Person::getAge));
    // people = [Person("Park", 20), Person("Kim", 25), Person("Lee", 30)]

    // 이름 기준 정렬
    people.sort(Comparator.comparing(Person::getName));
    // people = [Person("Kim", 25), Person("Lee", 30), Person("Park", 20)]
        
    // 나이 기준 정렬 후, 이름으로 정렬
    people.sort(Comparator.comparing(Person::getAge).thenComparing(Person::getName));
    // people = [Person("Park", 20), Person("Kim", 25), Person("Lee", 30)]

 

 

최소값, 최대값 (Min / Max)

    List<Integer> numbers = Arrays.asList(5, 2, 8, 1, 9);

    // 최소값
    int min = Collections.min(numbers);
    // min = 1

    // 최대값
    int max = Collections.max(numbers);
    // max = 9

 

 

빈도 (Frequency)

    List<String> words = Arrays.asList("apple", "banana", "apple", "cherry", "banana");
    
    // 빈도 계산
    int count = Collections.frequency(words, "apple");
    // count = 2

 

 

이진 탐색 (Binary Search)

    List<Integer> numbers = Arrays.asList(1, 3, 5, 7, 9);

    // 정렬 필수!!!!
    Collections.sort(numbers);  

    // 5의 위치 탐색
    int index = Collections.binarySearch(numbers, 5);
    // index = 2

 

 

요소 치환 (Replace)

    List<String> words = Arrays.asList("apple", "banana", "apple");

    Collections.replaceAll(words, "apple", "pear");
    
    // words = [pear, banana, pear]

 

 

역순 변환 (Reversing)

    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

    Collections.reverse(numbers);

    // numbers = [5, 4, 3, 2, 1]

 

 

요소 회전 (Rotation)

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// 요소를 2칸 이동
Collections.rotate(numbers, 2); 

// numbers = [4, 5, 1, 2, 3]

'Java > 기본 문법' 카테고리의 다른 글

Collection 주요 기능 - Map  (0) 2026.06.11
쓰레드(Thread)  (0) 2026.06.11
스트림(Stream)  (0) 2026.06.04
인터페이스(interface)  (0) 2026.06.04
람다(Lambda)  (0) 2026.05.21