\
2021.03.29 - [JAVA/JAVA(자바)설치 및 툴 이용(ECLIPSE)] - [JAVA] 자바 설치 및 환경 하기 JDK 1.8 버전
개발 환경
Window 10
JDK 1.8
이클립스 2020_03 버전
자바로 교집합 여집합 나타내기
결과 값
A = [1, 2, 3, 4, 5]
B = [4, 5, 6, 7, 8]
A ∩ B = [4, 5]
A ∪ B = [1, 2, 3, 4, 5, 6, 7, 8]
A - B = [1, 2, 3]
package coection.set;
import java.util.*;
class HashSetEx5 {
public static void main(String args[]) {
HashSet setA = new HashSet();
HashSet setB = new HashSet();
HashSet setHab = new HashSet();
HashSet setKyo = new HashSet();
HashSet setCha = new HashSet();
// set A 12345
setA.add("1"); setA.add("2");
setA.add("3"); setA.add("4");
setA.add("5");
System.out.println("A = "+setA);
// set A 45678
setB.add("4"); setB.add("5");
setB.add("6"); setB.add("7");
setB.add("8");
System.out.println("B = "+setB);
Iterator it = setB.iterator();
while(it.hasNext()) {
Object tmp = it.next();
if(setA.contains(tmp))
setKyo.add(tmp);
}
it = setA.iterator();
while(it.hasNext()) {
Object tmp = it.next();
if(!setB.contains(tmp))
setCha.add(tmp);
}
it = setA.iterator();
while(it.hasNext())
setHab.add(it.next());
it = setB.iterator();
while(it.hasNext())
setHab.add(it.next());
System.out.println("A ∩ B = "+setKyo); // 한글 ㄷ 을 누르고 한자키
System.out.println("A ∪ B = "+setHab); //
System.out.println("A - B = "+setCha);
}
}