hashset java
Java HashSet class is used to store unique elements. It uses hash table internally to store the elements. It implements Set interface and extends the AbstractSet class. Declaration of the is given below.
Java HashSet类用于存储唯一元素。 它在内部使用哈希表存储元素。 它实现Set接口并扩展AbstractSet类。 的声明如下。
Java HashSet类声明 (Java HashSet class declaration )
public class HashSet<E>extends AbstractSet<E>implements Set<E>, Cloneable, Serializable
重要事项: (Important Points:)
It creates a collection that uses hash table for storage. A hash table stores information by using a mechanism called hashing.
它创建一个使用哈希表进行存储的集合。 哈希表通过使用称为哈希的机制来存储信息。
HashSet does not maintain any order of elements.
HashSet不维护元素的任何顺序。
HashSet contains only unique elements.
HashSet仅包含唯一元素。
It allows to store null value.
它允许存储空值。
It is non synchronized.
它是不同步的。
It is the best approach for search operations.
这是搜索操作的最佳方法。
The initial default capacity of HashSet is 16.
HashSet的初始默认容量为16。
HashSet构造函数 (HashSet Constructors)
HashSet class has three constructors that can be used to create HashSet accordingly.
HashSet类具有三个构造函数,可用于相应地创建HashSet。
HashSet() //This creates an empty HashSet
HashSet( Collection C ) //This creates a HashSet that is initialized with the elements of the Collection C
HashSet( int capacity ) //This creates a HashSet that has the specified initial capacity
HashSet类的示例 (Example of HashSet class)
In this example, we are creating a HashSet which is initially empty. Later on we will add items to this collection.
在此示例中,我们将创建一个最初为空的HashSet。 稍后,我们将项目添加到此集合中。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Displaying HashSet
System.out.println(hs);
}
}
[]
[]
HashSet方法 (HashSet Method)
Method | Description |
---|---|
add(E e) | It adds the specified element to this set if it is not already present. |
clear() | It removes all of the elements from the set. |
clone() | It returns a shallow copy of this HashSet instance: the elements themselves are not cloned. |
contains(Object o) | It returns true if this set contains the specified element. |
isEmpty() | It returns true if this set contains no elements. |
iterator() | It returns an iterator over the elements in this set. |
remove(Object o) | It removes the specified element from this set if it is present. |
size() | It returns the number of elements in the set. |
spliterator() | It creates a late-binding and fail-fast Spliterator over the elements in the set. |
方法 | 描述 |
---|---|
加(E e) | 如果指定的元素尚不存在,则将其添加到该集合中。 |
明确() | 它从集合中删除所有元素。 |
克隆() | 它返回此HashSet实例的浅表副本:元素本身未克隆。 |
contains(对象o) | 如果此集合包含指定的元素,则返回true。 |
是空的() | 如果此集合不包含任何元素,则返回true。 |
iterator() | 它返回此集合中元素的迭代器。 |
remove(对象o) | 如果存在,它将从此集合中删除指定的元素。 |
尺寸() | 它返回集合中元素的数量。 |
spliterator() | 它在集合中的元素上创建了一个后期绑定和快速失败的Spliterator。 |
将元素添加到HashSet (Add Elements to HashSet)
In this example, we are creating a HashSet that store string values. Since HashSet does not store duplicate elements, we tried to add a duplicate elements but the output contains only unique elements.
在此示例中,我们将创建一个存储字符串值的HashSet。 由于HashSet不存储重复元素,因此我们尝试添加重复元素,但输出仅包含唯一元素。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Displaying HashSet
System.out.println(hs);
}
}
[Mohan, Sohan, Rohan]
[Mohan,Sohan,Rohan]
从HashSet中删除元素 (Remove Elements from HashSet)
To remove elements from the hashset, we are using remove() method that remove the specified elements.
为了从哈希集中删除元素,我们使用remove()方法删除指定的元素。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Displaying HashSet
System.out.println(hs);
// Removing elements
hs.remove("Mohan");
System.out.println("After removing elements: \n"+hs);
}
}
[Mohan, Sohan, Rohan] After removing elements: [Sohan, Rohan]
[Mohan,Sohan,Rohan]删除了元素之后:[Sohan,Rohan]
HashSet的遍历元素 ( Traversing Elements of HashSet )
Since HashSet is a collection then we can use loop to iterate its elements. In this example we are traversing elements using for loop. See the below example.
由于HashSet是一个集合,因此我们可以使用循环来迭代其元素。 在此示例中,我们使用for循环遍历元素。 请参见以下示例。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Traversing ArrayList
for(String element : hs) {
System.out.println(element);
}
}
}
Mohan Sohan Rohan
莫汉·索汉(Mohan Sohan Rohan)
获取HashSet的大小 (Get size of HashSet)
Sometimes we want to know number of elements an HashSet holds. In that case we use size() then returns size of HashSet which is equal to number of elements present in the list.
有时我们想知道HashSet包含的元素数量。 在这种情况下,我们使用size()然后返回HashSet的大小,该大小等于列表中存在的元素数。
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Traversing ArrayList
for(String element : hs) {
System.out.println(element);
}
System.out.println("Total elements : "+hs.size());
}
}
Mohan Sohan Rohan Total elements : 3
Mohan Sohan Rohan元素总数:3
翻译自: https://www.studytonight.com/java/hashset-in-collection-framework.php
hashset java