setdemo.java

来自「java课件 java课件 java课件 java课件」· Java 代码 · 共 27 行

JAVA
27
字号
  //SetDemo.java
import java.util.*;
public class SetDemo {
    public static void main(String args[]) {
        Set<String> s1 = new HashSet<String>();
        Set<String> s2 = new HashSet<String>();
        s1.add(new String("one"));
        s1.add(new String("two"));
        s1.add(new String("three"));
        
        s2.add(new String("two"));
        s2.add(new String("three"));
        s2.add(new String("four"));
        
        Set<String> union = new HashSet<String>(s1);
        union.addAll(s2);
        Set<String> intersection = new HashSet<String>(s1);
        intersection.retainAll(s2);
        Set<String> difference = new HashSet<String>(s1);
        difference.removeAll(s2);

        System.out.println(union);
        System.out.println(intersection);
        System.out.println(difference);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?