Java by API/java.util/Set

Материал из Java эксперт
Перейти к: навигация, поиск

Set: addAll(Collection c)

 
/*
 Output:
true
false
 * */
import java.util.HashSet;
import java.util.Set;
public class MainClass {
  public static void main(String args[]) {
    Set set = new HashSet();
    Set set2 = new HashSet();
    StringBuffer buff1 = new StringBuffer("Irish Setter");
    StringBuffer buff2 = new StringBuffer("Irish Setter");
    set.add(buff1);
    set2.add(buff2);
    System.out.println(set.addAll(set2));
    System.out.println(set.addAll(set));
  }
}





Set: add(String e)

 
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    Set<String> set = new HashSet<String>();
    set.add("Hello");
    if (set.add("Hello")) {
      System.out.println("addition successful");
    } else {
      System.out.println("addition failed");
    }
  }
}





Set: clear()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "E", "F" };
    set.addAll(Arrays.asList(elements));
    System.out.println(set);
    set.clear();
    System.out.println(set);
  }
}





Set: clone()

 
/*
 Output:
[I, P, G, E]
[I, P, G, E]
 * */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MainClass {
  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);
    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);
  }
}





Set: containsAll(Collection<?> c)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "A", "B", "C" };
    Set<String> set2 = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.containsAll(set2));
  }
}





Set: contains(Object o)

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.contains("A"));
  }
}





Set: equals(Object o)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "A", "B", "C", "D" };
    Set<String> set2 = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.equals(set2));
  }
}





Set: isEmpty()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.isEmpty());
  }
}





Set: iterator()

 
/*
 Output:
I
P
G
E
 * */
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class MainClass {
  public static void main(String args[]) {
    String elements[] = { "I", "P", "E", "G", "P" };
    Set set = new HashSet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}





Set: removeAll(Collection<?> c)

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "B", "D", "F", "G", "1", "2", "3", "4" };
    Set<String> set2 = new HashSet<String>(Arrays.asList(elements));
    set.removeAll(set2);
    System.out.println(set);
  }
}





Set: remove(Object o)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    set.remove("A");
    System.out.println(set);
  }
}





Set: retainAll(Collection c)

 
/*
--------------union--------------
A
B
C
--------------intersection--------------
A
B*/
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class MainClass {
  public static void main(String[] args) {
    // Create two sets.
    Set s1 = new HashSet();
    s1.add("A");
    s1.add("B");
    s1.add("C");
    Set s2 = new HashSet();
    s2.add("A");
    s2.add("B");
    Set union = new TreeSet(s1);
    union.addAll(s2); // now contains the union
    print("union", union);
    Set intersect = new TreeSet(s1);
    intersect.retainAll(s2);
    print("intersection", intersect);
  }
  protected static void print(String label, Collection c) {
    System.out.println("--------------" + label + "--------------");
    Iterator it = c.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}





Set: size()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.size());
  }
}





Set: toArray()

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    Object[] arrObj = set.toArray();
    for (int i = 0; i < arrObj.length; i++) {
      System.out.println(arrObj[i]);
    }
    System.out.println(set); 
  }
}





Set: toArray(String[] a)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  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);
    for (String s : result) {
      System.out.print(s + ", ");
    }
  }
}