집합 자료형인 set의 특징은
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)
print(a.intersection(b)) # 교집합: {2, 3}
print(a | b)
print(a.union(b)) # 합집합: {1, 2, 3, 4}
print(a - b)
print(a.difference(b)) # 차집합: {1}
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3));
Set<Integer> b = new HashSet<>(Arrays.asList(2, 3, 4));
// 교집합
Set<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b);
System.out.println("교집합: " + intersection); // [2, 3]
// 합집합
Set<Integer> union = new HashSet<>(a);
union.addAll(b);
System.out.println("합집합: " + union); // [1, 2, 3, 4]
// 차집합
Set<Integer> difference = new HashSet<>(a);
difference.removeAll(b);
System.out.println("차집합: " + difference); // [1]
}
}
이렇게 비교해보니 코드 길이 차이가... 😱 파이썬이 훨씬 간단하고 쉽긴 하다 !!