【Java】Set之HashSet

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.方法摘要

booleanadd(E e)
Adds the specified element to this set if it is not already present.
voidclear()
Removes all of the elements from this set.
Objectclone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
booleancontains(Object o)
Returns true if this set contains the specified element.
booleanisEmpty()
Returns true if this set contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this set.
booleanremove(Object o)
Removes the specified element from this set if it is present.
intsize()
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());// 集合是否为空集

	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值