Java List Intersect intersection(final List list1, final List list2)

Here you can find the source of intersection(final List list1, final List list2)

Description

Returns a new list containing all elements that are contained in both given lists.

License

Open Source License

Parameter

Parameter Description
list1 the first list
list2 the second list

Exception

Parameter Description
NullPointerException if either list is null

Return

the intersection of those two lists

Declaration

public static List intersection(final List list1, final List list2) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  .  j  ava 2  s.  c o m*/
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.util.*;

public class Main {
    /**
     * Returns a new list containing all elements that are contained in
     * both given lists.
     *
     * @param list1 the first list
     * @param list2 the second list
     * @return the intersection of those two lists
     * @throws NullPointerException if either list is null
     */
    public static List intersection(final List list1, final List list2) {
        final ArrayList result = new ArrayList();
        final Iterator iterator = list2.iterator();

        while (iterator.hasNext()) {
            final Object o = iterator.next();

            if (list1.contains(o))
                result.add(o);
        }

        return result;
    }
}

Related

  1. intersect(Set a, List b)
  2. intersect2orderedList(List s1, List s2)
  3. intersectAll(final List collector, final T[] a, final T[] b)
  4. intersectDate(List oDate, List tDate)
  5. intersection(final List list1, final List list2)
  6. intersection(final List list1, final List list2)
  7. intersection(List> sets)
  8. intersection(List list1, List list2)
  9. intersection(List list1, List list2)