Java Set difference

Description

Java Set difference


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

public class Main {
  public static <T> Set<T> difference(Set<T> s1, Set<T> s2) {
    Set<T> s1Differences2 = new HashSet<>(s1);
    s1Differences2.removeAll(s2);//from   w  w w . j a  v  a2s  . c  o m
    return s1Differences2;
  }

  public static void main(String[] args) {
    Set<String> s1 = new HashSet<>();
    s1.add("HTML");
    s1.add("CSS");
    s1.add("XML");

    Set<String> s2 = new HashSet<>();
    s2.add("Java");
    s2.add("XML");
    s2.add("Javascript");
    s2.add("CSS");

    System.out.println(s1);
    System.out.println(s2);

    Set<String> s3 = difference(s1, s2);

    System.out.println(s3);

    s3 = difference(s2, s1);

    System.out.println(s3);
  }
}



PreviousNext

Related