Click here to watch in Youtube :
https://www.youtube.com/watch?v=9f3_QWoHnqY&list=UUhwKlOVR041tngjerWxVccw
HashSetExample.java
Output
https://www.youtube.com/watch?v=9f3_QWoHnqY&list=UUhwKlOVR041tngjerWxVccw
HashSetExample.java
import java.util.HashSet; import java.util.Iterator; /* * Example of Iterator remove() method. */ public class HashSetExample { public static void main( String[] args ) { HashSet<String> hashSet = new HashSet<String>(); hashSet.add("Dave"); hashSet.add("Peter"); hashSet.add("Phil"); hashSet.add("Rohit"); hashSet.add("Virat"); System.out.println("hashSet : " + hashSet + "\n"); /* * Returns an iterator over the elements in this set. The elements are * returned in no particular order. */ Iterator<String> iterator = hashSet.iterator(); while( iterator.hasNext() ) { iterator.next(); /* * Removes from the set, the last element that was returned by * next() */ iterator.remove(); } System.out.println("hashSet : " + hashSet + "\n"); } }
hashSet : [Rohit, Dave, Phil, Peter, Virat] hashSet : []
https://sites.google.com/site/javaee4321/java-collections/HashSetDemoIteratorRemove.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment