Java Utililty Methods Collection Subtract

List of utility methods to do Collection Subtract

Description

The list of methods to do Collection Subtract are organized into topic(s).

Method

Collectionsub(Collection s, T item)
sub
Set<T> result = new HashSet<T>();
for (T t : s)
    if (t != item)
        result.add(t);
return result;
CollectionsubCollection(Collection col, int amount)
sub Collection
if (!isNullOrEmpty(col) && (col.size() > amount)) {
    ArrayList<T> list = new ArrayList<T>(col);
    return list.subList(0, amount);
return col;
CollectionsubCollection(final Collection collection, final int offset, final int limit)
sub Collection
if (collection == null || collection.isEmpty()) {
    return collection;
int start = offset;
if (start > collection.size()) {
    start = collection.size();
int end = start + limit;
...
MapsubMap(Map map, Collection keys)
sub Map
Map<K, V> subMap = new HashMap<K, V>(keys.size());
for (K key : keys) {
    V value = map.get(key);
    if (value != null) {
        subMap.put(key, value);
return subMap;
...
Map>substractBackground( final Map> concentrationToSampleReadings, final Map> concentrationToBackgroundReadings)
substract Background
final double ZERO = 0.0;
final Map<Double, Collection<double[]>> concentrationToSubtractedReadings = new TreeMap<>();
for (Iterator<Map.Entry<Double, Collection<double[]>>> iterator = concentrationToSampleReadings.entrySet()
        .iterator(); iterator.hasNext();) {
    final Map.Entry<Double, Collection<double[]>> entry = iterator.next();
    final Double concentration = entry.getKey();
    final Collection<double[]> sampleReadings = entry.getValue();
    final Collection<double[]> backgroundReadings = concentrationToBackgroundReadings.get(concentration);
...
Collectionsubtract(Collection a, Collection b)
subtract
Collection results = new HashSet();
for (Iterator i = a.iterator(); i.hasNext();) {
    Object o = i.next();
    if (!(b.contains(o))) {
        results.add(o);
return results;
...
Collectionsubtract(Collection c1, Collection c2)
subtract
if (isEmpty(c1) || isEmpty(c2))
    return c1;
List list = newArrayList(c1);
Iterator iterator = c2.iterator();
Object element;
while (iterator.hasNext()) {
    element = iterator.next();
    if (c2.contains(element))
...
Listsubtract(Collection a, Collection b)
subtract
Set<Integer> y = new HashSet<Integer>(b);
Set<Integer> z = new HashSet<Integer>();
for (Integer i : a) {
    if (y.add(i))
        z.add(i);
return new ArrayList(z);
Listsubtract(Collection c1, Collection c2)
c1 - c2, does not change input collections, returns new collection (list)
if (c1 == null) {
    return Collections.emptyList();
List<T> tmp = new ArrayList<>(c1);
if (c2 != null) {
    tmp.removeAll(c2);
return tmp;
...
Listsubtract(Collection l1, Collection l2)
Returns all elements that are in l1 but not in l2 as a list.
List<T> newList = new ArrayList<T>(l1);
newList.removeAll(l2);
return newList;