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:HashSetExample.java

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

    set.add(new Integer(1));
    set.add(new Integer(2));
    set.add(new Integer(3));
    set.add(new Integer(4));
    set.add(new Integer(5));
    set.add(new Integer(6));
    set.add(new Integer(7));
    set.add(new Integer(8));
    set.add(new Integer(9));
    set.add(new Integer(10));

    // Use iterator to display the vsetes
    System.out.println("HashSet Before: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
        Integer integer = (Integer) i.next();
        System.out.println(integer);
    }//w  w w . j a v a 2s.c o  m

    // Remove the integer 6
    System.out.println("\nRemove integer 6");
    set.remove(new Integer(6));

    // Use iterator to display the vsetes
    System.out.println("\nHashSet After: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
        Integer integer = (Integer) i.next();
        System.out.println(integer);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>();

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);
}

From source file:Main.java

public static void main(String[] args) {
    Set<Object> result = new HashSet<Object>();
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        Set<Object> keys = provider.keySet();
        for (Object key : keys) {
            String data = (String) key;
            data = data.split(" ")[0];
            if (data.startsWith("Alg.Alias")) {
                data = data.substring(10);
            }//  w ww  .  j a  v a  2s .  c o m
            data = data.substring(0, data.indexOf('.'));
            result.add(data);
        }
    }
    for (Object o : result) {
        System.out.println("Service Type = " + o);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Set<Object> set = new HashSet<Object>();
    set.add("A");
    set.add(new Long(10));
    set.add(new Date());

    List<Object> list = new ArrayList<Object>(set);
    Object[] objects = list.toArray();

    for (int i = 0; i < objects.length; i++) {
        Object object = objects[i];
        System.out.println("Object = " + object);
    }//from  w ww  .j av a 2 s .  co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>();

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}

From source file:Main.java

public static void main(final String[] args) throws Exception {
    Random random = new Random();
    Set<Integer> intSet = new HashSet<>();
    while (intSet.size() < 6) {
        intSet.add(random.nextInt(49) + 1);
    }/* w w w. j  a v  a2 s .c om*/
    Integer[] ints = intSet.toArray(new Integer[intSet.size()]);
    System.out.println(Arrays.toString(ints));
}

From source file:Main.java

public static void main(String[] args) {
    Random random = new Random();
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < 5) {
        set.add(random.nextInt());//from ww  w .  j a v a 2  s.c o  m
    }
    List<Integer> result = new ArrayList<Integer>(set);
    System.out.println(result);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {
  Set result = new HashSet();
  String serviceType = "KeyFactory";
  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(serviceType + ".")) {
        result.add(key.substring(serviceType.length() + 1));
      } else if (key.startsWith("Alg.Alias." + serviceType + ".")) {
        result.add(key.substring(serviceType.length() + 11));
      }/* w  w  w.  java2s.  c  o  m*/
    }
  }
  System.out.println(result);
}

From source file:ItemSet.java

public static void main(String args[]) {
    String names[] = { "Item 1", "Item 2", "Item 3" };
    Set moons = new HashSet();
    int namesLen = names.length;
    int index;//from w w  w. ja va 2 s  .c  o m
    for (int i = 0; i < 100; i++) {
        index = (int) (Math.random() * namesLen);
        moons.add(names[index]);
    }
    Iterator it = moons.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
    System.out.println("---");
    Set orderedMoons = new TreeSet(moons);
    it = orderedMoons.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
}

From source file:Main.java

public static void main(String[] arg) throws Exception {
    Runnable parallelCode = () -> {
        HashSet<String> allThreads = new HashSet<>();
        IntStream.range(0, 1_000_000).parallel().filter(i -> {
            allThreads.add(Thread.currentThread().getName());
            return false;
        }).min();// w w  w  .j  ava  2 s .com
        System.out.println("executed by " + allThreads);
    };
    System.out.println("default behavior: ");
    parallelCode.run();
    System.out.println("specialized pool:");
    ForkJoinPool pool = new ForkJoinPool(2);
    pool.submit(parallelCode).get();
}