.NET 帮助

HashSet C#(它对开发人员的用途)

发布 2024年三月6日
分享:

简介

C# 编程非常灵活,提供了大量数据结构,可有效管理各种工作。其 哈希集合 就是这样一种强大的数据结构,它为基本操作提供了独特的组件和恒定时间平均复杂度。本篇文章将探讨 HashSet 在 C# 中的使用,以及如何将其与 IronPDF是一个功能强大的用于处理 PDF 文档的库。

如何使用 HashSet C#

1.创建一个新的控制台应用程序项目。

2.用 C# 创建一个 HashSet 对象。向 HashSet 添加默认值。

3.添加时,HashSet 会通过检查元素是否存在自动删除重复元素。

4.只逐个处理 HashSet 中的唯一元素。

5.显示结果并处理对象。

理解 C&num 中的 HashSet;

C# 中的 HashSet 旨在提供高性能的集合操作。当您需要保存一组不同的数据时,哈希集合是一个完美的集合,因为它可以防止元素重复。它包含在 System.Assortments.Hash 表中,是 C# 通用命名空间的基础,可提供快速插入、删除、快速检索和查找操作。我们还可以在 HashSet 元素中找到合适的子集。在 C# 中,使用 HashSet 集合操作方法,可以轻松执行标准的集合操作。HashSet 类提供了集合操作方法。

下面是 HashSet 在 C# 中的几种用法:

初始化和基本操作

建立哈希集合并执行基本操作,如追加、删除和验证条目的存在。

// set operations 
HashSet<int> numbers = new HashSet<int>();
// Add elements
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// Remove an element, predicate function
numbers.Remove(2);
// Check for membership or duplicate element
bool containsThree = numbers.Contains(3);
// set operations 
HashSet<int> numbers = new HashSet<int>();
// Add elements
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// Remove an element, predicate function
numbers.Remove(2);
// Check for membership or duplicate element
bool containsThree = numbers.Contains(3);
' set operations 
Dim numbers As New HashSet(Of Integer)()
' Add elements
numbers.Add(1)
numbers.Add(2)
numbers.Add(3)
' Remove an element, predicate function
numbers.Remove(2)
' Check for membership or duplicate element
Dim containsThree As Boolean = numbers.Contains(3)
VB   C#

使用集合进行初始化

使用现有集合作为 HashSet 的起点,可以立即消除重复。

List<int> duplicateNumbers = new List<int> { 1, 2, 2, 3, 3, 4 }; // all the elements
HashSet<int> uniqueNumbers = new HashSet<int>(duplicateNumbers);
List<int> duplicateNumbers = new List<int> { 1, 2, 2, 3, 3, 4 }; // all the elements
HashSet<int> uniqueNumbers = new HashSet<int>(duplicateNumbers);
Dim duplicateNumbers As New List(Of Integer) From {1, 2, 2, 3, 3, 4} ' all the elements
Dim uniqueNumbers As New HashSet(Of Integer)(duplicateNumbers)
VB   C#

与另一个哈希集合的联合

使用 UnionWith 函数合并两个 HashSet 实例,生成一个新的集合,该集合结合了两个集合中的不同项。

HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.UnionWith(set2); // set1 now contains { 1, 2, 3, 4, 5 }
HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.UnionWith(set2); // set1 now contains { 1, 2, 3, 4, 5 }
Dim set1 As New HashSet(Of Integer) From {1, 2, 3}
Dim set2 As New HashSet(Of Integer) From {3, 4, 5}
set1.UnionWith(set2) ' set1 now contains { 1, 2, 3, 4, 5 }
VB   C#

与另一个哈希集合的交集

使用 IntersectWith 函数,确定两个 HashSet 实例之间的共享组件。

HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.IntersectWith(set2); // set1 now contains { 3 }
HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.IntersectWith(set2); // set1 now contains { 3 }
Dim set1 As New HashSet(Of Integer) From {1, 2, 3}
Dim set2 As New HashSet(Of Integer) From {3, 4, 5}
set1.IntersectWith(set2) ' set1 now contains { 3 }
VB   C#

与另一个哈希集合的差异

使用 ExceptWith 函数,查找在一个 HashSet 中但不在另一个 HashSet 中的元素。

HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.ExceptWith(set2); // set1 now contains { 1, 2 }
HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.ExceptWith(set2); // set1 now contains { 1, 2 }
Dim set1 As New HashSet(Of Integer) From {1, 2, 3}
Dim set2 As New HashSet(Of Integer) From {3, 4, 5}
set1.ExceptWith(set2) ' set1 now contains { 1, 2 }
VB   C#

检查子集或超集:

使用 IsSubsetOf 和 IsSupersetOf 方法,可以确定给定的 HashSet 实例是另一个实例的子集还是超集。

HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 2, 3 };
bool isSubset = set2.IsSubsetOf(set1); // returns true
bool isSuperset = set1.IsSupersetOf(set2); // returns true
HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 2, 3 };
bool isSubset = set2.IsSubsetOf(set1); // returns true
bool isSuperset = set1.IsSupersetOf(set2); // returns true
Dim set1 As New HashSet(Of Integer) From {1, 2, 3}
Dim set2 As New HashSet(Of Integer) From {2, 3}
Dim isSubset As Boolean = set2.IsSubsetOf(set1) ' returns true
Dim isSuperset As Boolean = set1.IsSupersetOf(set2) ' returns true
VB   C#

对称差分

使用 SymmetricExceptWith 技术确定对称差值 (元素在一组中存在,但在两组中都不存在).

HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.SymmetricExceptWith(set2); // set1 now contains { 1, 2, 4, 5 }
HashSet<int> set1 = new HashSet<int> { 1, 2, 3 };
HashSet<int> set2 = new HashSet<int> { 3, 4, 5 };
set1.SymmetricExceptWith(set2); // set1 now contains { 1, 2, 4, 5 }
Dim set1 As New HashSet(Of Integer) From {1, 2, 3}
Dim set2 As New HashSet(Of Integer) From {3, 4, 5}
set1.SymmetricExceptWith(set2) ' set1 now contains { 1, 2, 4, 5 }
VB   C#

IronPDF

程序员可以使用 C# 语言制作、编辑和修改 PDF 文档。 IronPDF .NET PDF 库。该应用程序提供多种工具和功能,可对 PDF 文件进行不同的操作,包括从 HTML 创建新的 PDF、将 HTML 转换为 PDF、合并或分割 PDF 文档,以及用文本、照片和其他数据对现有 PDF 进行注释。要了解有关 IronPDF 的更多信息,请参阅此页 文献资料.

IronPDF 的功能

  • HTML 到 PDF 的转换:任何类型的 HTML 数据,包括文件、URL 和 HTML 代码字符串,都可以通过 IronPDF 转换为 PDF 文档。
  • PDF 生成:可使用 C# 编程语言以编程方式将文本、图像和其他对象添加到 PDF 文档中。
  • PDF 操作:IronPDF 可以将 PDF 文件分割成多个文件,并编辑预先存在的 PDF 文件。它还能将多个 PDF 文件合并为一个文件。
  • PDF 表单:由于该库可让用户创建和完成 PDF 表单,因此在需要收集和处理表单数据的情况下非常有用。

  • 安全功能:IronPDF 支持 PDF 文档加密以及密码和权限安全。

安装 IronPDF

获取 IronPDF 库;即将发布的补丁需要它。为此,请在软件包管理器中输入以下代码:

Install-Package IronPDF 
//or 
dotnet add package IronPdf

HashSet C#(开发人员如何使用):图 1 - 使用软件包管理器控制台并输入以下命令安装 IronPDF 库:"Install-Package IronPDF" 或 "dotnet add package IronPdf"。

另一种方法是使用 NuGet 软件包管理器查找软件包 "IronPDF"。我们可以从与 IronPDF 相关的所有 NuGet 软件包列表中选择并下载所需的软件包。

HashSet C#(如何为开发人员工作):图 2 - 您可以使用 NuGet 软件包管理器安装 IronPDF 库。在 浏览 选项卡中搜索软件包 "ironpdf",然后选择并安装最新版本的 IronPDF。

使用 IronPDF 的哈希集

在 C# 环境中,IronPDF 是一个功能强大的库,它能让 PDF 文档的处理变得更简单。在对独特的数据表示和有效的文档创建至关重要的情况下,将 HashSet 的效率与 IronPDF 的文档操作能力相结合,可能会产生创造性的解决方案。

在 IronPDF 中使用 HashSet 的优势

  • 减少数据冗余:通过确保只保留不同的元素,哈希集有助于避免数据重复。在处理庞大的数据集以删除重复信息时,这一点非常有用。
  • 有效查找:使用 HashSet 可以以恒时平均复杂度执行插入、删除和查找等基本操作。在处理不同大小的数据集时,这种性能非常重要。

  • 简化文档制作:IronPDF 简化了 C# PDF 文档创建过程。通过将 HashSet 与 IronPDF 集成,您可以建立快速有效的流程,为您的 PDF 创建原创的动态内容。

现在,让我们来看看在现实世界中使用 HashSet 和 IronPDF 可能会非常有用的场景。

生成包含唯一数据的 PDF

using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
class PdfGenerator
{
    static void Main()
    {
        // Sample user names
        string [] userNames = { "Alice", "Bob", "Charlie", "Bob", "David", "Alice" };
        // Using HashSet to store unique user names
        HashSet<string> uniqueUserNames = new HashSet<string>(userNames);
        // Generating PDF with unique user names
        GeneratePdf(uniqueUserNames);
    }
    static void GeneratePdf(HashSet<string> uniqueUserNames)
    {
        // Create a new PDF document using IronPDF
        IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
        PdfDocument pdf = renderer.RenderHtmlAsPdf(BuildHtmlDocument(uniqueUserNames));
        // Save the PDF to a file
        string pdfFilePath = "UniqueUserNames.pdf";
        pdf.SaveAs(pdfFilePath);
        // Display a message with the file path
        Console.WriteLine($"PDF generated successfully. File saved at: {pdfFilePath}");
    }
    static string BuildHtmlDocument(HashSet<string> uniqueUserNames)
    {
        // Build an HTML document with unique user names
        string htmlDocument = "<html><body><ul>";
        foreach (var userName in uniqueUserNames)
        {
            htmlDocument += $"<li>{userName}</li>";
        }
        htmlDocument += "</ul></body></html>";
        return htmlDocument;
    }
}
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
class PdfGenerator
{
    static void Main()
    {
        // Sample user names
        string [] userNames = { "Alice", "Bob", "Charlie", "Bob", "David", "Alice" };
        // Using HashSet to store unique user names
        HashSet<string> uniqueUserNames = new HashSet<string>(userNames);
        // Generating PDF with unique user names
        GeneratePdf(uniqueUserNames);
    }
    static void GeneratePdf(HashSet<string> uniqueUserNames)
    {
        // Create a new PDF document using IronPDF
        IronPdf.HtmlToPdf renderer = new IronPdf.HtmlToPdf();
        PdfDocument pdf = renderer.RenderHtmlAsPdf(BuildHtmlDocument(uniqueUserNames));
        // Save the PDF to a file
        string pdfFilePath = "UniqueUserNames.pdf";
        pdf.SaveAs(pdfFilePath);
        // Display a message with the file path
        Console.WriteLine($"PDF generated successfully. File saved at: {pdfFilePath}");
    }
    static string BuildHtmlDocument(HashSet<string> uniqueUserNames)
    {
        // Build an HTML document with unique user names
        string htmlDocument = "<html><body><ul>";
        foreach (var userName in uniqueUserNames)
        {
            htmlDocument += $"<li>{userName}</li>";
        }
        htmlDocument += "</ul></body></html>";
        return htmlDocument;
    }
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO
Friend Class PdfGenerator
	Shared Sub Main()
		' Sample user names
		Dim userNames() As String = { "Alice", "Bob", "Charlie", "Bob", "David", "Alice" }
		' Using HashSet to store unique user names
		Dim uniqueUserNames As New HashSet(Of String)(userNames)
		' Generating PDF with unique user names
		GeneratePdf(uniqueUserNames)
	End Sub
	Private Shared Sub GeneratePdf(ByVal uniqueUserNames As HashSet(Of String))
		' Create a new PDF document using IronPDF
		Dim renderer As New IronPdf.HtmlToPdf()
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(BuildHtmlDocument(uniqueUserNames))
		' Save the PDF to a file
		Dim pdfFilePath As String = "UniqueUserNames.pdf"
		pdf.SaveAs(pdfFilePath)
		' Display a message with the file path
		Console.WriteLine($"PDF generated successfully. File saved at: {pdfFilePath}")
	End Sub
	Private Shared Function BuildHtmlDocument(ByVal uniqueUserNames As HashSet(Of String)) As String
		' Build an HTML document with unique user names
		Dim htmlDocument As String = "<html><body><ul>"
		For Each userName In uniqueUserNames
			htmlDocument &= $"<li>{userName}</li>"
		Next userName
		htmlDocument &= "</ul></body></html>"
		Return htmlDocument
	End Function
End Class
VB   C#

在上述代码示例中,我们使用 HashSet uniqueUserNames 保存从数组中获取的唯一用户名。哈希集会自动消除重复。接下来,我们使用 IronPDF 在 PDF 文档中创建这些唯一用户名的无序列表。要了解更多代码信息,请查看 这里.

输出端

HashSet C#(如何为开发人员工作):图 3 - 输出:UniqueUserNames.pdf

结论

总而言之,C# HashSet 数据结构是组织不同项目集的有效工具。当与 IronPDF 搭配使用时,它为动态、独一无二和性能优化的 PDF 文档创建创造了新的机会。

我们展示了如何使用 HashSet 来保证数据的唯一性,同时使用 IronPDF 的例子中创建 PDF 文档。无论是重复数据删除、报告还是管理动态内容,构建强大而有效的 C# 应用程序都可以从 HashSet 和 IronPDF 的组合中受益。在您进行更多研究时,请考虑到您可能会在许多情况下利用这种组合来提高应用程序的可用性和功能。

$749 Lite 版本的 IronPDF 附带永久许可证、升级选项和一年的软件支持。整个水印 试用期 了解有关 IronPDF 价格、许可和免费试用的更多信息。请访问 页码 了解有关 Iron 软件的更多信息。

< 前一页
C# Nameof(它如何为开发人员工作)
下一步 >
C# 数组长度(开发人员使用方法)

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >