Here you can find the source of intersection(final List list1, final List list2)
Parameter | Description |
---|---|
list1 | the first list |
list2 | the second list |
Parameter | Description |
---|---|
NullPointerException | if either list is null |
public static List intersection(final List list1, final List list2)
//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; } }