Check if element from a collection has Duplicates - Android java.util

Android examples for java.util:Collection Element

Description

Check if element from a collection has Duplicates

Demo Code


//package com.book2s;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "book2s.com");
        System.out.println(hasDuplicates(collection));
    }//  ww  w . j  a  v a2s . c o  m

    public static <T> boolean hasDuplicates(Collection<T> collection) {
        Set<T> set = new HashSet<T>(collection);
        return set.size() != collection.size();
    }
}

Related Tutorials