Here you can find the source of containsDuplicates(List
Parameter | Description |
---|---|
list | the list. |
comparator | the comparator. |
public static <T> boolean containsDuplicates(List<T> list, Comparator<T> comparator)
//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; } }