Here you can find the source of intersect(List
public static <T> List<T> intersect(List<T> in1, List<T> in2)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { public static <T> List<T> intersect(List<T> in1, List<T> in2) { final List<T> theResults = new ArrayList<T>(0); if (in1 == null || in2 == null || in1.isEmpty() || in2.isEmpty()) { return theResults; }/* w w w . j a v a 2 s. c o m*/ for (final T anObject : in1) { if (anObject == null && in2.contains(null)) { theResults.add(anObject); } else if (anObject != null && in2.contains(anObject)) { theResults.add(anObject); } } return theResults; } public static <T> List<T> intersect(List<T> in1, T[] in2) { final List<T> theResults = new ArrayList<T>(0); if (in1 == null || in2 == null || in1.isEmpty() || in2.length == 0) { return theResults; } for (final T anObject : in1) { if (isContaining(in2, anObject)) { theResults.add(anObject); } } return theResults; } public static <T> List<T> intersect(T[] in1, List<T> in2) { return intersect(in2, in1); } /** * @param inValue * @return length is 0 or depending on setting a reaction <code>null</code> * value */ public static boolean isEmpty(String inValue, boolean isNullEmpty) { return inValue == null ? isNullEmpty : inValue.isEmpty(); } public static <T> boolean isContaining(T[] inValues, T inValue) { if (inValues == null) { return false; } for (final T aValue : inValues) { if (aValue == inValue) { return true; } else if (aValue != null && aValue.equals(inValue)) { return true; } } return false; } public static boolean equals(Object inO1, Object inO2) { return equals(inO1, inO2, true); } public static boolean equals(Object inO1, Object inO2, boolean isBothNullEqual) { if (inO1 == null && inO2 == null) { return isBothNullEqual; } else if (inO1 == null && inO2 != null) { return false; } else if (inO2 == null && inO1 != null) { return false; } return inO1.equals(inO2); } }