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: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  w  w  w.  ja  va2s .  c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Car> carsList = new ArrayList<Car>();
    Set<Car> carsset = new HashSet<Car>();
    File fXmlFile = new File("cars.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("cars");
    Car tempCar = null;/*from ww w  . j  a  v  a 2s  .c  o  m*/
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            tempCar = new Car();
            Element eElement = (Element) nNode;
            tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent());
            tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent());
            carsList.add(tempCar);
            carsset.add(tempCar);

        }
    }
    for (Car cs : carsset) {
        int count = 0;
        for (Car cl : carsList) {
            if (cs.equals(cl)) {
                count = count + 1;
            }
        }
        System.out.println(cs + "\t" + count);
    }
}

From source file:Synchronization.java

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

From source file:Set2.java

public static void main(String[] args) {
    test(new HashSet());
    test(new TreeSet());
    test(new LinkedHashSet());
}

From source file:TestCipher.java

public static void main(String args[]) throws Exception {
    Set set = new HashSet();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Point point = new Point(random.nextInt(1000), random.nextInt(2000));
        set.add(point);/*w  ww .  j av  a 2  s. c o m*/
    }
    int last = random.nextInt(5000);

    // Create Key
    byte key[] = password.getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    // Create Cipher
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Create stream
    FileOutputStream fos = new FileOutputStream("out.des");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    ObjectOutputStream oos = new ObjectOutputStream(cos);

    // Write objects
    oos.writeObject(set);
    oos.writeInt(last);
    oos.flush();
    oos.close();

    // Change cipher mode
    desCipher.init(Cipher.DECRYPT_MODE, secretKey);

    // Create stream
    FileInputStream fis = new FileInputStream("out.des");
    BufferedInputStream bis = new BufferedInputStream(fis);
    CipherInputStream cis = new CipherInputStream(bis, desCipher);
    ObjectInputStream ois = new ObjectInputStream(cis);

    // Read objects
    Set set2 = (Set) ois.readObject();
    int last2 = ois.readInt();
    ois.close();

    // Compare original with what was read back
    int count = 0;
    if (set.equals(set2)) {
        System.out.println("Set1: " + set);
        System.out.println("Set2: " + set2);
        System.out.println("Sets are okay.");
        count++;
    }
    if (last == last2) {
        System.out.println("int1: " + last);
        System.out.println("int2: " + last2);
        System.out.println("ints are okay.");
        count++;
    }
    if (count != 2) {
        System.out.println("Problem during encryption/decryption");
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.wikipediafilter.filter.util.FileFilter.java

public static void main(String[] args) throws IOException {
    Set<String> articles = new HashSet<String>();
    articles.addAll(FileUtils.readLines(new File("target/wikipedia/articles.txt")));

    BufferedWriter writer = new BufferedWriter(new FileWriter(new File("target/passwords.txt.filtered")));
    for (String line : FileUtils.readLines(new File("target/passwords.txt"))) {
        if (articles.contains(line.split("\t")[0].toLowerCase())) {
            writer.write(line);/*from   ww w.  j ava2 s . c o m*/
            writer.newLine();
        }
        writer.close();
    }
}

From source file:CollectionAll.java

public static void main(String[] args) {
    List list1 = new LinkedList();
    list1.add("list");
    list1.add("dup");
    list1.add("x");
    list1.add("dup");
    traverse(list1);/* www. j  a v  a2 s. co m*/
    List list2 = new ArrayList();
    list2.add("list");
    list2.add("dup");
    list2.add("x");
    list2.add("dup");
    traverse(list2);
    Set set1 = new HashSet();
    set1.add("set");
    set1.add("dup");
    set1.add("x");
    set1.add("dup");
    traverse(set1);
    SortedSet set2 = new TreeSet();
    set2.add("set");
    set2.add("dup");
    set2.add("x");
    set2.add("dup");
    traverse(set2);
    LinkedHashSet set3 = new LinkedHashSet();
    set3.add("set");
    set3.add("dup");
    set3.add("x");
    set3.add("dup");
    traverse(set3);
    Map m1 = new HashMap();
    m1.put("map", "Java2s");
    m1.put("dup", "Kava2s");
    m1.put("x", "Mava2s");
    m1.put("dup", "Lava2s");
    traverse(m1.keySet());
    traverse(m1.values());
    SortedMap m2 = new TreeMap();
    m2.put("map", "Java2s");
    m2.put("dup", "Kava2s");
    m2.put("x", "Mava2s");
    m2.put("dup", "Lava2s");
    traverse(m2.keySet());
    traverse(m2.values());
    LinkedHashMap /* from String to String */ m3 = new LinkedHashMap();
    m3.put("map", "Java2s");
    m3.put("dup", "Kava2s");
    m3.put("x", "Mava2s");
    m3.put("dup", "Lava2s");
    traverse(m3.keySet());
    traverse(m3.values());
}

From source file:SetTest.java

public static void main(String[] args) {
    Set<String> words = new HashSet<String>(); // HashSet implements Set
    long totalTime = 0;

    Scanner in = new Scanner(System.in);
    while (in.hasNext()) {
        String word = in.next();//from www  .jav  a 2s  . c o  m
        long callTime = System.currentTimeMillis();
        words.add(word);
        callTime = System.currentTimeMillis() - callTime;
        totalTime += callTime;
    }

    Iterator<String> iter = words.iterator();
    for (int i = 1; i <= 20 && iter.hasNext(); i++)
        System.out.println(iter.next());
    System.out.println(". . .");
    System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds.");
}

From source file:Test.java

public static void main(String[] args) {
    final AtomicLong orderIdGenerator = new AtomicLong(0);
    final List<Item> orders = Collections.synchronizedList(new ArrayList<Item>());

    for (int i = 0; i < 10; i++) {
        Thread orderCreationThread = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    long orderId = orderIdGenerator.incrementAndGet();
                    Item order = new Item(Thread.currentThread().getName(), orderId);
                    orders.add(order);/*from   ww  w  .j  ava  2s .  c  o m*/
                }
            }
        });
        orderCreationThread.setName("Order Creation Thread " + i);
        orderCreationThread.start();
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Set<Long> orderIds = new HashSet<Long>();
    for (Item order : orders) {
        orderIds.add(order.getID());
        System.out.println("Order id:" + order.getID());
    }
}

From source file:org.gwaspi.gui.TestReports.java

public static void main(final String[] args) throws InterruptedException, FileNotFoundException, IOException {

    File plinkFile = new File("/home/fernando/work/Moapi/input/plink.assoc");
    File outputFile = new File("/home/fernando/work/Moapi/input/plink.png");
    Set<String> redMarkers = new HashSet<String>();
    createCombinedPNGFromAssocUnadjLogPvsPos(plinkFile, outputFile, redMarkers, 4048, 700);

    log.info("This is a main test");
}