Here you can find the source of intersect(String[] arr1, String[] arr2)
public static String[] intersect(String[] arr1, String[] arr2)
//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); } }