Java Array Merge mergeStringArrays(String array1[], String array2[], String array3[])

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

Description

Merge three string arrays

License

Apache License

Parameter

Parameter Description
array1 Array of strings
array2 Array of strings
array3 Array of strings

Return

array Merged array containing each of the elements of array1, array2 and array3

Declaration

public static String[] mergeStringArrays(String array1[], String array2[], String array3[]) 

Method Source Code


//package com.java2s;
/*//w  w w . jav  a  2  s . c o m
 * Copyright 2011-2015 Kay Stenschke
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * This String utility or util method can be used to merge 2 arrays of
     * string values. If the input arrays are like this array1 = {"a", "b" ,
     * "c"} array2 = {"c", "d", "e"} Then the output array will have {"a", "b" ,
     * "c", "d", "e"}
     *
     * This takes care of eliminating duplicates and checks null values.
     *
     * @param   array1   Array of strings
     * @param   array2   Array of strings
     * @return  array    Merged array containing each of the elements of array1 and array2
     */
    public static String[] mergeStringArrays(String array1[], String array2[]) {
        if (array1 == null || array1.length == 0) {
            return array2;
        }
        if (array2 == null || array2.length == 0) {
            return array1;
        }

        List<String> array1List = Arrays.asList(array1);
        List<String> array2List = Arrays.asList(array2);

        List<String> result = new ArrayList<String>(array1List);

        List<String> tmp = new ArrayList<String>(array1List);
        tmp.retainAll(array2List);

        result.removeAll(tmp);
        result.addAll(array2List);

        return result.toArray(new String[result.size()]);
    }

    /**
     * Merge three string arrays
     *
     * @param   array1     Array of strings
     * @param   array2     Array of strings
     * @param   array3     Array of strings
     * @return  array    Merged array containing each of the elements of array1, array2 and array3
     */
    public static String[] mergeStringArrays(String array1[], String array2[], String array3[]) {
        return mergeStringArrays(mergeStringArrays(array1, array2), array3);
    }
}

Related

  1. mergeStringArray(String[] ary1, String[] ary2)
  2. mergeStringArray(String[] inp1, String[] inp2)
  3. mergeStringArray(String[] input)
  4. mergeStringArrayFromIndex(String[] arrayString, int i)
  5. mergeStringArrays(String array1[], String array2[])
  6. mergeStringArrays(String[] array1, String[] array2)
  7. mergeStringArrays(String[][] arrayOfArrays)
  8. mergeStrings(String[] x, String[] y)
  9. mergeToArray(T... args)