Some tips on using HashSet<T> and List<T>
This article is written based on my colleague's review
Most of the times, when I want to use a collection to store data, I usually Use List<T> type, becuase it supports generic type and can be used in many cases
However, one guy in my team point out that sometimes HashSet<T> is better than List<T>
Here is the scenario:
1) collection store Guid type data, no duplicate data
2) collection is used to check whether a Guid exists in this collection
So Using HashSet<T>(Complexity is O(1)) will cost less than List<T>(Complexity is O(n))
see the simplified code changes:
Before:
static bool HasData(Guid guid)
{
List<Guid> listGuid = new List<Guid>();
listGuid.Add(Guid.NewGuid());
listGuid.Add(Guid.NewGuid()); return listGuid.Contains(guid);
}
After:
static bool HasData(Guid guid)
{
HashSet<Guid> listGuid = new HashSet<Guid>();
listGuid.Add(Guid.NewGuid());
listGuid.Add(Guid.NewGuid()); return listGuid.Contains(guid);
}
This is only a bit change, but is better to do for performance improve. So I write it down to summarise and remind me be carefull, even use some basic types
Below is a link about difference between HashSet and List
http://stackoverflow.com/questions/6391738/what-is-the-difference-between-hashsett-and-listt
Some tips on using HashSet<T> and List<T>的更多相关文章
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- 阅读《Effective Java》每条tips的理解和总结(1)
<Effective Java>这本书的结构是90来条tips,有长有短,每条tip都值的学习.这里根据对书中每条tip的理解做简短的总结,方便日后回顾.持续更新~ 1. 考虑用静态方法代 ...
- HashSet HashTable 与 TreeSet
HashSet<T>类 HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet& ...
- Mac上MySQL忘记root密码且没有权限的处理办法&workbench的一些tips (转)
忘记Root密码肿么办 Mac上安装MySQL就不多说了,去mysql的官网上下载最新的mysql包以及workbench,先安装哪个影响都不大.如果你是第一次安装,在mysql安装完成之后,会弹出来 ...
- Set容器--HashSet集合
Set容器特点: ① Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ② 最常用的两个Set接口的实 ...
- 计算机程序的思维逻辑 (41) - 剖析HashSet
上节介绍了HashMap,提到了Set接口,Map接口的两个方法keySet和entrySet返回的都是Set,本节,我们来看Set接口的一个重要实现类HashSet. 与HashMap类似,字面上看 ...
- Bloom Filter:海量数据的HashSet
Bloom Filter一般用于数据的去重计算,近似于HashSet的功能:但是不同于Bitmap(用于精确计算),其为一种估算的数据结构,存在误判(false positive)的情况. 1. 基本 ...
- 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇
[Tips]史上最全H1B问题合辑——保持H1B身份终级篇 2015-04-10留学小助手留学小助手 留学小助手 微信号 liuxue_xiaozhushou 功能介绍 提供最真实全面的留学干货,帮您 ...
- HashSet,TreeSet和LinkedHashSet的区别
Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...
随机推荐
- cefSharp在XP下使得程序崩溃记录
前言:这是一个奇葩的问题,到现在自己还没有搞明白问题出现在哪里,但是从问题总算是解决了,希望看到此文章的大牛,如果知道问题出在什么地方,可以告知一下. [一个在XP系统下面应用程序崩溃问题] 资源: ...
- 【转】requirejs简单入门
博主今天正式工作啦,工作中用到了js模块化技术,这里转来一个入门教程,很易懂,转给同样刚入门的你们~~ 原地址:http://www.ruanyifeng.com/blog/2012/11/requi ...
- HTML5新增属性
[sourcecode language="plain"] <!DOCTYPE html> <html manifest="cache.manifest ...
- _margin和margin的区别
_margin和margin的区别 _margin和margin的区别 Question: margin:15px 300px 0px 100px; height:72px; width:188px; ...
- windows端口被占用
查看端口号被占用进程netstat -a -n -o 强制结束PIDtaskkill /pid:604 /F
- php 解压 .gz 文件
在百度搜索到的 PharData 类和 ZipArchive 都是解压不了 .gz 的文件的,后来在 google 搜索到解决方法,问题解决. try { $phar = new PharData($ ...
- mysql 的 GROUP_CONCAT
GROUP_CONCAT 通常跟 group by 一起用,但也可以不用.例:select GROUP_CONCAT(pct_id) as pct_ids from (select max(pct_i ...
- 在Spring MVC中使用注解的方式校验RequestParams
概述 Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestPa ...
- poj 3299 Humidex
直接套公式就可以,可我套公式第一遍都错了,英语差的孩子伤不起(┬_┬) #include <iostream> #include <cmath> #include <io ...
- Cocos2d-JS v3.0 alpha
Cocos2d-JS是整合了Cocos2d-html5 v3.0 alpha和Cocos2d-x JSBinding的新JS引擎仓库.整合之后的核心优势在于Html5和JSB的开发流程及API现在变得 ...