Java Array Merge mergeSortArrays(String[][] arrayArray)

Here you can find the source of mergeSortArrays(String[][] arrayArray)

Description

Returns a new array which merges, sorts, and removes duplicates from the elements of the arrays contained in the given array (of arrays).

License

Open Source License

Parameter

Parameter Description
arrayArray the array containing the set of arrays to merge

Return

the new merged, sorted array

Declaration

protected static final String[] mergeSortArrays(String[][] arrayArray) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright ? 2005, 2007IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which is available at//ww w  .  j av a 2 s.c  o m
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import java.util.LinkedHashSet;
import java.util.List;

import java.util.Set;

public class Main {
    /**
     * Returns a new array which merges, sorts, and removes duplicates from the elements of 
     * the arrays contained in the given array (of arrays).
     * 
     * @param arrayArray the array containing the set of arrays to merge
     * @return the new merged, sorted array
     */
    protected static final String[] mergeSortArrays(String[][] arrayArray) {
        String[] mergedArray = null;

        int arrayCount = arrayArray.length;
        if (arrayCount > 0) {

            /* Combine all the input arrays into one list. */
            String[] tempArray = arrayArray[0];
            List tempList = Arrays.asList(tempArray);

            /* The array returned as the result of the asList method is not an ArrayList,
             * and it doesn't support the "addAll" method.  So create a new ArrayList so
             * we can do the merge. */
            List mergedList = new ArrayList(tempList);

            for (int i = 1; i < arrayCount; i++) {
                tempArray = arrayArray[i];
                tempList = Arrays.asList(tempArray);
                mergedList.addAll(tempList);
            }

            /* Sort the list, then remove duplicates. Note that the LinkedHashSet is
             * used because it maintains the insertion order of the elements. (A
             * regular HashSet would lose the sorted order.) */
            Collections.sort(mergedList);
            Set mergedListNoDups = new LinkedHashSet(mergedList);

            /* Create the result array from the list. */
            int listLen = mergedListNoDups.size();
            mergedArray = (String[]) mergedListNoDups.toArray(new String[listLen]);
        }

        return mergedArray;
    }
}

Related

  1. mergeNoDuplicates(String[] in1, String[] in2)
  2. mergeParameterVariableNameDescription(String[] parameterType, String[] variableName)
  3. mergerBy(String[] target, String seperator)
  4. mergeSort(Object[] src, Object[] dest, int low, int high, int off)
  5. mergeSort(Object[] src, Object[] dest, int low, int high, int off, Comparator c)
  6. mergeSortedInc(long[] a, long[] b)
  7. mergeStringArray(String[] array)
  8. mergeStringArray(String[] array, String seperator)
  9. mergeStringArray(String[] ary1, String[] ary2)