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(int initialCapacity) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    List<String> myList = new ArrayList<String>();
    myList.add("A");
    myList.add("B");
    myList.add("C");
    myList.add("D");

    Set<String> mySet = new HashSet<String>(myList);

    for (Object theFruit : mySet)
        System.out.println(theFruit);
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new HashSet(Arrays.asList(elements));

    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);/*w ww .  ja va2  s  .  c o m*/
    oos.close();

    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set anotherSet = (Set) ois.readObject();
    ois.close();
    System.out.println(anotherSet);
}

From source file:Main.java

public static void main(String[] args) {

    String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";

    List<String> list = Arrays.asList(text.split(" "));

    Set<String> uniqueWords = new HashSet<String>(list);
    for (String word : uniqueWords) {
        System.out.println(word + ": " + Collections.frequency(list, word));
    }/*from   www .j a v a  2  s .c o m*/
}

From source file:Main.java

public static void main(String[] argv) {
    List<String> arrayList1 = new ArrayList<String>();

    arrayList1.add("A");
    arrayList1.add("A");
    arrayList1.add("B");
    arrayList1.add("B");
    arrayList1.add("B");
    arrayList1.add("C");

    HashSet<String> hashSet = new HashSet<String>(arrayList1);

    List<String> arrayList2 = new ArrayList<String>(hashSet);

    for (Object item : arrayList2)
        System.out.println(item);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);//from ww  w. j av a  2  s. c  o m
    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);
    oos.close();
    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set set3 = (Set) ois.readObject();
    ois.close();
    System.out.println(set3);
}

From source file:Main.java

public static void main(String[] args) {
    // A string array with duplicate values
    String[] data = { "A", "C", "B", "D", "A", "B", "E", "D", "B", "C" };
    System.out.println("Original array         : " + Arrays.toString(data));

    List<String> list = Arrays.asList(data);
    Set<String> set = new HashSet<String>(list);

    System.out.print("Remove duplicate result: ");

    String[] result = new String[set.size()];
    set.toArray(result);//from w  w w. j av a 2 s .  c  o m
    for (String s : result) {
        System.out.print(s + ", ");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    List list2 = new LinkedList(list);

    Set set = new HashSet(stuff);
    Set set2 = new TreeSet(set);

    Map map = new HashMap();
    Map map2 = new TreeMap(map);
}

From source file:Main.java

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

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//from  w w w  .  j  a v a2 s  .  c om
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {

    }

    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException, ExecutionException {
    List<String> searchList = new ArrayList<String>(7);
    searchList.add("hello");
    searchList.add("world");
    searchList.add("CSS");
    searchList.add("debian");
    searchList.add("linux");
    searchList.add("HTML");
    searchList.add("stack");

    Set<String> targetSet = new HashSet<String>(searchList);
    Set<String> matchSet = findMatches(searchList, targetSet);
    for (String match : matchSet) {
        System.out.println("match:  " + match);
    }//from   w  w  w  .j av a 2  s .  c o  m
}

From source file:KeyboardFocusManagerDemo.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));

    Set<AWTKeyStroke> set = p.getFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
    set = new HashSet(set);
    KeyStroke up = KeyStroke.getKeyStroke("A");
    set.add(up);//from www  .  j  ava  2  s.c  o m
    System.out.println(set);

    p.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);

    aWindow.add(p);

    aWindow.setVisible(true); // Display the window
}