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: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);
    }//from ww  w. j  a  v  a2s.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[] args) {
    Set<Counter> set = new TreeSet<Counter>();

    set.add(new Counter("A", 1));
    set.add(new Counter("B", 5));
    set.add(new Counter("C", 10));
    set.add(new Counter("D", 4));
    set.add(new Counter("E", 3));
    set.add(new Counter("F", 5));
    set.add(new Counter("G", 15));
    set.add(new Counter("H", 225));
    set.add(new Counter("I", 225));
    for (Counter f : set) {
        System.out.println(f);/*from  w  w  w .j  a  va 2 s.  co  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    // Add a few elements
    s1.add("HTML");
    s1.add("CSS");
    s1.add("XML");
    s1.add("XML"); // Duplicate

    // Create another set by copying s1
    Set<String> s2 = new HashSet<>(s1);
    // Add a few more elements 
    s2.add("Java");
    s2.add("SQL");
    s2.add(null); // one null is fine
    s2.add(null); // Duplicate

    System.out.println("s1: " + s1);
    System.out.println("s1.size(): " + s1.size());

    System.out.println("s2: " + s2);
    System.out.println("s2.size(): " + s2.size());
}

From source file:FindDups.java

public static void main(String[] args) {
    Set<String> s = new HashSet<String>();
    for (String a : args)
        s.add(a);
    System.out.println(s.size() + " distinct words: " + s);
}

From source file:SetStuff.java

public static void main(String[] args) {

    // Create two sets.
    Set s1 = new HashSet();
    s1.add("Ian Darwin");
    s1.add("Bill Dooley");
    s1.add("Jesse James");

    Set s2 = new HashSet();
    s2.add("Ian Darwin");
    s2.add("Doolin' Dalton");

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

    print("union", union);

    Set intersect = new TreeSet(s1);
    intersect.retainAll(s2);//w  ww.  ja v  a  2 s  .  c  om

    print("intersection", intersect);

}

From source file:Main.java

public static void main(String args[]) {

    Map<String, Boolean> map = new WeakHashMap<String, Boolean>();

    // create a set from map
    Set<String> set = Collections.newSetFromMap(map);

    // add values in set
    set.add("Java");
    set.add("C");
    set.add("C++");
    set.add("from java2s.com");

    // set and map values are
    System.out.println("Set is: " + set);
    System.out.println("Map is: " + map);
}

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());
    }/*from ww w.j a  v  a  2s  .  c o  m*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Set map = new ArraySet();
    map.add("V");
    map.add("M");
    map.add("N");
    System.out.println(map);//from  ww  w.  jav a  2  s .  c o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setValidating(false);//from  ww w.j a  va2 s . c  om
    domFactory.setNamespaceAware(true);
    domFactory.setIgnoringComments(true);
    domFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse("C:/data.xsd");

    Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0);
    System.out.println(rootNode.getNodeName());

    XPath xPath1 = XPathFactory.newInstance().newXPath();
    NamespaceContext nsContext = new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            return "http://www.w3.org/2001/XMLSchema";
        }

        @Override
        public String getPrefix(String namespaceURI) {
            return "xs";
        }

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            Set s = new HashSet();
            s.add("xs");
            return s.iterator();
        }
    };
    xPath1.setNamespaceContext((NamespaceContext) nsContext);
    NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET);
    System.out.println(nList1.item(0).getNodeName());

    NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET);
    System.out.println(nList2.item(0).getNodeName());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("a");

    Set set = new HashSet(component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

    set.add(KeyStroke.getKeyStroke("F2"));
    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}