Java List Contain containsDuplicates(List list, Comparator comparator)

Here you can find the source of containsDuplicates(List list, Comparator comparator)

Description

Checks whether the given list contains duplicates.

License

Open Source License

Parameter

Parameter Description
list the list.
comparator the comparator.

Return

true if the list contains duplicates, false if not.

Declaration

public static <T> boolean containsDuplicates(List<T> list, Comparator<T> comparator) 

Method Source Code

//package com.java2s;

import java.util.Collections;
import java.util.Comparator;

import java.util.List;

public class Main {
    /**//from  w w w . jav a2  s.  com
     * Checks whether the given list contains duplicates. List entries are compared
     * using the given comparator.
     * 
     * @param list the list.
     * @param comparator the comparator.
     * @return true if the list contains duplicates, false if not.
     */
    public static <T> boolean containsDuplicates(List<T> list, Comparator<T> comparator) {
        Collections.sort(list, comparator);

        T previous = null;

        for (T entry : list) {
            if (previous != null && previous.equals(entry)) {
                return true;
            }

            previous = entry;
        }

        return false;
    }
}

Related

  1. containsAtLeastAValue(List values)
  2. containsAtLeastOneElement(List l1, List l2)
  3. containsBasedOnEntryIdentity(final List list, final Object object)
  4. containsCaseInsensitive(String s, List l)
  5. containsColumnByAlias(List columns, String alias)
  6. containsElement( List> list, List element)
  7. containsEnchantment(String enchantments, List enchList)
  8. containsExactly(Collection list, T... mustHaveItems)
  9. containsExactly(List items, T... itemsToMatch)