Description
Returns the sum of the 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
a new list containing the sum of those lists
Declaration
public static List sum(final List list1, final List list2)
Method Source Code
//package com.java2s;
/*/* w w w . j a v a 2s . 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 the sum of the given lists. This is their intersection
* subtracted from their union.
*
* @param list1 the first list
* @param list2 the second list
* @return a new list containing the sum of those lists
* @throws NullPointerException if either list is null
*/
public static List sum(final List list1, final List list2) {
return subtract(union(list1, list2), intersection(list1, list2));
}
/**
* Subtracts all elements in the second list from the first list,
* placing the results in a new list.
* <p/>
* This differs from {@link List#removeAll(Collection)} in that
* cardinality is respected; if <Code>list1</Code> contains two
* occurrences of <Code>null</Code> and <Code>list2</Code> only
* contains one occurrence, then the returned list will still contain
* one occurrence.
*
* @param list1 the list to subtract from
* @param list2 the list to subtract
* @return a new list containing the results
* @throws NullPointerException if either list is null
*/
public static List subtract(final List list1, final List list2) {
final ArrayList result = new ArrayList(list1);
final Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
result.remove(iterator.next());
}
return result;
}
/**
* Returns a new list containing the second list appended to the
* first list. The {@link List#addAll(Collection)} operation is
* used to append the two given lists into a new list.
*
* @param list1 the first list
* @param list2 the second list
* @return a new list containing the union of those lists
* @throws NullPointerException if either list is null
*/
public static List union(final List list1, final List list2) {
final ArrayList result = new ArrayList(list1);
result.addAll(list2);
return result;
}
/**
* 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
- getSum(List values)
- getSum(List longList)
- getSumOfInts(List ints)
- getSumOfPositiveDoubles(List scores)
- removeOne(List summary, int start)
- sum(final List extends Number> list)
- sum(final List list)
- sum(int[] list)
- sum(List extends Length> list)