Java Set Union union(Set left, Set right)

Here you can find the source of union(Set left, Set right)

Description

union

License

Open Source License

Declaration

public static <T> Set<T> union(Set<T> left, Set<T> right) 

Method Source Code

//package com.java2s;
/**/*w w w.ja v a  2 s .co  m*/
 * This file is part of journey-neo4j-plugin. journey-neo4j-plugin is a neo4j server extension that provides out-of-box action path analysis features on top of the graph database.
 *
 * Copyright 2015 ThoughtWorks, Inc. and Pengchao Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    public static <T> Set<T> union(Set<T> left, Set<T> right) {
        Set<T> smaller;
        Set<T> bigger;
        if (left.size() > right.size()) {
            bigger = left;
            smaller = right;
        } else {
            bigger = right;
            smaller = left;
        }

        HashSet<T> result = new HashSet<>(bigger);
        result.addAll(smaller);
        return result;
    }
}

Related

  1. union(final Iterable> elements)
  2. union(final Set set1, final Set set2)
  3. union(final Set... elements)
  4. union(Set one, Set two)
  5. union(Set a, Set b)
  6. union(Set setA, Set setB)
  7. union(Set setA, Set setB)
  8. union(Set... sets)
  9. union(String[] set1, String[] set2)