Java Utililty Methods List Difference

List of utility methods to do List Difference

Description

The list of methods to do List Difference are organized into topic(s).

Method

Listdiff(List ls, List ls2)
diff
List list = new ArrayList(Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);
list.removeAll(ls2);
return list;
Listdiff(List doublesA, List doublesB)
returns a list with the values difference values doublesA-doublesB
if (doublesA.size() != doublesB.size()) {
    throw new IllegalArgumentException("The two lists must be of same size!");
List<Double> diff = new ArrayList<Double>(doublesA.size());
for (int i = 0; i < doublesA.size(); i++) {
    diff.add(doublesA.get(i) - doublesB.get(i));
return diff;
...
SetdiffAsSet(Collection list1, Collection list2)
Returns all objects in list1 that are not in list2.
Set<T> diff = new HashSet<>();
for (T t : list1) {
    if (!list2.contains(t)) {
        diff.add(t);
return diff;
Listdifference(final List minuend, final List subtrahend)
difference
final List<Double> result = new ArrayList<Double>();
for (int i = 0; i < minuend.size(); ++i) {
    final Object o1 = minuend.get(i);
    final Object o2 = subtrahend.get(i);
    if (areBothNumbers(o1, o2)) {
        subtract(result, o1, o2);
    } else {
        result.add(Double.NaN);
...
Listdifference(final List list1, final List list2)
difference
final List<T> result = new ArrayList<T>(list1);
final Iterator<? extends T> iterator = list2.iterator();
while (iterator.hasNext()) {
    result.remove(iterator.next());
return result;
String[]difference(final String[] list1, final String[] list2)
The difference set operation
HashSet<String> set = new HashSet<String>();
HashSet<String> set1 = new HashSet<String>(Arrays.asList(list1));
HashSet<String> set2 = new HashSet<String>(Arrays.asList(list2));
for (final String s : list1) {
    if (!set2.contains(s)) {
        set.add(s);
for (final String s : list2) {
    if (!set1.contains(s)) {
        set.add(s);
return set.toArray(new String[set.size()]);
Listdifference(List firstList, List secondList)
Returns the set difference of the given lists.
List<E> results = create();
if (firstList != null)
    results.addAll(firstList);
if (secondList != null)
    results.removeAll(secondList);
return results;
voiddifference(List lst1, List lst2)
difference
for (T1 elem : lst2) {
    lst1.remove(elem);
Listdifference(List set1, List set2)
difference
List<T> diff = new ArrayList<T>(set1);
diff.removeAll(set2);
return diff;
ListdifferenceOfList(List a, List b)
difference Of List
List<E> result = new ArrayList<>();
for (E i : a) {
    if (b.contains(i))
        continue;
    result.add(i);
return result;