Example usage for java.util Set add

List of usage examples for java.util Set add

Introduction

In this page you can find the example usage for java.util Set add.

Prototype

boolean add(E e);

Source Link

Document

Adds the specified element to this set if it is not already present (optional operation).

Usage

From source file:IteratorEnumeration.java

public static void main(String[] args) {
    Set<String> set = new HashSet<String>();
    set.add("a");
    set.add("b");
    set.add("c");
    set.add("d");
    Enumeration<String> x = new IteratorEnumeration<String>(set.iterator());
    while (x.hasMoreElements()) {
        System.out.println(x.nextElement());
    }//from   www.  ja v a 2s. com
}

From source file:Main.java

public static void main(String[] argv) {
    // Create the sorted set
    Set<String> set = new TreeSet<String>();

    set.add("b");
    set.add("c");
    set.add("a");

    Iterator it = set.iterator();
    while (it.hasNext()) {

        Object element = it.next();
        System.out.println(element);
    }/* w  w  w . ja v  a2  s  .c  o m*/

    // Create an array containing the elements in a set
    String[] array = (String[]) set.toArray(new String[set.size()]);
    Arrays.toString(array);
}

From source file:SyncTest.java

public static void main(String args[]) {
    Set simpsons = new HashSet();
    simpsons.add("Bart");
    simpsons.add("Hugo");
    simpsons.add("Lisa");
    simpsons.add("Marge");
    simpsons.add("Homer");
    simpsons.add("Maggie");
    simpsons.add("Roy");
    simpsons = Collections.synchronizedSet(simpsons);
    synchronized (simpsons) {
        Iterator iter = simpsons.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }//from w  ww.j a  v a2 s.co m
    }
    Map map = Collections.synchronizedMap(new HashMap(89));
    Set set = map.entrySet();
    synchronized (map) {
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Set simpsons = new HashSet();
    simpsons.add("B");
    simpsons.add("H");
    simpsons.add("L");
    simpsons = Collections.synchronizedSet(simpsons);
    synchronized (simpsons) {
        Iterator iter = simpsons.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }/*from w w  w  .java2 s .co  m*/
    }
    Map map = Collections.synchronizedMap(new HashMap(89));
    Set set = map.entrySet();
    synchronized (map) {
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

From source file:SetDemo.java

public static void main(String[] argv) {
    //+//w ww . j a va  2  s  .  c  o  m
    Set h = new HashSet();
    h.add("One");
    h.add("Two");
    h.add("One"); // DUPLICATE
    h.add("Three");
    Iterator it = h.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    //-
}

From source file:Product.java

public static void main(String args[]) {
    Set<Product> prodList = new TreeSet<Product>();

    prodList.add(new Product("A", 1));
    prodList.add(new Product("B", 0));
    prodList.add(new Product("C", 2));
    prodList.add(new Product("D", 4));

    for (Product p : prodList)
        System.out.printf("%-14s ID: %d\n", p.prodName, p.prodID);
}

From source file:Product.java

public static void main(String args[]) {
    Set<Product> prodList = new TreeSet<Product>();

    prodList.add(new Product("A", 13546));
    prodList.add(new Product("B", 04762));
    prodList.add(new Product("C", 12221));
    prodList.add(new Product("D", 44387));

    for (Product p : prodList)
        System.out.printf("%-14s ID: %d\n", p.prodName, p.prodID);
}

From source file:MainClass.java

public static void main(String[] args) {

    // Create two sets.
    Set s1 = new HashSet();
    s1.add("A");
    s1.add("B");//from   w w w .j av a 2s  . co  m
    s1.add("C");

    Set s2 = new HashSet();
    s2.add("A");
    s2.add("B");

    Set union = new TreeSet(s1);
    union.addAll(s2); // now contains the union

    print("union", union);

    Set intersect = new TreeSet(s1);
    intersect.retainAll(s2);

    print("intersection", intersect);

}

From source file:FindDups.java

public static void main(String[] args) {
    Set<String> s = new HashSet<String>();
    for (String a : args)
        if (!s.add(a))
            System.out.println("Duplicate detected: " + a);

    System.out.println(s.size() + " distinct words: " + s);
}

From source file:Main.java

public static void main(String args[]) {
    Set<String> hs = new HashSet<String>();

    hs.add("one");
    hs.add("two");
    hs.add("three");

    System.out.println("Here is the HashSet: " + hs);

    if (!hs.add("three"))
        System.out.println("Attempt to add duplicate. " + "Set is unchanged: " + hs);

    TreeSet<Integer> ts = new TreeSet<Integer>();

    ts.add(8);/*  www. j a  v  a 2 s.c  om*/
    ts.add(19);
    ts.add(-2);
    ts.add(3);

    System.out.println(ts);

    System.out.println("First element in ts: " + ts.first());
    System.out.println("Last element in ts: " + ts.last());

    System.out.println("First element > 15: " + ts.higher(15));
    System.out.println("First element < 15: " + ts.lower(15));
}