Java List Difference diffAsSet(Collection list1, Collection list2)

Here you can find the source of diffAsSet(Collection list1, Collection list2)

Description

Returns all objects in list1 that are not in list2.

License

Open Source License

Parameter

Parameter Description
T Type of items in the collection
list1 First collection
list2 Second collection

Return

The collection difference list1 - list2

Declaration

public static <T> Set<T> diffAsSet(Collection<T> list1, Collection<T> list2) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**//w w w  .  jav a  2 s  .  c om
     * Returns all objects in list1 that are not in list2.
     *
     * @param <T> Type of items in the collection
     * @param list1 First collection
     * @param list2 Second collection
     * @return The collection difference list1 - list2
     */
    public static <T> Set<T> diffAsSet(Collection<T> list1, Collection<T> list2) {
        Set<T> diff = new HashSet<>();
        for (T t : list1) {
            if (!list2.contains(t)) {
                diff.add(t);
            }
        }
        return diff;
    }
}

Related

  1. diff(List ls, List ls2)
  2. diff(List doublesA, List doublesB)
  3. difference(final List minuend, final List subtrahend)
  4. difference(final List list1, final List list2)
  5. difference(final String[] list1, final String[] list2)
  6. difference(List firstList, List secondList)