Java ArrayList Union unionSets(ArrayList s1, ArrayList s2)

Here you can find the source of unionSets(ArrayList s1, ArrayList s2)

Description

union two sets s1 and s2

License

Open Source License

Parameter

Parameter Description
s1 set s1
s2 set s2

Return

the union of two sets

Declaration

public static ArrayList<Integer> unionSets(ArrayList<Integer> s1, ArrayList<Integer> s2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//from   w  w w. j  a va  2s  .  co  m
     * union two sets s1 and s2
     *
     * @param s1
     *            set s1
     * @param s2
     *            set s2
     * @return the union of two sets
     */
    public static ArrayList<Integer> unionSets(ArrayList<Integer> s1, ArrayList<Integer> s2) {
        if (s2 == null) {
            return s1;
        }
        for (Integer i : s2) {
            if (!s1.contains(i)) {
                s1.add(i);
            }
        }
        return s1;
    }
}

Related

  1. union(ArrayList list1, ArrayList list2)
  2. Union(ArrayList list1, ArrayList list2)
  3. union(ArrayList a, ArrayList b)