JavaCode(review)

Set - Symmetric&Asymmetric ( Set Interface Bulk Operation) - collection

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2020. 7. 13. 01:54
반응형

https://edu.goorm.io/learn/lecture/41/바로실습-생활코딩-자바-java/lesson/792/set 그림참고

 

Set Interface Bulk Operation

A: {1 2 3},   B: {3 4 5}

Union 합집합 A.addAlll(B) ;
Subset 부분집합 A.containAll(B); -> false or sout(다른거 설정)
Intersect 교집합 A.retainAll(B) ; ->3
Difference 차집합
(Asymmetric difference)
A.removeAll(B); -> 1,2
Symmetric difference 
대칭차집합
Union - Intersection ->  1,2,4,5

 

Set이용이유? ex)addAll 하면 반복불가니, 반복되는거 알아서 count 1번만 함. (합집합)

 

예제)

package com.timbuchalka;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SetMain {
    public static void main(String[] args) {
        Set<Integer> squares = new HashSet<>();     //set define
        Set<Integer> cubes = new HashSet<>();       //set define

        for (int i = 1; i <= 100; i++) {      // 각 set initialization(value추가)
            squares.add(i * i); //제곱근
            cubes.add(i * i * i); //세제곱근
        }
        //각 set 사이즈 출력 (각각 100개)
        System.out.println("There are " + squares.size() + " squares and " + cubes.size() + " cubes.");

        //union  -> 196개(set은 반복불가로 count한번만.)
        Set<Integer> union = new HashSet<>(squares);  //squares값 가진 set 새로 정의
        union.addAll(cubes);
        System.out.println("Union contains " + union.size() + "  elements.");

        //intersection
        Set<Integer> intersection = new HashSet<>(squares); //squares값 가진 set새로 정의
        intersection.retainAll(cubes);
        System.out.println("Intersection contains " + intersection.size() + " elements.");
        for (int i : intersection) {
            System.out.println(i + " is the square of " + Math.sqrt(i) + " and the cube of " + Math.cbrt(i));
        }           //Math.sqrt() 제곱근, Math.cbrt() 세제곱근


        //string set define// Arrays.asList()이용해서.
        Set<String> words = new HashSet<>();
        String sentence = "one day in the year of the fox";
        String[] arrayWords = sentence.split(" ");
        words.addAll(Arrays.asList(arrayWords));
        //출력
        for (String s : words) {
            System.out.println(s);
        }

        //string set정의// Arrays.asList이용한
        Set<String> nature = new HashSet<>();
        Set<String> divine = new HashSet<>();
        
        String[] natureWords = {"all", "nature", "is", "but", "art", "unknown", "to", "thee"};
        nature.addAll(Arrays.asList(natureWords));

        String[] divineWords = {"to", "err", "is", "human", "to", "forgive", "divine"};
        divine.addAll(Arrays.asList(divineWords));

        //차집합 difference
        System.out.println("nature - divine:");
        Set<String> diff1 = new HashSet<>(nature); //값이nature인 set새로 define
        diff1.removeAll(divine);
        printSet(diff1);

        System.out.println("divine - nature:");
        Set<String> diff2 = new HashSet<>(divine);
        diff2.removeAll(nature);
        printSet(diff2);

        //symmetric difference 대칭차집합// union - intersection
        Set<String> unionTest = new HashSet<>(nature);  //union구하기
        unionTest.addAll(divine);
        Set<String> intersectionTest = new HashSet<>(nature);//intersection구하기
        intersectionTest.retainAll(divine);

        System.out.println("Symmetric difference"); // symmetric 계산
        unionTest.removeAll(intersectionTest);
        printSet(unionTest);

        
     //containAll 예시   
        if(nature.containsAll(intersectionTest)) {
            System.out.println("intersection is  subset of nature");
        }

        if(divine.containsAll(intersectionTest)) {
            System.out.println("intersection is a subset of divine");
        }
    }
//method
    private static void printSet(Set<String> set) {
        System.out.print("\t");
        for(String s : set) {
            System.out.print(s + " ");
        }
        System.out.println();
    }
}

//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=50233:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Finishing-Off-Sets-Source-code\out\production\SetAndHashSet com.timbuchalka.SetMain
There are 100 squares and 100 cubes.
Union contains 196  elements.
Intersection contains 4 elements.
4096 is the square of 64.0 and the cube of 16.0
1 is the square of 1.0 and the cube of 1.0
64 is the square of 8.0 and the cube of 4.0
729 is the square of 27.0 and the cube of 9.0
the
in
year
one
of
day
fox
nature - divine:
	all but art thee nature unknown 
divine - nature:
	err forgive divine human 
Symmetric difference
	all but art thee err nature forgive divine human unknown 
intersection is  subset of nature
intersection is a subset of divine

Process finished with exit code 0