String Utility Methods: Merging two String arrays

This is another simple Java String utility/util method, which can be used to merge two arrays of String values. This takes care of eliminating the duplicates and also does null checking. If you are dealing with multiple String arrays then it could be useful method.



/**
  * 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 values
  * @return
  */
 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 array1List = Arrays.asList(array1);
  List array2List = Arrays.asList(array2);
  List result = new ArrayList(array1List);  
  List tmp = new ArrayList(array1List);
  tmp.retainAll(array2List);
  result.removeAll(tmp);
  result.addAll(array2List);  
  return ((String[]) result.toArray(new String[result.size()]));
 }
 

Post a Comment Default Comments

  1. Don't you think the ArrayList.contains is going to be little inefficient here. Why don't you use HashSet here?

    ReplyDelete
  2. Well I think if you use only HashSet then the sequence of the array may not be gauranteed. May be its a good idea if you don't worry about the sequence.

    But if you need to keep the sequence intact then need to use ArrayList and HashSet both, ArrayList can be used to keep final result in exact sequence and HashSet can help eliminatethe duplicate.

    ReplyDelete
  3. Those who don't know the standard API are doomed to reimplement ;-)

    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 result = new ArrayList(Arrays.asList(array1));
    result.retainAll(Arrays.asList(array2));
    return ((String[]) result.toArray(new String[result.size()]));
    }

    ReplyDelete
  4. @Paul Thanks for your implementation, I have updated my post with code suggested by you.

    ReplyDelete
  5. Did you try your code?
    =========================================
    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 result = new ArrayList(Arrays.asList(array1));
    result.retainAll(Arrays.asList(array2));
    return ((String[]) result.toArray(new String[result.size()]));
    }
    =========================================
    It compiles but brings back an empty array

    ReplyDelete
  6. @Anonymous - Thanks for pointing that out. I have fixed it now. Please let me know if you have any other feedback.

    ReplyDelete

Individuals who comment on FromDev at regular basis, will be rewarded in Top Commenter section. (Comments are selectively moderated so please do not spam)

emo-but-icon

...

item