Here you can find the source of mergeSortArrays(String[][] arrayArray)
Parameter | Description |
---|---|
arrayArray | the array containing the set of arrays to merge |
protected static final String[] mergeSortArrays(String[][] arrayArray)
//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; } }