Java - Write code to Merge to string arrays

Requirements

Write code to Merge to string arrays

Demo

//package com.book2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) {
        String[] input = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        String[] list = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(java.util.Arrays.toString(merge(input, list)));
    }//w  w w  .j  a  va  2  s . c  o m

    /**
     * Merge to string arrays
     *
     * @param input Array elements to add
     * @param list  List to merge input into
     * @return string array of merged set
     */
    public static String[] merge(final String[] input, final String[] list) {
        final List<String> v = new ArrayList<String>(Arrays.asList(list));
        for (final String anInput : input) {
            if ((null != anInput) && !v.contains(anInput)) {
                v.add(anInput);
            }
        }
        return v.toArray(new String[v.size()]);
    }

    /**
     * Returns true if the value is in the list.
     * @param list
     * @param value
     * @return
     */
    public static boolean contains(final String[] list, final String value) {
        HashSet<String> set = new HashSet<String>(Arrays.asList(list));
        return set.contains(value);
    }
}

Related Exercise