olrlobt

[JAVA/자료구조] 리스트 List, Collections 클래스와 메서드의 활용 본문

Java/자료구조

[JAVA/자료구조] 리스트 List, Collections 클래스와 메서드의 활용

olrlobt 2023. 1. 14. 02:42

 

Collections 클래스 (java.util.Collections)

Collections 클래스는 Collection 인터페이스를 구현하는 List, Set, Map 등의 클래스에 대한 여러 추가 기능들을 제공하는 클래스이다. 대표적인 기능으로 정렬(sort), 병합(merge), 검색(search) 등의 기능을 제공하는 메서드가 있다.

 


Collections 사용 가능한 자료구조

  • List
  • Set
  • Map
  • Queue
  • Deque
  • ArrayList
  • LinkedList
  • Vector
  • Stack
  • HashSet
  • LinkedHashSet
  • TreeSet
  • ArrayDeque
  • PriorityQueue
  • HashMap
  • LinkedHashMap
  • TreeMap
  • EnumMap
  • WeakHashMap
  • IdentityHashMap

 


Collections.reverse()

리스트를 역순으로 뒤집는다. 

 

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(0,1,2,3,4,5,6,7,8,9);

        Collections.reverse(list);

        System.out.println(list);
    }

결과 :

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Collections.sort()

리스트를 오름차순으로 정렬한다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(3,2,1,4,9,8,7,6,0,5);

        Collections.sort(list);

        System.out.println(list);
    }

결과 :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

 

 

내림차 순으로 정렬하기 위해서는 Collections.reverseOrder()를 사용한다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(3,2,1,4,9,8,7,6,0,5);

        Collections.sort(list,Collections.reverseOrder());

        System.out.println(list);
    }

결과 :

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

 

 

 

 

 


Collections.max() , Collections.min()

max() : 리스트 요소 중에 최댓값을 출력한다.

min() : 리스트 요소 중에 최솟값을 출력한다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(3, 2, 1, 4, 9, 8, 7, 6, 0, 5);
        
        System.out.println(Collections.max(list));

        System.out.println(Collections.min(list));
    }

결과 :

9
0

 

 


Collections.shuffle()

리스트를 랜덤 한 순서로 변경한다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

        Collections.shuffle(list);
        System.out.println(list);

        Collections.shuffle(list); // 다른 결과를 보여주기위해 2번 셔플
        System.out.println(list);
    }

결과 :

[2, 9, 0, 4, 3, 6, 5, 8, 1, 7]
[0, 9, 3, 5, 8, 6, 1, 7, 2, 4]

 

 


Collections.disjoint()

두 리스트의 요소 중, 하나라도 같은 값이 있으면 True, 없으면 False를 반환한다.

 

실제로 잘 사용되지 않는 메서드이다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> list2 = Arrays.asList(5, 6, 7);

        System.out.println(Collections.disjoint(list, list2));

        List<Integer> list3 = Arrays.asList(1, 2, 3); // 두 리스트에
        List<Integer> list4 = Arrays.asList(3, 4, 5); // 같은 값 3이 존재한다.
        System.out.println(Collections.disjoint(list3, list4));
    }

결과 :

true
false

 


Collections.copy()

리스트를 다른 리스트로 복사한다.

 

이때 첫 번째 리스트가, 두 번째 리스트보다 리스트의 크기가 커야 한다.

 

만약, 두 번째 리스트가 더 크다면, 에러가 발생한다.

public static void main(String[] args) {

        List<Integer> list = Arrays.asList(1, 2, 3);
        List<Integer> list2 = Arrays.asList(5, 6, 7);

        Collections.copy(list, list2);

        System.out.println(list);
        System.out.println(list2);
    }

결과 :

[5, 6, 7]
[5, 6, 7]

 

 

public static void main(String[] args) {

    List<Integer> list = Arrays.asList(1, 2, 3, 4);
    List<Integer> list2 = Arrays.asList(5, 6, 7);

    Collections.copy(list, list2);

    System.out.println(list);
    System.out.println(list2);
}

결과 :

[5, 6, 7, 4]
[5, 6, 7]

 

 

 


참고 사이트: 

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

 

Collections (Java Platform SE 7 )

Rotates the elements in the specified list by the specified distance. After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive. (Thi

docs.oracle.com

https://www.javatpoint.com/collections-in-java

 

Collections in Java - javatpoint

Collections in java or collection framework in java with List, Set, Queue and Map implementation, hierarchy and methods of Java Collections framework, ArrayList, LinkedList, HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, TreeMap, PriorityQueue, A

www.javatpoint.com

 

Comments