ConcurrentSkipListSet - iterate using iterator, listIterator, Enumeration and enhanced for loop






ConcurrentSkipListSet - iterate using iterator, listIterator, Enumeration and enhanced for loop.



import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentSkipListSetTest {
   public static void main(String args[]) {
          Set<String> concurrentSkipListSet = new ConcurrentSkipListSet<String>();
          concurrentSkipListSet.add("javaMadeSoEasy");
          concurrentSkipListSet.add("ankit");
          System.out.println("-------use iterator-------");
          Iterator<String> iterator = concurrentSkipListSet.iterator();
          while (iterator.hasNext()) {
                 System.out.println(iterator.next());
          }
          System.out.println("-------use Enumeration-------");
          Enumeration<String> listEnum = Collections
                       .enumeration(concurrentSkipListSet);
          while (listEnum.hasMoreElements()) {
                 System.out.println(listEnum.nextElement());
          }
          System.out.println("-------use enhanced for loop-------");
          for (String string : concurrentSkipListSet) {
                 System.out.println(string);
          }
   }
}
/*OUTPUT
-------use iterator-------
ankit
javaMadeSoEasy
-------use Enumeration-------
ankit
javaMadeSoEasy
-------use enhanced for loop-------
ankit
javaMadeSoEasy
*/


RELATED LINKS>

ConcurrentSkipListSet - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop




No comments:

Post a Comment