Java Collection Remove removeDuplicates(Collection collection)

Here you can find the source of removeDuplicates(Collection collection)

Description

* Removes duplicates from collection based on its natural comparator or equality operator.

License

GNU General Public License

Parameter

Parameter Description
collection The collection from which duplicate elements are to be removed.

Declaration

public static void removeDuplicates(Collection collection) 

Method Source Code

//package com.java2s;
/** This code is copyright Articulate Software (c) 2003.  Some portions
 copyright Teknowledge (c) 2003 and reused under the terms of the GNU license.
 This software is released under the GNU Public License <http://www.gnu.org/copyleft/gpl.html>.
 Users of this code also consent, by use of this code, to credit Articulate Software
 and Teknowledge in any writings, briefings, publications, presentations, or 
 other representations of any software which incorporates, builds on, or uses this 
 code.  Please cite the following article in any publication with references:

 Pease, A., (2003). The Sigma Ontology Development Environment, 
 in Working Notes of the IJCAI-2003 Workshop on Ontology and Distributed Systems,
 August 9, Acapulco, Mexico.//from   w w w .j  av a2  s.c  om
 */

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

public class Main {
    /** ***************************************************************
     * Removes duplicates from collection based on its natural
     * comparator or equality operator.
     *
     * @param collection The collection from which duplicate elements
     *                   are to be removed.
     *
     */
    public static void removeDuplicates(Collection collection) {

        try {
            HashSet hs = new HashSet();
            Object obj = null;
            for (Iterator it = collection.iterator(); it.hasNext();) {
                obj = (Object) it.next();
                if (hs.contains(obj))
                    it.remove();
                else
                    hs.add(obj);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return;
    }
}

Related

  1. removeAny(Collection collection)
  2. removeArrayMarkerFromCollectionToString(Collection col)
  3. removeArrayToCollection(T[] array, Collection collection)
  4. removed(Collection old, Collection nu)
  5. removed(Collection a, Collection b)
  6. removeDuplicates(Collection original)
  7. removeDuplicates(Collection p_collection)
  8. removeDuplicates(Collection objects)
  9. removeDuplicates(Collection elements)