Java Array Minus minus(String[] left, String[] right)

Here you can find the source of minus(String[] left, String[] right)

Description

minus

License

Apache License

Declaration

public static String[] minus(String[] left, String[] right) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**//from  w  w w . ja va  2 s  . c o  m
     * Return the result of left minus right.
     *
     * <pre>
     * StringUtil.minus(["s1", "s2"], ["s1"], true) = ["s2"]
     * StringUtil.minus(["s1", "s2"], ["S1"], false) = ["s2"]
     * </pre>
     *
     * @author chenke
     */
    public static String[] minus(String[] left, String[] right, boolean caseSensive) {
        if (left == null || left.length == 0) {
            return EMPTY_STRING_ARRAY;
        }
        if (right == null || right.length == 0) {
            return left;
        }
        List result = new ArrayList(left.length);
        for (int i = 0; i < left.length; i++) {
            int index = indexOf(right, left[i], caseSensive);
            if (index == -1) {
                result.add(left[i]);
            }
        }
        return toArray(result);
    }

    public static String[] minus(String[] left, String[] right) {
        return minus(left, right, true);
    }

    /**
     * Find the index of the searchStr in the string array strs.
     * Return -1, if not found.
     *
     * <pre>
     * StringUtil.indexOf(["s1", "s2"], "s1", true) = 0
     * StringUtil.indexOf(["s1", "s2"], "S1", true) = -1
     * </pre>
     *
     * @param strs
     *            the string array to check, maybe null.
     * @param searchStr
     *            the string to search for, maybe null.
     * @param caseSensive
     *            is it case sensive while finding the searchStr
     *            in the searchStr.
     * @return index of the searchStr found in the strs, -1 if not found.
     *
     * @author chenke
     */
    public static int indexOf(String[] strs, String searchStr, boolean caseSensive) {

        if (strs == null || strs.length == 0) {
            return -1;
        }
        for (int i = 0; i < strs.length; i++) {
            if (isEqual(strs[i], searchStr, caseSensive)) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(String[] strs, String searchStr) {
        return indexOf(strs, searchStr, true);
    }

    /**
     * Returns a string array contains all the String object in the
     * Collection c.
     *
     * @param c
     *            Collection of String object.
     * @throws ClassCastException
     *             if the object in the Collection is
     *             not a String object.
     *
     * @author chenke
     */
    public static String[] toArray(Collection c) {
        if (c == null || c.size() == 0) {
            return EMPTY_STRING_ARRAY;
        }
        String[] result = new String[c.size()];
        int i = 0;
        for (Iterator iter = c.iterator(); iter.hasNext();) {
            result[i++] = (String) iter.next();
        }
        return result;
    }

    private static boolean isEqual(String s1, String s2, boolean caseSensive) {
        if (caseSensive) {
            return s1 == null ? s2 == null : s1.equals(s2);
        } else {
            return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
        }
    }
}

Related

  1. minusC(double[] v, double[] u)