Here you can find the source of intersect(String[] list1, String[] list2)
public static String[] intersect(String[] list1, String[] list2)
//package com.java2s; /*//w w w . j a v a2 s . com * StringUtil.java * * Copyright (c) 2005-2009 Andrew Krizhanovsky <andrew.krizhanovsky at gmail.com> * Distributed under GNU Public License. */ import java.util.Map; import java.util.HashMap; public class Main { /** Returns intersections of two list. * Return null if input String[] is null. */ public static String[] intersect(String[] list1, String[] list2) { if (null == list1 || 0 == list1.length || null == list2 || 0 == list2.length) { return null; } Map<String, Boolean> map1 = new HashMap<String, Boolean>(); for (String s : list1) { map1.put(s, false); } int counter = 0; for (String s : list2) { if (map1.containsKey(s)) { //map1.put(s, true); counter++; } } String[] res = new String[counter]; counter = 0; for (String s : list2) { if (map1.containsKey(s)) { res[counter++] = s; } } return res; } }