Creating a Set That Retains Order-of-Insertion : LinkedHashSet « Collections Data Structure « Java






Creating a Set That Retains Order-of-Insertion

 

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class Main {
  public static void main(String[] argv) throws Exception {

    Set<String> set = new LinkedHashSet<String>();

    // Add some elements
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("2");

    // List the elements
    for (Iterator it = set.iterator(); it.hasNext();) {
      Object o = it.next();
    }
  }
}

   
  








Related examples in the same category

1.Get Size of Java LinkedHashSet
2.Iterate through elements of Java LinkedHashSet
3.Remove all elements from Java LinkedHashSet
4.Remove specified element from Java LinkedHashSet
5.Simple Java LinkedHashSet