Clean duplicate values in target list, if present in main list. - Java java.util

Java examples for java.util:List Duplicate Element

Description

Clean duplicate values in target list, if present in main list.

Demo Code


//package com.book2s;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        List targetList = java.util.Arrays.asList("asdf", "book2s.com");
        List mainList = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(cleanListDuplicates(targetList, mainList));
    }//  w w w  .java 2s .  com

    /**
     * Clean duplicate values in target list, if present in main list. This method can be used to reduce repetition of elements in a
     * list, checked against a main reference list.
     * 
     * @param targetList
     *          The target list, for which repeated elements are to be removed.
     * @param mainList
     *          The reference list.
     * @return A new list, including ONLY items exclusively present in targetList.
     */
    public static <T> List<T> cleanListDuplicates(List<T> targetList,
            List<T> mainList) {
        List<T> retObj = null;

        if (isNotEmpty(targetList)) {
            if (isEmpty(mainList)) {
                retObj = new ArrayList<T>(targetList);
            } else {
                retObj = new ArrayList<T>();

                for (T value : targetList) {
                    if (!mainList.contains(value)) {
                        retObj.add(value);
                    }
                }
            }
        }
        return retObj;
    }

    /**
     * Check if set isn't empty.
     * 
     * @param set
     *          The set to be tested.
     * @return <code>true</code> is not empty, <code>false</code> otherwise.
     */
    public static <E> boolean isNotEmpty(Set<E> set) {
        return set != null && !set.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(List<E> list) {
        return list != null && !list.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(E[] list) {
        return list != null && list.length > 0;
    }

    /**
     * Check if set is empty.
     * 
     * @param set
     * @return
     */
    public static <E> boolean isEmpty(Set<E> set) {
        return !isNotEmpty(set);
    }

    public static <E> boolean isEmpty(List<E> list) {
        return !isNotEmpty(list);
    }

    public static <E> boolean isEmpty(E[] list) {
        return !isNotEmpty(list);
    }

    /**
     * This method checks if the list contains the given string (case sensitive).
     * 
     * @param list
     *          the list to check
     * @param string
     *          the string
     * @return true/false
     */
    public static boolean contains(List<String> list, String string) {
        if (isNotEmpty(list)) {
            for (String item : list) {
                if ((item != null && string != null && item
                        .compareTo(string) == 0)
                        || (item == null && string == null)) {
                    return true;
                }
            }
        }
        return false;
    }
}

Related Tutorials