자바에서 데이터 그룹을 효과적으로 저장, 관리, 처리할 수 있는 클래스와 인터페이스의 집합
1-1. ArrayList
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
//String 데이터형 리스트 객체 생성
List<String> arrayList = new ArrayList<>();
// 요소 추가
arrayList.add("fever");
arrayList.add("max");
arrayList.add("cookie");
// 요소 출력
for (String str : arrayList) {
System.out.println(str);
}
}
}
💻 출력 결과
fever
max
cookie
1-2. LinkedList
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample {
public static void main(String[] args) {
// LinkedList 생성
List<String> list = new LinkedList<>();
// 노드로 연결된 리스트 구조
// 삽입 및 삭제가 빠르고 중간에 요소를 삽입, 삭제하는 데 유용
list.add("Apple");
list.add("Banana");
list.add("Orange");
// 중간에 요소 삽입 (1번 인덱스에 추가)
linkedList.add(1, "Grapes");
// 출력
System.out.println("첫번째 출력");
for (String fruit : list) {
System.out.println(fruit);
}
// 중간의 요소 삭제
linkedList.remove(2); (2번 인덱스 삭제)
// 출력
System.out.println("두번째 출력");
for (String fruit : list) {
System.out.println(fruit);
}
}
}
💻 출력 결과
첫번째 출력
Apple
Grapes
Banana
Orange
두번째 출력
Apple
Banana
Orange
1-3 Vector
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
// Vector 생성
Vector<Integer> vector = new Vector<>();
// 스레드 1이 요소 추가
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
vector.add(i);
System.out.println("Thread 1: " + i);
}
});
// 스레드 2가 요소 추가
Thread thread2 = new Thread(() -> {
for (int i = 5; i < 10; i++) {
vector.add(i);
System.out.println("Thread 2: " + i);
}
});
// 스레드 1과 스레드 2 동시 실행
thread1.start();
thread2.start();
// 메인 스레드가 기다림
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 출력
System.out.println("Vector");
for (Integer value : vector) {
System.out.println(value);
}
}
}
2-1. HashSet
import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
public static void main(String[] args) {
// HashSet 생성
Set<String> hashSet = new HashSet<>();
// 해시 테이블을 기반으로 한 집합
// 중복을 허용하지 않고, 순서가 없음
hashSet.add("Apple");
hashSet.add("Banana");
hashSet.add("Orange");
// 중복된 요소 추가 시도
hashSet.add("Apple"); // 중복된 요소는 허용되지 않음
// 출력
System.out.println("HashSet");
for (String fruit : hashSet) {
System.out.println(fruit);
}
}
}
💻 출력 결과
HashSet:
Apple
Grapes
Orange
2-3. LinkedHashSet
2-4. TreeSet
3-1. HashMap
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
// HashMap 생성
HashMap<String, Integer> hashMap = new HashMap<>();
// 값 추가
hashMap.put("One", 1);
hashMap.put("Two", 2);
hashMap.put("Three", 3);
// 값 조회
System.out.println("key 'Two': " + hashMap.get("Two"));
}
}
💻 출력 결과
key 'Two': 2
3-2. LinkedHashMap
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapExample {
public static void main(String[] args) {
// LinkedHashMap 생성
LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
// 값 추가
linkedHashMap.put("One", 1);
linkedHashMap.put("Two", 2);
linkedHashMap.put("Three", 3);
// 순서 출력
for (Map.Entry<String, Integer> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
💻 출력 결과
One: 1
Two: 2
Three: 3
3-3. TreeMap
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
// TreeMap 생성
TreeMap<String, Integer> treeMap = new TreeMap<>();
// 값 추가
treeMap.put("Three", 3);
treeMap.put("One", 1);
treeMap.put("Two", 2);
// 정렬된 순서 출력
for (String key : treeMap.keySet()) {
System.out.println(key + ": " + treeMap.get(key));
}
}
}
💻 출력 결과
One: 1
Two: 2
Three: 3
4-1. PriorityQueue
import java.util.PriorityQueue;
public class PriorityQueueExample {
public static void main(String[] args) {
// PriorityQueue 생성
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
// 값 추가
priorityQueue.offer(3);
priorityQueue.offer(1);
priorityQueue.offer(2);
// 우선순위에 따라 출력
while (!priorityQueue.isEmpty()) {
System.out.println(priorityQueue.poll());
}
}
}
💻 출력 결과
1
2
3