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()); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter