Java List Intersect getIntersection(String[] list1, String[] list2)

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

Description

get Intersection

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//ww  w.  ja va  2 s. 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 {
    public static String[] getIntersection(String[] list1, String[] list2) {
        ArrayList intersection = new ArrayList();
        for (int i = 0; i < list1.length; i++) {
            if (contains(list1[i], list2))
                intersection.add(list1[i]);
        }

        return arrayListToStringArray(intersection);
    }

    /** determines whether a String is in the String array
       The code is very simplistic.
       <pre>
      public static boolean contains(String string, String[] array)
      {   for(int i=0; i<array.length; i++)
    if(string.equals(array[i]))
       return true;
     return false;
      }
       </pre>
    */

    public static boolean contains(String string, String[] array) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++)
            if (string.equals(array[i]))
                return true;
        return false;
    }

    /**
     * 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. calcIntersection(List a, List b)
  2. calculauteIntersection(Set wordList1, Set wordList2)
  3. getIntersection( final List... collectionOfIDLists)
  4. getIntersection(List list1, List list2)
  5. getIntersectionOfLineAndSegments(double x1, double y1, double x2, double y2, List segmentPoints)
  6. hasIntersection(List list1, List list2)
  7. hasIntersection(Set set, List list)
  8. Intersect(List A, List B)