Java Array Intersect intersect(String[] arr1, String[] arr2)

Here you can find the source of intersect(String[] arr1, String[] arr2)

Description

intersect

License

LGPL

Declaration

public static String[] intersect(String[] arr1, String[] arr2) 

Method Source Code

//package com.java2s;
/**//from   www . java 2  s .  c o m
 * Converts a line of text into an array of lower case words using a
 * BreakIterator.wordInstance(). <p>
 *
 * This method is under the Jive Open Source Software License and was
 * written by Mark Imbriaco.
 *
 * @param text a String of text to convert into an array of words
 * @return text broken up into an array of words.
 */

import java.util.HashMap;

import java.util.LinkedList;
import java.util.Map;

import java.util.Map.Entry;

public class Main {

    public static String[] intersect(String[] arr1, String[] arr2) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        LinkedList<String> list = new LinkedList<String>();
        for (String str : arr1) {
            if (!map.containsKey(str)) {
                map.put(str, Boolean.FALSE);
            }
        }
        for (String str : arr2) {
            if (map.containsKey(str)) {
                map.put(str, Boolean.TRUE);
            }
        }
        for (Entry<String, Boolean> e : map.entrySet()) {
            if (e.getValue().equals(Boolean.TRUE)) {
                list.add(e.getKey());
            }
        }
        String[] result = {};
        return list.toArray(result);
    }
}

Related

  1. getNonIntersection(int[] interval, int[] intervalToRemove)
  2. hasIntersection(String a1[], String a2[], int mode)
  3. intersect(boolean[] mask, int[] examples, boolean[] intersection)
  4. intersect(int[] sorted1, int[] sorted2)
  5. intersect(int[]... arrays)
  6. intersect(String[] list1, String[] list2)
  7. intersect(T[] a, T[] b)
  8. intersect(T[] a, T[] b)
  9. intersectArrays(int[] a, int[] b)