Java > Java Util Package sample source codes
Using a HashSet to remove duplicates
Using a HashSet to remove duplicates import java.util.*; public class HashSetTest { private String colors[] = { "red", "white", "blue", "green", "gray", "orange", "tan", "white", "cyan", "peach", "gray", "orange" }; public HashSetTest() { ArrayList aList; aList = new ArrayList( Arrays.asList( colors ) ); System.out.println( "ArrayList: " + aList ); printNonDuplicates( aList ); } public void printNonDuplicates( Collection c ) { HashSet ref = new HashSet( c ); // create a HashSet Iterator i = ref.iterator(); // get iterator System.out.println( "\nNonduplicates are: " ); while ( i.hasNext() ) System.out.print( i.next() + " " ); System.out.println(); } public static void main( String args[] ) { new HashSetTest(); } }
Java Codes
Beginners
Core Java
Date Time
Java2D
Java Applets
Java AWT
Mathematics
Networking
Servlets
Session
Sound
Swing
Threads
Util Package
JDBC
Learning
Other
Security
XML