Java Set union

Description

Java Set union

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

public class Main {
  public static <T> Set<T> union(Set<T> s1, Set<T> s2) {
    Set<T> s1Unions2 = new HashSet<>(s1);
    s1Unions2.addAll(s2);/*from w  w  w .ja va  2s.  com*/
    return s1Unions2;
  }

  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 = union(s1, s2);
    
    System.out.println(s3);
  }
}



PreviousNext

Related