다음 요구사항에 따른 계산을 수행하여 결과를 출력하시오.
철수와 영희는 각자 국내에서 상영하는 영화를 다음과 같이 봤습니다.
- 철수 : 이상한 나라의 수학자, 더 배트맨, 인민을 위해 복무하라, 블랙라이트
- 영희 : 더 배트맨, 스파이더맨 : 노웨이 홈, 블랙라이트, 우리가 사랑이라고 믿는 것
(Q1) 철수와 영희가 둘 다 본 영화 목록을 출력하세요
(Q2) 철수와 영희 중 한 명만 본 영화 목록을 출력하세요
메인 메소드
package day19_api.util.collection2;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Test07 {
public static void main(String[] args) {
// 철수의 Set 생성 및 값 입력
Set<String> chulsoo = new TreeSet<>();
chulsoo.add("이상한 나라의 수학자");
chulsoo.add("더 배트맨");
chulsoo.add("인민을 위해 복무하라");
chulsoo.add("블랙라이트");
// 영희의 Set 생성
Set<String> younghee = new TreeSet<>();
younghee.add("더 배트맨");
younghee.add("스파이더맨 : 노웨이 홈");
younghee.add("블랙라이트");
younghee.add("우리가 사랑이라고 믿는 것");
// (Q) 둘 다 본 영화
// 둘 다 본 영화 Set 생성
Set<String> common = new TreeSet<>();
common.addAll(chulsoo);
common.retainAll(younghee);
System.out.println("둘 다 본 영화 : " + common);
// (Q) 둘 중 한명만 본 영화
// 1) (A ∪ B) - (A ∩ B)
Set<String> complement1 = new TreeSet<>();
complement1.addAll(chulsoo);
complement1.addAll(younghee);
complement1.removeAll(common); // 위에서 구한 common의 원소를 제거
System.out.println("둘 중 한명만 본 영화 : " + complement1);
// 2) (A - B) ∪ (B - A)
Set<String> ab = new TreeSet<>();
ab.addAll(chulsoo);
ab.removeAll(younghee);
Set<String> ba = new TreeSet<>();
ba.addAll(younghee);
ba.removeAll(chulsoo);
Set<String> complement2 = new TreeSet<>();
complement2.addAll(ab);
complement2.addAll(ba);
System.out.println("둘 중 한명만 본 영화 : " + complement2);
}
}
Set의 메소드 일부 (Set은 인터페이스이며 TreeSet의 상위 클래스)
참조변수.addAll(Collection <? extends E> c) | 지정한 Collection에 포함된 모든 요소가 없는 경우 Set에 추가 | boolean |
참조변수.removeAll(Collection <?> c) | 지정한 Collection에 포함된 모든 요소를 해당 Set에서 제거 | boolean |
참조변수.retainAll(Collection c) | 지정한 Collection에 포함된 모든 요소만 해당 Set에 유지 | boolean |