Example usage for java.util HashSet HashSet

List of usage examples for java.util HashSet HashSet

Introduction

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

Prototype

public HashSet() 

Source Link

Document

Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).

Usage

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 w w  . ja  v  a  2 s.  c  o  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:Main.java

  public static void main(String[] argv) throws Exception {
  Set result = new HashSet();
  Provider[] providers = Security.getProviders();
  for (int i = 0; i < providers.length; i++) {
    Set keys = providers[i].keySet();
    for (Iterator it = keys.iterator(); it.hasNext();) {
      String key = (String) it.next();
      key = key.split(" ")[0];

      if (key.startsWith("Alg.Alias.")) {
        // Strip the alias
        key = key.substring(10);//from www.  jav  a2 s  .c o m
      }
      int ix = key.indexOf('.');
      result.add(key.substring(0, ix));
    }
  }
  System.out.println(result);
}

From source file:SetDemo.java

public static void main(String[] args) {
    String[] letters = { "A", "B", "C", "D", "E" };

    Set s = new HashSet();

    for (int i = 0; i < letters.length; i++)
        s.add(letters[i]);/*from www .  ja v a  2s . com*/

    if (!s.add(letters[0]))
        System.out.println("Cannot add a duplicate.\n");

    Iterator iter = s.iterator();
    while (iter.hasNext())
        System.out.println(iter.next());

    System.out.println("");

    SortedSet ss = new TreeSet();

    for (int i = 0; i < letters.length; i++)
        ss.add(letters[i]);

    if (!ss.add(letters[0]))
        System.out.println("Cannot add a duplicate.\n");

    iter = ss.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  ww.ja 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) {

    // Create two sets.
    Set s1 = new HashSet();
    s1.add("A");/*  w ww  .  j av a  2s  .c o  m*/
    s1.add("B");
    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:MainClass.java

public static void main(String args[]) {
    String[] name1 = { "Amy", "Jose", "Jeremy", "Alice", "Patrick" };

    String[] name2 = { "Alan", "Amy", "Jeremy", "Helen", "Alexi" };

    String[] name3 = { "Adel", "Aaron", "Amy", "James", "Alice" };

    Set<String> letter = new HashSet<String>();

    for (int i = 0; i < name1.length; i++)
        letter.add(name1[i]);/*w  ww.ja  va 2 s .  c om*/

    for (int j = 0; j < name2.length; j++)
        letter.add(name2[j]);

    for (int k = 0; k < name3.length; k++)
        letter.add(name3[k]);

    System.out.println(letter.size() + " letters must be sent to: " + letter);

}

From source file:SetExampleV2.java

public static void main(String args[]) {
    // create two sets
    Set set1 = new HashSet();
    set1.add("Red");
    set1.add("Green");

    Set set2 = new HashSet();
    set2.add("Yellow");
    set2.add("Red");

    // create a composite set out of these two
    CompositeSet composite = new CompositeSet();

    // set the class that handles additions, conflicts etc
    // composite.setMutator(new CompositeMutator());

    // initialize the composite with the sets
    // Cannot be used if set1 and set2 intersect is not null and
    // a strategy to deal with it has not been set
    composite.addComposited(new Set[] { set1, set2 });

    // do some addition/deletions
    // composite.add("Pink");
    // composite.remove("Green");

    // whats left in the composite?
    Iterator itr = composite.iterator();

    while (itr.hasNext()) {
        System.err.println(itr.next());
    }// w w w . jav a2 s .c  o m
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] arr = { 1, 2, 3, 1, 4, 4, 1, 5 };
    System.out.println(Arrays.toString(arr));
    List<Integer> l = Arrays.asList(arr);

    System.out.println(l);/*from w  ww  . ja  v  a  2  s  .  c  o m*/

    Set<Integer> set = new HashSet<Integer>();
    for (int j = 0; j < l.size(); j++) {
        if (Collections.frequency(l, l.get(j)) > 1) {
            set.add(l.get(j));
        }
    }
    System.out.println("dups are:" + set);
}

From source file:Main.java

public static void main(String[] args) {
    Collection<String> c = Collections.synchronizedCollection(new ArrayList<String>());
    List<String> list = Collections.synchronizedList(new ArrayList<String>());
    Set<String> s = Collections.synchronizedSet(new HashSet<String>());
    Map<String, String> m = Collections.synchronizedMap(new HashMap<String, String>());
}

From source file:Main.java

public static void main(String[] args) throws java.lang.Exception {
    List<Person> people = new ArrayList<Person>();
    people.add(new Person("J", "S"));
    people.add(new Person("J", "S"));
    people.add(new Person("J", "F"));
    people.add(new Person("J", "W"));
    people.add(new Person("J", "B"));
    Set<Object> seen = new HashSet<Object>();
    for (Person p : people) {

        Wrap wrap = new Wrap(p);
        if (seen.add(wrap)) {
            System.out.println(p + " is new");
        } else {/*from w  ww .  j av  a2 s.c om*/
            System.out.println(p + " is a duplicate");
        }
    }
}