Java List Subtract subtract(String[] list1, String[] list2)

Here you can find the source of subtract(String[] list1, String[] list2)

Description

Remove Strings that are in list2 from list1.

License

Open Source License

Parameter

Parameter Description
list1 List that is being subtracted from
list2 List of unwanted strings

Return

method returns all strings in list1 except those strings that are in both list2 and list1.

Declaration

public static String[] subtract(String[] list1, String[] list2) 

Method Source Code

//package com.java2s;
/*/*  w ww . j  a  va 2s . c o  m*/
 * Copyright: (c) 2002-2006 Mayo Foundation for Medical Education and
 * Research (MFMER).  All rights reserved.  MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, the trade names, 
 * trademarks, service marks, or product names of the copyright holder shall
 * not be used in advertising, promotion or otherwise in connection with
 * this Software without prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *       http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
    Remove Strings that are in list2 from list1.  Strings in list2 that do not exist in list1 have
    no effect in this method.
        
    @param list1 List that is being subtracted from
    @param list2 List of unwanted strings
        
    @return method returns all strings in list1 except those strings that are in both list2 and list1.
    */
    public static String[] subtract(String[] list1, String[] list2) {
        ArrayList diff = new ArrayList(list1.length);
        boolean add = true;
        for (int i = 0; i < list1.length; i++) {
            for (int j = 0; j < list2.length; j++) {
                if (list1[i].equals(list2[j])) {
                    add = false;
                    break;
                }
            }
            if (add)
                diff.add(list1[i]);
            add = true;
        }

        return arrayListToStringArray(diff);
    }

    /**
     * This method is here because my first implementation used the brute force approach.
     * Then I realized that you could accomplish this in one line of code - and it is
     * more efficient because it uses System.arraycopy().
     */
    public static String[] arrayListToStringArray(ArrayList arrayList) {
        if (arrayList == null)
            return new String[0];
        return (String[]) (arrayList.toArray(new String[arrayList.size()]));
    }

    public static String[] toArray(ArrayList arrayList) {
        return arrayListToStringArray(arrayList);
    }
}

Related

  1. subtract(final String[] input, final String[] list)
  2. subtract(List a, List b)
  3. subtract(List vector, int[] sparseVector)
  4. subtract(List a, List b)
  5. subtract(List aList, List bList)
  6. subtractAsList(T[] array1, T[] array2)
  7. subtractFloatLists(List listA, List listB)
  8. subtractFromList(List op1, Object op2)
  9. subtractQ(LinkedList Q, List P)