Java List Remove Duplicate removeDuplicate(List dest, List src)

Here you can find the source of removeDuplicate(List dest, List src)

Description

remove Duplicate

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static <T> void removeDuplicate(List<T> dest, List<T> src) 

Method Source Code


//package com.java2s;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> void removeDuplicate(List<T> dest, List<T> src) {
        if (dest == null) {
            return;
        }/*from ww w . j a v  a2s . c om*/

        if (src == null || src.isEmpty()) {
            return;
        }

        int capacity = dest.size() > src.size() ? dest.size() : src.size();
        HashMap<T, Integer> map = new HashMap<T, Integer>(capacity);
        for (int i = 0; i < dest.size(); i++) {
            map.put(dest.get(i), i);
        }

        T[] oldData = (T[]) dest.toArray();
        int length = oldData.length;
        for (T t : src) {
            Integer index = map.get(t);
            if (index != null) {
                oldData[index] = null;
                length--;
            }
        }

        T[] removedDuplicate = (T[]) new Object[length];
        int index = 0;
        for (int i = 0; i < oldData.length; i++) {
            if (oldData[i] != null) {
                removedDuplicate[index++] = oldData[i];
            }
        }

        dest.clear();
        dest.addAll(Arrays.asList(removedDuplicate));
    }
}

Related

  1. removeDublicates(List l)
  2. removeDup(List lst)
  3. removeDuplicate(Collection entityList)
  4. removeDuplicate(List uidList)
  5. removeDuplicates(int[] list)
  6. removeDuplicates(List list)
  7. removeDuplicates(List listofthings)
  8. removeDuplicates(List commonVars)