1.构造函数
HashSet()
Constructs a new, empty set; the backing
HashMap instance has default initial capacity (16) and load factor (0.75).
|
HashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
|
HashSet(int initialCapacity)
Constructs a new, empty set; the backing
HashMap instance has the specified initial capacity and default load factor (0.75).
|
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing
HashMap instance has the specified initial capacity and the specified load factor.
2.方法摘要 |
boolean | add(E e)
Adds the specified element to this set if it is not already present.
|
void | clear()
Removes all of the elements from this set.
|
Object | clone()
Returns a shallow copy of this
HashSet instance: the elements themselves are not cloned.
|
boolean | contains(Object o)
Returns
true if this set contains the specified element.
|
boolean | isEmpty()
Returns
true if this set contains no elements.
|
Iterator<E> | iterator()
Returns an iterator over the elements in this set.
|
boolean | remove(Object o)
Removes the specified element from this set if it is present.
|
int | size()
Returns the number of elements in this set (its cardinality).
|
3.程序实例
public class HashSetInstance {
public static void main(String[] args) {
int length = 1000;
Random random = new Random(25);
Set<Integer> intSet = new HashSet<Integer>();
for (int i = 0; i < length; i++) {
intSet.add(random.nextInt(30));
}
System.out.print(intSet);
// HashSet以散列的形式存放集合中的各个元素
Set<String> set1 = new HashSet<String>();
System.out.print("--Is the set1 empty? --");
System.out.println(set1.isEmpty());// 集合是否为空集
// 各一个集合添加一些元素
Collections.addAll(set1, "A B C D E F G".split(" "));// 以split参数中的字符作为分隔标识符,用","分割字符串
System.out.println(set1);
System.out.print("--Is the set1 empty? --");
System.out.println(set1.isEmpty());// 集合是否为空集
System.out.print("The size of Set1=");
System.out.println(set1.size());// 一个集合的大小,即一个集合中元素的个数
System.out.print("B is in set:");
System.out.println(set1.contains("B"));// 判断某一个元素是否在集合中存在
System.out.print("X is in set:");
System.out.println(set1.contains("X"));
Set<String> set2 = new HashSet<String>();
Collections.addAll(set2, "E F G H I J".split(" "));
System.out.println(set2);
System.out.print("Set1 contains Set2:");
System.out.println(set1.containsAll(set2));// 判断set2是否是set1的子集
Set<String> set3 = new HashSet<String>();
Collections.addAll(set3, "U V W X Y Z".split(" "));
System.out.println(set3);
set3.remove("W");// 经一个元素从集合中删除
System.out.println(set3);
// 使用迭代器遍历输出集合中的各个元素
Iterator<String> iterator = set3.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ";");
}
System.out.println();
set1.removeAll(set2);// 将从set1中删除与集合set2中相同的元素,即set1=set1-set1与set2的交集
System.out.println(set1);
System.out.println(set2);
System.out.print("The size of Set1=");
System.out.println(set1.size());// 一个集合的大小,即一个集合中元素的个数
set1.clear();// 将集合置为空集,即删除集合中的所有元素
System.out.print("--Is the set1 empty? --");
System.out.println(set1.isEmpty());// 集合是否为空集
}
}