Java中HashSet的用法
1. HashSet概念
2.Java文档中HashSet的实现
3.HashSet的构造函数
3.1 HashSet()
3.2 HashSet(int initialCapacity)
3.3 HashSet(int initialCapacity, float loadFactor)
3.4 HashSet(Collection)
4.HashSet的操作
4.1 添加元素add()
4.2 删除元素remove()
4.3 判断是否包含元素contains()
4.4 判断是否为空isEmpty()
4.5 获得大小size()
4.6 遍历HashSet
4.7 HashSet操作表格
练习:
1. HashSet概念
HashSet是基于HashMap来实现的,实现了Set接口,同时还实现了序列化和可克隆化。而集合(Set)是不允许重复值的。
所以HashSet是一个没有重复元素的集合,但不保证集合的迭代顺序,所以随着时间元素的顺序可能会改变。
由于HashSet是基于HashMap来实现的,所以允许空值,不是线程安全的。
想了解这两者的区别,可以去我的Java专栏看。
2.Java文档中HashSet的实现
HashSet是基于HashMap实现的,区别就在于在HashMap中输入一个键值对,而在HashSet中只输入一个值。
Java代码:
private transient HashMap map;
// Constructor - 1
// All the constructors are internally creating HashMap Object.
public HashSet()
{
// Creating internally backing HashMap object
map = new HashMap();
}
// Constructor - 2
public HashSet(int initialCapacity)
{
// Creating internally backing HashMap object
map = new HashMap(initialCapacity);
}
// Dummy value to associate with an Object in Map
private static final Object PRESENT = new Object();
而HashSet类的add的实现是这样子的,很明显可以看到是调用了HashMap里的put()方法。而里面的present则是常量,就是没用的。
public boolean add(E e)
{
return map.put(e, PRESENT) == null;
}
3.HashSet的构造函数
创建HashSet是首先需要创建一个HashSet类的对象,下面是几种构造函数。
3.1 HashSet()
该构造函数用于构建一个空的HashSet对象,其中默认初始容量为16,默认加载因子为0.75。如果我们希望创建一个名为 hs 的空 HashSet,则可以将其创建为:
示例:
HashSet<E> hs = new HashSet<E>();
//其实也可以写成,以下类推
Set<E> hs = new HashSet<E>();
3.2 HashSet(int initialCapacity)
该构造函数用于构建一个空的HashSet对象,在对象创建时指定initialCapacity。这里,默认的 loadFactor 保持为 0.75。
示例:
HashSet<E> hs = new HashSet<E>(int initialCapacity);
3.3 HashSet(int initialCapacity, float loadFactor)
该构造函数用于构建一个空的HashSet对象,其中在创建对象时指定了initialCapacity和loadFactor。
示例:
HashSet<E> hs = new HashSet<E>(int initialCapacity, float loadFactor);
3.4 HashSet(Collection)
此构造函数用于构建包含给定集合中所有元素的 HashSet 对象。简而言之,当需要从任何 Collection 对象到 HashSet 对象的任何转换时,都会使用此构造函数。如果我们希望创建一个名为 hs 的 HashSet,它可以创建为:
示例:
HashSet<E> hs = new HashSet<E>(Collection C);
4.HashSet的操作
4.1 添加元素add()
使用add(),但是HashSet不会存重复的元素,所以add相同的不会存进去。
示例:
// Java program to Adding Elements to HashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// AddingElementsToHashSet
class GFG {
// Method 1
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashSet of string entities
HashSet<String> hs = new HashSet<String>();
// Adding elements using add() method
hs.add("Geek");
hs.add("For");
hs.add("Geeks");
// Printing all string el=ntries inside the Set
System.out.println("HashSet elements : " + hs);
}
}
输出:
HashSet elements : [Geek, For, Geeks]
4.2 删除元素remove()
hs.remove("B");
示例:
// Java program Illustrating Removal Of Elements of HashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// RemoveElementsOfHashSet
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an
HashSet<String> hs = new HashSet<String>();
// Adding elements to above Set
// using add() method
hs.add("Geek");
hs.add("For");
hs.add("Geeks");
hs.add("A");
hs.add("B");
hs.add("Z");
// Printing the elements of HashSet elements
System.out.println("Initial HashSet " + hs);
// Removing the element B
hs.remove("B");
// Printing the updated HashSet elements
System.out.println("After removing element " + hs);
// Returns false if the element is not present
System.out.println("Element AC exists in the Set : "
+ hs.remove("AC"));
}
}
输出:
Initial HashSet [A, B, Geek, For, Geeks, Z]
移除元素后 [A, Geek, For, Geeks, Z]
元素AC存在于Set中:false
4.3 判断是否包含元素contains()
if (set1.contains(i))
4.4 判断是否为空isEmpty()
boolean con = hs.isEmpty();
4.5 获得大小size()
int size = hs.size();
4.6 遍历HashSet
使用iterator()遍历,也就是迭代器。
还有一种方法是for循环。
for (String s : hs){}
//或者是
Iterator itr = hs.iterator();
while (itr.hasNext()){}
示例:
// Java Program to Illustrate Iteration Over HashSet
// Importing required classes
import java.io.*;
import java.util.*;
// Main class
// IterateTheHashSet
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashSet of string entries
HashSet<String> hs = new HashSet<String>();
// Adding elements to above Set
// using add() method
hs.add("Geek");
hs.add("For");
hs.add("Geeks");
hs.add("A");
hs.add("B");
hs.add("Z");
// Iterating though the HashSet using iterators
Iterator itr = hs.iterator();
// Holds true till there is single element
// remaining in Set
while (itr.hasNext())
// Traversing elements and printing them
System.out.print(itr.next() + ", ");
System.out.println();
// Using enhanced for loop for traversal
for (String s : hs)
// Traversing elements and printing them
System.out.print(s + ", ");
System.out.println();
}
}
输出:
A,B,极客,对于,极客,Z,
A,B,极客,对于,极客,Z,
HashSet 操作的时间复杂度:HashSet的底层数据结构是 hashtable。因此,HashSet
的添加、删除和查找(包含方法)操作的摊销(平均或通常情况)时间复杂度需要O(1)时间。
练习:
今天在Leetcode刚写到有关HashSet的简单题,如果有时间可以去写一下,巩固一下:349. 两个数组的交集