Monday, 22 December 2014

Java : Collection Framework : LinkedHashSet (Remove object using Iterator)

LinkedHashSetExample.java
import java.util.LinkedHashSet;
import java.util.Iterator;

/*
 * Example of Iterator remove() method.
 */
public class LinkedHashSetExample
{
    public static void main( String[] args )
    {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();

        linkedHashSet.add("Dave");
        linkedHashSet.add("Peter");
        linkedHashSet.add("Phil");
        linkedHashSet.add("Rohit");
        linkedHashSet.add("Virat");

        System.out.println("linkedHashSet : " + linkedHashSet + "\n");

        /*
         * Returns an iterator over the elements in this set. The elements are
         * returned in no particular order (unless this set is an instance of
         * some class that provides a guarantee).
         */

        Iterator<String> iterator = linkedHashSet.iterator();

        while( iterator.hasNext() )
        {
            iterator.next();
            /*
             * Removes from the list the last element that was returned by
             * next()
             */
            iterator.remove();
        }

        System.out.println("linkedHashSet : " + linkedHashSet + "\n");
    }
}

Output
linkedHashSet : [Dave, Peter, Phil, Rohit, Virat]

linkedHashSet : []
To Download LinkedHashSetDemoIteratorRemove Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/LinkedHashSetDemoIteratorRemove.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • 3 comments:

    1. Do you want to make your photo more stylish & attractive? Do you want to delete unwanted object from your photo? If yes then use our great application clone Stamp for this for this purpose..
      remove object

      ReplyDelete
    2. Everyone wants to make their photos the most beautiful. Somebody wants to auto cut his image. Someone wants to change background of their photo or cut out image.
      MasterLogix team make the best tool for change background & background removerchange background in android app.#changebackground #backgroundremove #backgroundtransparent
      Remove Image Background Make Transparent Image best app for background eraser and change background to make your photo more stylish and attractive with this cut out image

      ReplyDelete

    3. We are added much functionality in this Remove object from photo and Remove BG from photo app like remover, clone stamp, background eraser, quick remover, transparent background and cloth remover.

      ReplyDelete