Java Array Remove removeAll(T[] array, Collection toRemove)

Here you can find the source of removeAll(T[] array, Collection toRemove)

Description

Remove the specified elements from the array in-place, replacing them with null .

License

Creative Commons License

Parameter

Parameter Description
T The element type
array The array
toRemove The elements to remove

Return

A map of the removed elements and their indices

Declaration

public static <T> Map<T, Integer> removeAll(T[] array, Collection<T> toRemove) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**/*from   w w  w .  j  a  v a2s  .c o  m*/
     * Remove the specified elements from the array in-place, replacing them with {@code null}.
     * <p>
     * {@link Collection#contains(Object)} is used to determine if an element should be removed.
     *
     * @param <T>      The element type
     * @param array    The array
     * @param toRemove The elements to remove
     * @return A map of the removed elements and their indices
     */
    public static <T> Map<T, Integer> removeAll(T[] array, Collection<T> toRemove) {
        Map<T, Integer> removed = new HashMap<>();

        for (int i = 0; i < array.length; i++) {
            T element = array[i];
            if (toRemove.contains(element)) {
                array[i] = null;
                removed.put(element, i);
            }
        }

        return removed;
    }
}

Related

  1. remove(int[] a, int[] b)
  2. remove(String[] initial, String... toExclude)
  3. remove(String[] target, String[] needRemoved)
  4. remove(T[] a, int i)
  5. removeAll(E[] array, Object... toRemove)
  6. removeAll(T[] items, T item)
  7. RemoveArgs(String[] args, int startIndex)
  8. removeAt(T[] array, int index)
  9. removeByPrefix(String[] array, String prefix)