Skip to content

Instantly share code, notes, and snippets.

@gmfc
Created June 9, 2013 19:29
Show Gist options
  • Select an option

  • Save gmfc/5744844 to your computer and use it in GitHub Desktop.

Select an option

Save gmfc/5744844 to your computer and use it in GitHub Desktop.
public static int[] merge(Comparable[] arg1, Comparable[] arg2)
{
//convert arrays to collections (lists)
Collection coll1 = Arrays.asList(arg1);
Collection coll2 = Arrays.asList(arg2);
//Create a SortedSet from the first collection
SortedSet sorter = new TreeSet(coll1);
//Add the second collection
sorter.addAll(coll2);
//Create an array to hold the results
int[] merged = new int[sorter.size()];
Iterator itSorted = sorter.iterator();
for (int i = 0; itSorted.hasNext(); i++) {
merged[i] = ((Integer)itSorted.next()).intValue();
}
//return the SortedSet as an array
return(merged);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment