本文介绍了定义:什么是HashSet的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashSet的
C#的HashSet的数据结构在.NET Framework 3.5中引入的。实施的成员的完整名单可在 HashSet的MSDN 页。


  1. 在哪里使用它?

  2. 您为什么要使用它?


解决方案


    1. 一个HashSet的持有对象集,但在某种程度上,它可以让你轻松,快速地确定对象是否已在设定与否。它由内部管理的阵列和使用从所述对象的哈希code计算的索引存储对象确实如此。


    2. HashSet的是包含独特元素的无序集合。它有标准的收集操作添加,删除,包含,但由于它采用了基于散列的实现,这些操作都是O(1)。 (而不是列出的例子,它是O(n)为contains和remove)的HashSet还提供了一套标准的操作,如集,交集和对称的区别。Take看看这里



  1. 有套不同的实现。一些使插入和查找操作超通过散列元素快。然而,这意味着,在其中加入的元素的顺序将丢失。其他实现preserve在较慢的运行时间成本增加的订单。


在C#中的HashSet的类无二的第一种方法,从而不可以 preserving元素的顺序。它比常规列表快得多。一些基本的基准测试表明,HashSet的是体面更快地主要类型(int,双,布尔等)的时候。与类对象时它是快了很多。所以这一点是HashSet的是快速的。

HashSet的唯一美中不足的是,有一个由指数的访问权限。要访问的元素,你可以使用一个枚举或者使用内置的功能,通过对HashSet的转换成一个列表和迭代。的

HashSetThe C# HashSet data structure was introduced in the .NET Framework 3.5. A full list of the implemented members can be found at the HashSet MSDN page.

  1. Where is it used?
  2. Why would you want to use it?
解决方案
    1. A HashSet holds a set of objects, but in a way that it allows you to easily and quickly determine whether an object is already in the set or not. It does so by internally managing an array and storing the object using an index which is calculated from the hashcode of the object. Take a look here

    2. HashSet is an unordered collection containing unique elements. It has the standard collection operations Add, Remove, Contains, but since it uses a hash-based implementation, these operation are O(1). (As opposed to List for example, which is O(n) for Contains and Remove.) HashSet also provides standard set operations such as union, intersection, and symmetric difference.Take a look here

  1. There are different implementations of Sets. Some make insertion and lookup operations super fast by hashing elements. However that means that the order in which the elements were added is lost. Other implementations preserve the added order at the cost of slower running times.

The HashSet class in C# goes for the first approach, thus not preserving the order of elements. It is much faster than a regular List. Some basic benchmarks showed that HashSet is decently faster when dealing with primary types (int, double, bool, etc.). It is a lot faster when working with class objects. So that point is that HashSet is fast.

The only catch of HashSet is that there is no access by indices. To access elements you can either use an enumerator or use the built-in function to convert the HashSet into a List and iterate through that.Take a look here

这篇关于定义:什么是HashSet的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:03