Arrays Merge/Remove Common Using Set - Java Algorithm

Java examples for Algorithm:Array

Description

Arrays Merge/Remove Common Using Set

Demo Code



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

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

        Set<Integer> a = new HashSet<>(Arrays.asList(1, 2, 3, 4));
        Set<Integer> b = new HashSet<>(Arrays.asList(3, 4, 5, 6));
        Set<Integer> result = new HashSet<>();

        //from www.j a v a  2 s . c  o m
        for (Integer el: a) {
          if (!b.contains(el)) {
            result.add(el);
          }
        }
        for (Integer el: b) {
          if (!a.contains(el)) {
            result.add(el);
          }
        }
        System.out.println("Uncommon elements of set a and set b is : "
            + result);
    }
  }

Related Tutorials