Here you can find the source of Intersect(ArrayList
Parameter | Description |
---|---|
list1 | First list |
list2 | Second list |
public static ArrayList<String> Intersect(ArrayList<String> list1, ArrayList<String> list2)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.util.*; public class Main { /** Finds the intersection between two lists of String objects. *//from w ww . j ava 2s. c om * @param list1 First list * @param list2 Second list * @return Intersection list (contains values that exist in both lists) */ public static ArrayList<String> Intersect(ArrayList<String> list1, ArrayList<String> list2) { if (list1.size() == 0) return list2; Set intersection = new HashSet(list1); intersection.retainAll(new HashSet(list2)); return new ArrayList(intersection); } }