Java HashSet

HashSet

 

 

Below is a HashSet Example showing how collections are manipulated using a HashSet

 import java.util.*;

 public class HashSetExample{
	 
	 private static String names[] = { "bob", "hemanth", "hhh", "hero", "shawn", "bob",
	 "mike", "Rick", "rock", "hemanth", "mike", "undertaker" };

	 public static void main( String args[] ){
		 ArrayList aList;

		 aList = new ArrayList( Arrays.asList( names ) );
		 System.out.println( "The names elements " + aList );
		 
		 HashSet ref = new HashSet(aList); // create a HashSet
		 Iterator i = ref.iterator();
		 System.out.println();
		 System.out.print("Unique names are: " );
		 while ( i.hasNext() )
			 System.out.print( i.next() + " " );

		 System.out.println();

	 }
 }

     

Output


The names elements [bob, hemanth, hhh, hero, shawn, bob, mike, Rick, rock, hemanth, mike, undertaker]

Unique names are: hhh hero bob Rick shawn hemanth rock mike undertaker

 

Download HashSetExample.java