Merge and sorting arrays of String and int in more efficient methods Are there better methods for merge and sorting arrays of int and String in more efficient methods both time-complexity wise and code-pattern wise? I also wonder if I could have a generic method that could work for any kind of given type like &lt;E&gt;.

I am preparing for coding interviews, so I prefer to see solutions that involve known Java APIs and nothing fancy.

/**
 * Created by mona on 5/24/16.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MergeArrays {

    public static String[] mergeAndSortStringArrays(String[] first, String[] second){
        List&lt;String&gt; merged = new ArrayList&lt;&gt;();
        Collections.addAll(merged,first);
        Collections.addAll(merged,second);
        Collections.sort(merged);
        return merged.toArray(new String[merged.size()]);
    }

    public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){

        List&lt;Integer&gt; merged = new ArrayList&lt;&gt;();

        for (int i=0; i&lt;firstInt.length; i++){
            merged.add(firstInt[i]);
        }

        for (int i=0; i&lt;secondInt.length; i++){
            merged.add(secondInt[i]);
        }

        Collections.sort(merged);
        int[] result=new int[merged.size()];
        for (int i=0; i&lt;merged.size(); i++){
            result[i]=merged.get(i);
        }
        return result;
    }


    public static void main(String[] args){
        String[] first={"hi", "mona", "how", "are", "you"};
        String[] second={"hello", "world", "good", "morning", "USA"};
        String[] merged=mergeAndSortStringArrays(first, second);
        for (int i=0; i&lt;merged.length; i++){
            System.out.print(merged[i]+" ");
        }

        int[] firstInt={1, 4, 2, 6, 7};
        int[] secondInt={2, 8, 2, 9, 1, 12};
        int[] resultedMerge=mergeAndSortIntArrays(firstInt, secondInt);
        for (int i=0; i&lt;resultedMerge.length; i++){
            System.out.print(resultedMerge[i]+" ");
        }
    }
}
mergeAndSortStringArrays looks good to me. What comes to mergeAndSortIntArrays, you can clean it a bit:

public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){
    List&lt;Integer&gt; merged = new ArrayList&lt;&gt;();
    IntStream.of(firstInt) .forEach(merged::add);
    IntStream.of(secondInt).forEach(merged::add);
    Collections.sort(merged);
    return merged.stream().mapToInt((Integer i) -&gt; (i)).toArray();
}


Generic implementation

Also, you could do 

public static &lt;E extends Comparable&lt;? super E&gt;&gt; E[]
mergeArrays(final E[] firstArray, final E[] secondArray) {
    final E[] ret = Arrays.copyOf(firstArray, 
                                  firstArray.length + secondArray.length);
    System.arraycopy(secondArray, 
                     0, 
                     ret, 
                     firstArray.length, 
                     secondArray.length);

    Arrays.sort(ret);
    return ret;
}


This, however, will not accept an int[] as the argument type, yet it will gladly work on Integer[] and other object arrays.

A minor tip

Instead of writing for loops for printing arrays, you can always say:

System.out.println(Arrays.toString(mergeArrays(first, second)));


Hope that helps.