Java Collections Framework

자바 컬렉션 프레임워크 (Java Collections Framework)

Java Collections Framework는 데이터를 저장하고 관리하는 데 유용한 클래스와 인터페이스들을 제공합니다. 주요 인터페이스에는 List, Set, Map 등이 포함되어 있습니다. 여기서는 각 인터페이스와 관련된 주요 클래스 및 기능을 설명하겠습니다.

리스트 (List)

리스트는 순서가 있는 데이터의 집합으로, 데이터의 중복을 허용합니다. 리스트의 주요 구현 클래스로는 ArrayList, LinkedList, Vector 등이 있습니다.

리스트 예제:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
// ArrayList 예제
List<String> arrayList = new ArrayList<>();
arrayList.add("사과");
arrayList.add("바나나");
arrayList.add("딸기");
System.out.println("ArrayList:");
for (String fruit : arrayList) {
System.out.println(fruit);
}
// LinkedList 예제
List<Integer> linkedList = new LinkedList<>();
linkedList.add(10);
linkedList.add(20);
linkedList.add(30);
System.out.println("\nLinkedList:");
for (int number : linkedList) {
System.out.println(number);
}
}
}
import java.util.*; public class Main { public static void main(String[] args) { // ArrayList 예제 List<String> arrayList = new ArrayList<>(); arrayList.add("사과"); arrayList.add("바나나"); arrayList.add("딸기"); System.out.println("ArrayList:"); for (String fruit : arrayList) { System.out.println(fruit); } // LinkedList 예제 List<Integer> linkedList = new LinkedList<>(); linkedList.add(10); linkedList.add(20); linkedList.add(30); System.out.println("\nLinkedList:"); for (int number : linkedList) { System.out.println(number); } } }
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // ArrayList 예제
        List<String> arrayList = new ArrayList<>();
        arrayList.add("사과");
        arrayList.add("바나나");
        arrayList.add("딸기");

        System.out.println("ArrayList:");
        for (String fruit : arrayList) {
            System.out.println(fruit);
        }

        // LinkedList 예제
        List<Integer> linkedList = new LinkedList<>();
        linkedList.add(10);
        linkedList.add(20);
        linkedList.add(30);

        System.out.println("\nLinkedList:");
        for (int number : linkedList) {
            System.out.println(number);
        }
    }
}

위의 예제에서는 ArrayListLinkedList를 생성하고 각각 데이터를 추가한 후 출력하는 방법을 보여줍니다.

세트 (Set)

세트는 중복을 허용하지 않는 데이터의 집합입니다. 세트의 주요 구현 클래스로는 HashSet, TreeSet, LinkedHashSet 등이 있습니다.

세트 예제:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
// HashSet 예제
Set<String> hashSet = new HashSet<>();
hashSet.add("사과");
hashSet.add("바나나");
hashSet.add("사과"); // 중복된 항목은 추가되지 않음
System.out.println("HashSet:");
for (String fruit : hashSet) {
System.out.println(fruit);
}
// TreeSet 예제 (자동 정렬)
Set<Integer> treeSet = new TreeSet<>();
treeSet.add(30);
treeSet.add(10);
treeSet.add(20);
System.out.println("\nTreeSet:");
for (int number : treeSet) {
System.out.println(number);
}
}
}
import java.util.*; public class Main { public static void main(String[] args) { // HashSet 예제 Set<String> hashSet = new HashSet<>(); hashSet.add("사과"); hashSet.add("바나나"); hashSet.add("사과"); // 중복된 항목은 추가되지 않음 System.out.println("HashSet:"); for (String fruit : hashSet) { System.out.println(fruit); } // TreeSet 예제 (자동 정렬) Set<Integer> treeSet = new TreeSet<>(); treeSet.add(30); treeSet.add(10); treeSet.add(20); System.out.println("\nTreeSet:"); for (int number : treeSet) { System.out.println(number); } } }
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // HashSet 예제
        Set<String> hashSet = new HashSet<>();
        hashSet.add("사과");
        hashSet.add("바나나");
        hashSet.add("사과"); // 중복된 항목은 추가되지 않음

        System.out.println("HashSet:");
        for (String fruit : hashSet) {
            System.out.println(fruit);
        }

        // TreeSet 예제 (자동 정렬)
        Set<Integer> treeSet = new TreeSet<>();
        treeSet.add(30);
        treeSet.add(10);
        treeSet.add(20);

        System.out.println("\nTreeSet:");
        for (int number : treeSet) {
            System.out.println(number);
        }
    }
}

위의 예제에서는 HashSetTreeSet을 생성하고 각각 데이터를 추가한 후 출력하는 방법을 보여줍니다.

맵 (Map)

맵은 키-값(key-value) 쌍으로 데이터를 저장하는 구조입니다. 맵의 주요 구현 클래스로는 HashMap, TreeMap, LinkedHashMap 등이 있습니다.

맵 예제:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
// HashMap 예제
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("One", 1);
hashMap.put("Two", 2);
hashMap.put("Three", 3);
System.out.println("HashMap:");
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
// TreeMap 예제 (키를 기준으로 자동 정렬)
Map<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3, "Three");
treeMap.put(1, "One");
treeMap.put(2, "Two");
System.out.println("\nTreeMap:");
for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
import java.util.*; public class Main { public static void main(String[] args) { // HashMap 예제 Map<String, Integer> hashMap = new HashMap<>(); hashMap.put("One", 1); hashMap.put("Two", 2); hashMap.put("Three", 3); System.out.println("HashMap:"); for (Map.Entry<String, Integer> entry : hashMap.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } // TreeMap 예제 (키를 기준으로 자동 정렬) Map<Integer, String> treeMap = new TreeMap<>(); treeMap.put(3, "Three"); treeMap.put(1, "One"); treeMap.put(2, "Two"); System.out.println("\nTreeMap:"); for (Map.Entry<Integer, String> entry : treeMap.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } }
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // HashMap 예제
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("One", 1);
        hashMap.put("Two", 2);
        hashMap.put("Three", 3);

        System.out.println("HashMap:");
        for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }

        // TreeMap 예제 (키를 기준으로 자동 정렬)
        Map<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(3, "Three");
        treeMap.put(1, "One");
        treeMap.put(2, "Two");

        System.out.println("\nTreeMap:");
        for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        }
    }
}

위의 예제에서는 HashMapTreeMap을 생성하고 각각 데이터를 추가한 후 출력하는 방법을 보여줍니다.

컬렉션 유틸리티 (Collections Utility)

Java는 Collections 클래스를 통해 컬렉션 관련 유틸리티 기능을 제공합니다. 이 클래스는 정렬, 검색, 변경 등의 여러 유틸리티 메서드를 제공하여 컬렉션을 다루는 데 유용합니다.

Collections 유틸리티 예제:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("사과");
list.add("바나나");
list.add("딸기");
System.out.println("정렬 전:");
for (String fruit : list) {
System.out.println(fruit);
}
Collections.sort(list); // 리스트 정렬
System.out.println("\n정렬 후:");
for (String fruit : list) {
System.out.println(fruit);
}
}
}
import java.util.*; public class Main { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("사과"); list.add("바나나"); list.add("딸기"); System.out.println("정렬 전:"); for (String fruit : list) { System.out.println(fruit); } Collections.sort(list); // 리스트 정렬 System.out.println("\n정렬 후:"); for (String fruit : list) { System.out.println(fruit); } } }
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("사과");
        list.add("바나나");
        list.add("딸기");

        System.out.println("정렬 전:");
        for (String fruit : list) {
            System.out.println(fruit);
        }

        Collections.sort(list); // 리스트 정렬

        System.out.println("\n정렬 후:");
        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}

위의 예제에서는 Collections 클래스의 sort() 메서드를 사용하여 리스트를 정렬하는 방법을 보여줍니다.

자바 컬렉션 프레임워크는 데이터를 효율적으로 관리하고 다루기 위한 강력한 도구를 제공합니다. 각 컬렉션 인터페이스와 구현 클래스들을 잘 이해하고 활용하면, 다양한 데이터 구조를 유연하게 다룰 수 있습니다.

Leave a Reply

Your email address will not be published. Required fields are marked *