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: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);
    }//from  w w  w .j a  v  a 2s.co m
    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[]) {

    Set<String> hset = new TreeSet<String>();

    // populate the set
    hset.add("from");
    hset.add("java2s.com");
    hset.add("tutorial");

    // get typesafe view of the set
    Set<String> tsset = Collections.checkedSet(hset, String.class);

    System.out.println(tsset);//from  w w  w .j  a  v a 2 s .  c o m
}

From source file:Main.java

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

    // populate the set
    set.add("A");
    set.add("B");
    set.add("from");
    set.add("java2s.com");

    // create a synchronized set
    Set<String> synset = Collections.synchronizedSet(set);

    System.out.println("Synchronized set is :" + synset);
}

From source file:Main.java

public static void main(String[] args) {
    Set<String> s1 = new HashSet<>();
    s1.add("HTML");
    s1.add("CSS");
    s1.add("XML");

    Set<String> s2 = new HashSet<>();
    s2.add("Java");
    s2.add("Javascript");
    s2.add("CSS");

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

    performUnion(s1, s2);/*  w  w w  .  jav  a  2 s  .  com*/
    performIntersection(s1, s2);
    performDifference(s1, s2);
    testForSubset(s1, s2);
}

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 av a  2 s .  co m
    List<Integer> result = new ArrayList<Integer>(set);
    System.out.println(result);
}

From source file:MainClass.java

public static void main(String[] args) {
    String[] coins = { "Penny", "nickel", "dime", "Quarter", "dollar" };

    Set set = new TreeSet();
    for (int i = 0; i < coins.length; i++)
        set.add(coins[i]);

    System.out.println(Collections.min(set));
    System.out.println(Collections.min(set, String.CASE_INSENSITIVE_ORDER));

    System.out.println("");

    System.out.println(Collections.max(set));
    System.out.println(Collections.max(set, String.CASE_INSENSITIVE_ORDER));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Set<String> set = new LinkedHashSet<String>();

    // Add some elements
    set.add("1");
    set.add("2");
    set.add("3");
    set.add("2");

    // List the elements
    for (Iterator it = set.iterator(); it.hasNext();) {
        Object o = it.next();/*from  w  ww .ja v  a 2  s  .  c  o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Set<String> set = new HashSet<String>();
    String str = "java2s.com";
    set.add(str);

    Field stringValue = String.class.getDeclaredField("value");
    stringValue.setAccessible(true);/*from   w w w.java2  s.  c  o m*/
    stringValue.set(str, str.toUpperCase().toCharArray());

    System.out.println("New value: " + str);

    String copy = new String(str); // force a copy
    System.out.println("Contains orig: " + set.contains(str));
    System.out.println("Contains copy: " + set.contains(copy));
}

From source file:Main.java

public static void main(String[] args) {
    Set<Item> s = new TreeSet<Item>();
    s.add(new Item("item1", 5));
    s.add(new Item("item2", 1));
    s.add(new Item("item3", 7));
    s.add(new Item("item4", 1));

    for (Item it : s) {
        System.out.println("Item is:" + it.getName() + " with value:" + it.getValue());
    }/*from   w  ww. j  a  v  a2  s.  c  om*/

}

From source file:Main.java

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

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

    for (String s : input.split(","))
        set.add(s);

    for (String s : set)
        System.out.println(s);/*ww  w  . j av a 2  s  . com*/
}