HashSet

PHOTO EMBED

Wed May 29 2024 17:04:09 GMT+0000 (Coordinated Universal Time)

Saved by @exam123

import java.util.*; 
 
public class HashSetDemo 
{ 
 public static void main(String[] args) 
 { 
  HashSet<String> hs = new HashSet<>(); 
   
  hs.add("first"); hs.add("second"); hs.add("third");  
  hs.add("fourth"); hs.add("fifth"); hs.add("sixth"); 
   
  System.out.println("Contents: " + hs); 
  System.out.println("Size: " + hs.size()); 
  System.out.println(); 
   
  Object[] arrayS = hs.toArray(); 
  for(Object each: arrayS) 
   System.out.println(each); 
   
  System.out.println(); 
  String[] arrayS2 = new String[hs.size()]; 
  arrayS2 = hs.toArray(arrayS2); 
  for( String each: arrayS2) 
   System.out.println(each); 
   
  System.out.println(); 
  Iterator it = hs.iterator(); 
  while(it.hasNext()) 
   System.out.println(it.next()); 
   
  System.out.println(); 
  System.out.println("Empty? " + hs.isEmpty()); 
  System.out.println(); 
   
  hs.remove("fourth"); 
  System.out.println("Contents: " + hs); 
  System.out.println("Size: " + hs.size()); 
 } 
} 
content_copyCOPY