7. Java 자료 구조 Collection

Java에서 Collection은 객체들의 그룹을 저장하고 조작하기 위한 인터페이스와 클래스들의 집합을 말합니다. Collection은 다양한 자료 구조를 구현한 클래스들을 제공하며, 객체들을 쉽게 추가, 제거, 조회 및 조작할 수 있는 다양한 메서드를 제공합니다. Collection 인터페이스의 주요 하위 인터페이스는 List, Set, Queue입니다.

1. List

  • List는 순서가 있는 객체의 컬렉션으로, 중복된 요소를 허용합니다.

  • 주요 구현 클래스로는 ArrayList, LinkedList, Vector 등이 있습니다.

예시:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple");
System.out.println(list);  // 출력: [Apple, Banana, Apple]

2. Set

  • Set은 순서가 없는 객체의 컬렉션으로, 중복된 요소를 허용하지 않습니다.

  • 주요 구현 클래스로는 HashSet, TreeSet 등이 있습니다.

예시:

Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple");
System.out.println(set);   // 출력: [Apple, Banana]

3. Queue

  • Queue는 요소들을 순서대로 저장하고 처리하는 컬렉션입니다. 일반적으로 FIFO(First-In-First-Out) 방식으로 작동합니다.

  • 주요 구현 클래스로는 LinkedList, PriorityQueue 등이 있습니다.

예시:

Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
System.out.println(queue.poll());   // 출력: Apple
System.out.println(queue.poll());   // 출력: Banana

Java의 Collection 프레임워크는 다양한 자료 구조를 제공하여 데이터를 관리하고 처리하는 데 유용합니다. Collection 클래스들은 다양한 메서드를 제공하여 데이터 추가, 삭제, 조회 등을 쉽게 수행할 수 있습니다. 이를 통해 객체들의 그룹을 효율적으로 다룰 수 있으며, 프로그램의 개발과 유지 보수를 용이하게 할 수 있습니다.

Last updated