Here you can find the source of removeDuplicates(Collection collection)
Parameter | Description |
---|---|
collection | The collection from which duplicate elements are to be removed. |
public static void removeDuplicates(Collection collection)
//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; } }