Study for Backend/Mathmatics Basic
[기초 수학] 집합
지미니박
2024. 3. 11. 15:47
집합( Set )
특정 조건에 맞는 원소들의 모임
집합 표현 방법
- 원소나열법
A = { 1, 2, 3, 4, 5 } , B = { 2, 4, 6, 8, 10 }
- 조건 제시법
A = { A | A는 정수 , 1<= A <= 5 }
B = { 2B | B는 정수, 1 <= B <= 5 }
- 벤 다이어그램
1. 교집합
두 집합이 공통으로 포함되는 원소로 이루어진 집합
- A ∩ B = { x | x ∈ A and x ∈ B }
2. 합집합
어느 하나에라도 속하는 원소들을 모두 모은 집합
- A ∪ B = { x | x ∈ A and x ∈ B }
3. 차집합
A( or B )에만 속하는 원소들의 집합
- A − B = { x | x ∈ A and x ∉ B }
4. 여집합
전체 집합( U ) 중 A의 원소가 아닌 것들의 집합
- Ac = { x | x ∈ U and x ∉ A }
//기초수학 - 집합
public class mySet {
public static void main(String[] args) {
//1. 자바에서 집합 사용 - HashSet
System.out.println("== HashSet ===");
HashSet set1 = new HashSet();
set1.add(1);
set1.add(1);
set1.add(1);
System.out.println("set1 = " + set1);
set1.add(2);
set1.add(3);
System.out.println("set1 = " + set1);
set1.remove(1);
System.out.println("set1 = " + set1);
System.out.println(set1.size());
System.out.println(set1.contains(2));
//집합 연산
System.out.println("== 집합 연산 ===");
//교집합 연산
HashSet a = new HashSet(Arrays.asList(1, 2, 3, 4, 5));
HashSet b = new HashSet(Arrays.asList(2, 4, 6, 8, 10));
//a.retainAll(b);
//System.out.println("교집합 : " + a);
//합집합 연산
//a.addAll(b);
//System.out.println("합집합 : " + a);
//차집합 연산
a.removeAll(b);
System.out.println("차집합 : " + a);
}
}