Java Array Merge merge(String[] array1, String[] array2)

Here you can find the source of merge(String[] array1, String[] array2)

Description

merge

License

Open Source License

Declaration

final static public String[] merge(String[] array1, String[] array2) 

Method Source Code

//package com.java2s;
/**/*from   ww  w .j av a 2s .c om*/
 * Copyright (C) 2011 Headvances Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
    
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This project aim to build a set of library/data to process 
 * the Vietnamese language and analyze the web data
 **/

import java.util.HashSet;
import java.util.Iterator;

public class Main {
    final static public String[] merge(String[] array1, String[] array2) {
        if (array1 == null && array2 == null)
            return null;
        if (array1 == null)
            return array2;
        if (array2 == null)
            return array1;
        HashSet<String> set = new HashSet<String>();
        for (String string : array1)
            set.add(string);
        for (String string : array2)
            set.add(string);
        return toArray(set);
    }

    final static public String[] merge(String[] array, String string) {
        if (array == null && string == null)
            return null;
        if (array == null)
            return new String[] { string };
        if (string == null)
            return array;
        for (int i = 0; i < array.length; i++) {
            if (string.equals(array[i]))
                return array;
        }
        String[] narray = new String[array.length + 1];
        for (int i = 0; i < array.length; i++)
            narray[i] = array[i];
        narray[array.length] = string;
        return narray;
    }

    static public String[] toArray(java.util.Collection<String> collection) {
        String[] array = new String[collection.size()];
        Iterator<String> i = collection.iterator();
        int index = 0;
        while (i.hasNext()) {
            array[index] = i.next();
            index++;
        }
        return array;
    }
}

Related

  1. merge(int[]... arrs)
  2. merge(long[] theArray, long[] workSpace, int lowPtr, int highPtr, int upperBound)
  3. merge(Object[][] array1, Object[][] array2)
  4. merge(String array[], String delimiter)
  5. merge(String[] array)
  6. merge(String[] strArr1, String[] strArr2)
  7. merge(String[] strArray)
  8. merge(String[]... arrays)
  9. merge(T[] array, T[] temp, int left, int middle, int right)