1. Collections 컬렉션을 위션 메서드(static)
- 컬렉션 채우기, 복사, 정렬, 검색 - fill(), copy(), sort(), binarySearch() 등
- 컬렉션의 동기화 - synchronizedXXX()
static Collection synchronizedCollection(Collection c)
static List synchronizedList(List list)
static Set synchronizedSet(Set s)
static Map synchronizedMap(Map m)
static SortedSet synchronizedSortedSet(SortedSet s)
static SortedMap synchronizedSortedMap(SortedMap m)
List syncList = Collections.synchronizedList(new ArrayList(...));
- 변경불가(readOnly) 컬렉션 만들기 - numodifiableXXX()
static Collection unmodifiableCollection (Collection c)
static List unmodifiableList(List list)
static Set unmodifiableSet(Set s)
static Map unmodifiableMap(Map m)
static NavigableSet unmodifiableNavigableSet(NavigableSet s)
static SortedSet unmodifiableSortedSet(SortedSet s)
static NavigableMap unmodifiableNavigableMap(NavigableMap m)
static SortedMap unmodifiableSortedMap(SortedMap m)
- 싱글톤 컬렉션 만들기 - singletonXXX()
static List singletonList(Object o)
static Set singleton(Object o) // singletonSet이 아님에 주의
static Map singletonMap(Object key, Object value)
- 한 종류의 객체만 저장하는 컬렉션 만들기 - checkedXXX()
static Collection checkedCollection(Collection c, Class type)
static List checkedList(List list, Class type)
static Set checkedSet(Set s, Class type)
static Map checkedMap(Map m, Class type)
static Queue checkedQueue(Queue queue, Class type)
static NavigableSet checkedNavigableSet(NavigableSet s, Class type)
static SortedSet checkedSortedSet(SortedSet s, Class type)
static NavigableMap checkedNavigableMap(NavigableMap m, Class keyType, Class valueType)
static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType)
List list = new ArrayList();
List checkedList = checkedList(list, String.class); // String만 저장가능
2. 예제
List list = new ArrayList();
System.out.println(list);
// Collections -> import static java.util.Collection.*로 생략가능
Collections.addAll(list,1,2,3,4,5); // 추가 메서드
System.out.println(list); // [1, 2, 3, 4, 5]
Collections.rotate(list, 2); // 오른쪽으로 두 칸씩 이동(반시계방향으로 두번 회전)
System.out.println(list); // [4, 5, 1, 2, 3]
Collections.swap(list, 0, 2); // 첫 번째와 세 번째를 교환(swap)
System.out.println(list); // [1, 5, 4, 2, 3]
Collections.shuffle(list); // 저장된 요소의 위치를 임의로 변경
System.out.println(list); // [3, 5, 1, 4, 2]
Collections.sort(list, reverseOrder()); // 역순 정렬 reverse(list)와 동일
System.out.println(list); // [5, 4, 3, 2, 1]
Collections.sort(list); // 정렬
System.out.println(list); // [1, 2, 3, 4, 5]
int idx = Collections.binarySearch(list, 3); // 3이 저장된 위치(index)를 반환
System.out.println("index of 3 = " + idx); // index of 3 = 2
System.out.println("max="+Collections.max(list)); // max=5
System.out.println("min="+Collections.min(list)); // min=1
System.out.println("min="+Collections.max(list, reverseOrder())); // min=1
Collections.fill(list, 9); // list를 9로 채운다.
System.out.println("list = "+ list); // list = [9, 9, 9, 9, 9]
// list와 같은 크기의 새로운 list를 생성하고 2로 채운다. 단, 결과는 변경불가
List newList = Collections.nCopies(list.size(), 2);
System.out.println("newList="+newList); // newList=[2, 2, 2, 2, 2]
System.out.println(Collections.disjoint(list, newList)); // 공통요소가 없으면 true
Collections.copy(list, newList);
System.out.println("newList="+newList); // newList=[2, 2, 2, 2, 2]
System.out.println("list="+list); // list=[2, 2, 2, 2, 2]
Collections.replaceAll(list, 2, 1);
System.out.println("list = "+list); // list = [1, 1, 1, 1, 1]
// Iterator와 같은 역할
Enumeration e = enumeration(list);
ArrayList list2 = list(e);
System.out.println("list2="+list2); // list2=[1, 1, 1, 1, 1]
'Java' 카테고리의 다른 글
115. 타입 변수 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.03 |
---|---|
114. 지네릭스 - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.02 |
112. HashMap - 패스트캠퍼스 백엔드 부트캠프 3기 (2) | 2025.01.02 |
111. TreeSet - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.02 |
110. HashSet(2) - 패스트캠퍼스 백엔드 부트캠프 3기 (0) | 2025.01.02 |