집합 (Set)

sebeen·2025년 2월 13일

기초수학

목록 보기
1/8

HashSet 사용해서 구현해보기

  • 교집합: a.retainAll(b); //a에는 교집합 원소만 남게 됨
  • 합집합: a.addAll(b); //a에는 합집합 원소 들어감
  • 차집합: a.removeAll(b);//a에는 b의 원소들이 모두 제거됨
import java.util.Arrays;
import java.util.HashSet;

public class Main {
    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)); //집합에 2라는 데이터가 들어있는지


//      2. 집합 연산
        System.out.println("== 집합 연산 ==");

//      2-1. 교집합
        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); //a에는 교집합 원소만 남게 됨
        System.out.println("교집합: " + a);

//      2-2. 합집합
        a.addAll(b); //a에는 합집합 원소 들어감
        System.out.println("합집합: " + a);

//      2-3. 차집합
        a.removeAll(b);//a에는 b의 원소들이 모두 제거됨
        System.out.println("차집합 " + a);
    }

}

ArrayList를 사용한 집합 구현 실습 (집합 관련 연산 사용x)

// Practice
// ArrayList를 사용한 집합 구현 실습 (집합 관련 연산 사용 X)

import java.util.ArrayList;

class MySet {
    // ArrayList
    ArrayList<Integer> list;

    // 생성자1
    MySet() {
        this.list = new ArrayList<Integer>();
    }

    // 생성자 2
    MySet(int[] arr) {
        this.list = new ArrayList<Integer>();

        for (int item : arr) {
            this.list.add(item);
        }
    }

    // 원소 추가 (중복 X)
    public void add(int x) {
        for (int item : this.list) {
            if (item == x) { //먼저 중복된게 있는지 검사
                return;
            }
        }
        this.list.add(x);
    }

    // 교집합
    public MySet retainAll(MySet b) {
        MySet result = new MySet();

        for (int itemA : this.list) {
            for (int itemB : b.list) {
                if (itemA == itemB) {
                    result.add(itemA);
                }
            }
        }
        return result;
    }

    // 합집합
    public MySet addAll(MySet b) {
        MySet result = new MySet();

        for (int itemA : this.list) {
            result.add(itemA);
        }

        for (int itemB : b.list) {
            result.add(itemB);
        }
        //add는 위에서 이미 중복X로 만들어놨기때문에 이렇게만 해도 괜찮음.
        return result;
    }

    // 차집합
    public MySet removeAll(MySet b) {
        MySet result = new MySet();

        for(int itemA:this.list){
            boolean containFlag=false;

            for(int itemB:b.list){
                if(itemA==itemB){
                    containFlag=true;
                    break;
                }
            }

            if(!containFlag) {
                result.add(itemA);
            }
        }
        return result;
    }
}

public class Practice1 {
    public static void main(String[] args) {

//      Test code
        MySet a = new MySet();

        a.add(1);
        a.add(1);
        a.add(1);
        System.out.println(a.list);
        a.add(2);
        a.add(3);
        System.out.println(a.list);

        a = new MySet(new int[]{1, 2, 3, 4, 5});
        MySet b = new MySet(new int[]{2, 4, 6, 8, 10});
        System.out.println("a: " + a.list);
        System.out.println("b: " + b.list);

        MySet result = a.retainAll(b);
        System.out.println("교집합: " + result.list);

        result = a.addAll(b);
        System.out.println("합집합: " + result.list);

        result = a.removeAll(b);
        System.out.println("차집합: " + result.list);
    }
}

0개의 댓글