Java List Intersect intersect(List in1, List in2)

Here you can find the source of intersect(List in1, List in2)

Description

intersect

License

Open Source License

Declaration

public static <T> List<T> intersect(List<T> in1, List<T> in2) 

Method Source Code

//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);
    }
}

Related

  1. Intersect(List A, List B)
  2. intersect(List ls, List ls2)
  3. intersect(List lst1, List lst2)
  4. intersect(List list1, List list2)
  5. intersect(List... lists)
  6. intersect(Set a, List b)
  7. intersect2orderedList(List s1, List s2)
  8. intersectAll(final List collector, final T[] a, final T[] b)
  9. intersectDate(List oDate, List tDate)