본문 바로가기

Java

[Java] Collections 컬렉션을 위한 메서드

Collections 

컬렉션을 위한 메서드(static)를 제공

 

1. 컬렉션 채우기, 복사, 정렬, 검색

fill( )

copy( )

sort( )

binarySearch( )

 

 

 

2. 컬렉션의 동기화

synchronizedXXX( )

남궁성 유튜브 출처: https://www.youtube.com/watch?v=u0pJGFyvrqc&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp&index=134

 

 

List syncList = Collections.synchronizedList(new ArrayList(...));

 

필요할 때만 동기화 하도록 설계가 바뀌었다.

 

 

 

2. 변경불가(readOnly) 컬렉션 만들기 

unmodifiableXXX( )

 

 

 

3. 싱글톤 컬렉션 만들기(객체 1개만 저장하는 컬렉션)

 

 

 

 

4. 한 종류의 객체만 저장하는 컬렉션 만들기

List list = new ArrayList();
List checkedList - checkedList(list, String); // String만 저장가능
checkedList.add("abc");
checkdList.add(new Integer(3)); // 에러

 

JDK(1.5 제네릭스 타입전에 나옴)

 

 

public class Ex1 {

    public static void main(String[] args) {
        List list = new ArrayList();
        System.out.println(list);

        Collections.addAll(list, 1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println(list);
        
        Collections.rotate(list, 2); // 오른쪽으로 두 칸씩 이동
        System.out.println(list);

        Collections.swap(list, 0, 2); // 첫 번째와 세 번째 교환(swap)
        System.out.println(list);

        Collections.shuffle(list); // 저장된 요소의 위치를 임의로 변경
        System.out.println(list);

        Collections.sort(list, Collections.reverseOrder()); // 역순 정렬 reverse(list);와 동일
        System.out.println(list);

        Collections.sort(list); // 정렬
        System.out.println(list);
        
        int idx = Collections.binarySearch(list, 3); // 3이 저장된 위치(index)를 반환
        System.out.println("idx of 3 = " + idx);

        System.out.println("max = " + Collections.max(list));
        System.out.println("min = " + Collections.min(list));
        System.out.println("min = " + Collections.max(list, Collections.reverseOrder())); // 최솟값

        Collections.fill(list, 9); // list를 9로 채운다.
        System.out.println("list = " + list);

        // list와 같은 크기의 새로운 list를 생성하고 2로 채운다. 단, 결과는 변경불가
        List newList = Collections.nCopies(list.size(), 2);

        System.out.println(Collections.disjoint(list, newList)); // 공통요소가 없으면 true

        Collections.copy(list, newList);
        System.out.println("newList = " + newList);
        System.out.println("list = " + list);

        Collections.replaceAll(list, 2, 1);
        System.out.println("list = " + list);

        Enumeration e = Collections.enumeration(list);
        ArrayList list2 = list(e);

        System.out.println("list2 = " + list2);


    }
}
[]
[1, 2, 3, 4, 5, 6, 7, 8]
[7, 8, 1, 2, 3, 4, 5, 6]
[1, 8, 7, 2, 3, 4, 5, 6]
[8, 6, 2, 7, 3, 4, 1, 5]
[8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8]
idx of 3 = 2
max = 8
min = 1
min = 1
list = [9, 9, 9, 9, 9, 9, 9, 9]
true
newList = [2, 2, 2, 2, 2, 2, 2, 2]
list = [2, 2, 2, 2, 2, 2, 2, 2]
list = [1, 1, 1, 1, 1, 1, 1, 1]
list2 = [1, 1, 1, 1, 1, 1, 1, 1]

'Java' 카테고리의 다른 글

[Java] Iterator<E>, Map<K, V>  (0) 2024.05.09
[Java] 지네릭스란?  (0) 2024.05.09
[Java] HashMap 키와 값  (0) 2024.05.08
[Java] TreeSet 범위 탐색, 정렬  (0) 2024.05.08
[Java] HashSet 객체 중복제거, 집합  (0) 2024.05.08