Java教程 第35章 HashSet

作者: 安卓技术砖家 | 来源:发表于2021-08-08 23:41 被阅读0次

HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。

HashSet 允许有 null 值。

HashSet 是无序的,即不会记录插入的顺序。

HashSet 不是线程安全的, 如果多个线程尝试同时修改 HashSet,则最终结果是不确定的。 您必须在多线程访问时显式同步对 HashSet 的并发访问。

HashSet 实现了 Set 接口。

图片

HashSet 中的元素实际上是对象,一些常见的基本类型可以使用它的包装类。

基本类型对应的包装类表如下:

基本类型 引用类型
boolean Boolean
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character

HashSet 类位于 java.util 包中,使用前需要引入它,语法格式如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">import java.util.HashSet; // 引入 HashSet 类</pre>

以下实例我们创建一个 HashSet 对象 sites,用于保存字符串元素:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">HashSet<String> sites = new HashSet<String>();</pre>

添加元素

HashSet 类提供类很多有用的方法,添加元素可以使用 add() 方法:

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
System.out.println(sites);
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[Google, Runoob, Zhihu, Taobao]</pre>

在上面的实例中,Runoob 被添加了两次,它在集合中也只会出现一次,因为集合中的每个元素都必须是唯一的。

判断元素是否存在

我们可以使用 contains() 方法来判断元素是否存在于集合当中:

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
System.out.println(sites.contains("Taobao"));
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">true</pre>

删除元素

我们可以使用 remove() 方法来删除集合中的元素:

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
sites.remove("Taobao"); // 删除元素,删除成功返回 true,否则为 false
System.out.println(sites);
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[Google, Runoob, Zhihu]</pre>

删除集合中所有元素可以使用 clear 方法:

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
sites.clear();
System.out.println(sites);
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[]</pre>

计算大小

如果要计算 HashSet 中的元素数量可以使用 size() 方法:

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
System.out.println(sites.size());
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">4</pre>

迭代 HashSet

可以使用 for-each 来迭代 HashSet 中的元素。

实例

// 引入 HashSet 类
import java.util.HashSet;

public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
for (String i : sites) {
System.out.println(i);
}
}
}

执行以上代码,输出结果如下:

<pre class="prettyprint prettyprinted" style="border-width: 1px 1px 1px 4px; border-style: solid; border-color: rgb(221, 221, 221); border-image: initial; margin: 15px auto; padding: 10px 15px; font: 400 12px / 20px Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background: url("/images/codecolorer_bg.gif") center top rgb(251, 251, 251); color: rgb(51, 51, 51); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Google Runoob Zhihu Taobao</pre>

相关文章

  • Java教程 第35章 HashSet

    HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。 HashSet 允许有 null ...

  • HashSet详解

    引用 java中HashSet详解 Java 编程下 HashSet 存入相同元素的原理 1.向HashSet 集...

  • HashSet的用法

    java集合——HashSet的用法 一、HashSet的构造 二、HashSet添加元素 三、遍历HashSet...

  • java中的hashset

    java 中的hashset

  • Java 语法学习笔记(四)

    [TOC] 参考教程 菜鸟教程 --- Java 教程 IBM --- Java 编程简介,第 2 部分 C语言中...

  • Java 语法学习笔记(三)

    [TOC] 参考教程 菜鸟教程 --- Java 教程 IBM --- Java 编程简介,第 2 部分 C语言中...

  • HashSet

    Java集合框架分析-HashSet

  • Java集合

    目录: Java集合APICollection 体系Set 集合HashSet 的存储机制:HashSet 还有一...

  • 2022-08-10

    import java.util.HashMap;import java.util.HashSet;import ...

  • 源码分析之HashSet

    概念 HashSet是Java Collections Framework中Set接口的一种实现了。HashSet...

网友评论

    本文标题:Java教程 第35章 HashSet

    本文链接:https://www.haomeiwen.com/subject/gjuavltx.html