C# 各种集合
大多数集合都在 System.Collections,System.Collections.Generic两个命名空间。
其中System.Collections.Generic专门用于泛型集合。
针对特定类型的集合类型位于System.Collections.Specialized;命名空间;
线程安全的集合类位于System.Collections.Concurrent;命名空间。
下面是集合和列表实现的接口如下:
一、列表
[Serializable]
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))] [DebuggerDisplay("Count = {Count}")] public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
从这个可以看出,泛型集合List<T>实现了这么多接口,具体接口的信息可以通过工具查看。
二、队列
队列先进先出,一头进一头出,用Queue<T>实现
[Serializable]
[DebuggerTypeProxy(typeof(System_QueueDebugView<>))]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
可以看出队列实现了集合的接口,迭代的接口
using System;
using System.Collections.Generic; namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
Queue<String> queue = new Queue<string>();
//进队
queue.Enqueue("张三");
queue.Enqueue("李四");
queue.Enqueue("王五");
queue.Enqueue("田六");
queue.Enqueue("赵七"); foreach (String item in queue)
{
Console.WriteLine("foreach迭代:" + item);
} //出队
while (queue.Count > )
{
Console.WriteLine("出队:" + queue.Dequeue());
} Console.Read();
}
}
}
三、栈
栈:从同一边先进后出,用Stack<T>实现
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(System_StackDebugView<>))]
[ComVisible(false)]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
栈也是实现了集合接口与迭代接口的
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Stack<String> stack = new Stack<string>();
- //进栈
- stack.Push("张三");
- stack.Push("李四");
- stack.Push("王五");
- stack.Push("田六");
- stack.Push("赵七");
- foreach (String item in stack)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- //出栈
- while (stack.Count > 0)
- {
- Console.WriteLine("出栈:" + stack.Pop());
- }
- Console.Read();
- }
- }
- }
四、链表
LinkedList是一个双向链表,链表有个有点,就是在链表中间插入、删除元素很快,但是查找中间与末尾的元素很慢,需要一个节点一个节点的去找。
- [Serializable]
- [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
由此可见链表也是有集合的特性的,可以迭代,同时还有链表的特性
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- LinkedList<String> lList = new LinkedList<string>();
- LinkedListNode<String> node = new LinkedListNode<string>("root");
- lList.AddFirst(node);
- node = lList.AddAfter(node, "张三");
- node = lList.AddAfter(node, "李四");
- node = lList.AddAfter(node, "王五");
- node = lList.AddAfter(node, "田六");
- node = lList.AddAfter(node, "赵七");
- foreach (String item in lList)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- node = lList.First;
- Console.WriteLine("第一个元素:" + node.Value);
- node = lList.Last;
- Console.WriteLine("最后一个元素:" + node.Value);
- Console.Read();
- }
- }
- }
五、有序列表
SortedList采用键-值对存储,键不能重复,并且会根据key进行排序
- [Serializable]
- [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
可以看出SortedList不仅具有字典的特性,还有集合,迭代的功能
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一,如果不唯一可以考虑Lookup<TKey,TElement>
- SortedList<int, String> sList = new SortedList<int, string>();
- sList.Add(100, "张三");
- sList.Add(21, "李四");
- sList.Add(13, "王五");
- sList.Add(44, "田六");
- sList.Add(35, "赵七");
- foreach (KeyValuePair<int, String> item in sList)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
六、字典
字典是很复杂的数据结构,允许通过key来查找值,字典可以自由添加、删除元素,没有集合由于移动元素导致的开销。
- [Serializable]
- [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
可以看出字典也具有集合的特性,可以迭代
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一
- Dictionary<int, String> dict = new Dictionary<int, string>();
- dict.Add(11, "张三");
- dict.Add(1, "李四");
- dict.Add(2, "王五");
- dict.Add(16, "田六");
- dict.Add(12, "赵七");
- foreach (KeyValuePair<int, String> item in dict)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
说到字典,顺便谈一下有序字典,与有序列表对应;SortedDictionary,SortedList,SortedSet
会根据Key进行排序
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一
- SortedDictionary<int, String> dict = new SortedDictionary<int, string>();
- dict.Add(11, "张三");
- dict.Add(1, "李四");
- dict.Add(2, "王五");
- dict.Add(16, "田六");
- dict.Add(12, "赵七");
- foreach (KeyValuePair<int, String> item in dict)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
七、集
集(Set):包含不重复元素,常用HashSet,SortedSet
- [Serializable]
- [DebuggerDisplay("Count = {Count}")]
- [DebuggerTypeProxy(typeof(HashSetDebugView<>))]
- public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
- [Serializable]
- [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]
- [DebuggerDisplay("Count = {Count}")]
- public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- HashSet<String> hSet = new HashSet<string>();
- hSet.Add("张三");
- hSet.Add("李四");
- hSet.Add("王五");
- hSet.Add("田六");
- hSet.Add("赵七");
- foreach (String item in hSet)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- Console.Read();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- SortedSet<String> hSet = new SortedSet<string>();
- hSet.Add("张三");
- hSet.Add("李四");
- hSet.Add("王五");
- hSet.Add("田六");
- hSet.Add("赵七");
- foreach (String item in hSet)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- Console.Read();
- }
- }
- }
性能比较:
出自:http://blog.csdn.net/ceclar123/article/details/8655853
C# 各种集合的更多相关文章
- java基础集合经典训练题
第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...
- .Net多线程编程—并发集合
并发集合 1 为什么使用并发集合? 原因主要有以下几点: System.Collections和System.Collections.Generic名称空间中所提供的经典列表.集合和数组都不是线程安全 ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- 编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)
建议75:集合中的元素必须做到compareTo和equals同步 实现了Comparable接口的元素就可以排序,compareTo方法是Comparable接口要求必须实现的,它与equals方法 ...
- java基础_集合List与Set接口
List接口继承了Collection的方法 当然也有自己特有的方法向指定位置添加元素 add(索引,添加的元素); 移除指定索引的元素 remove(索引) 修改指定索引的元素 set ...
- Java基础Collection集合
1.Collection是所有集合的父类,在JDK1.5之后又加入了Iterable超级类(可以不用了解) 2.学习集合从Collection开始,所有集合都继承了他的方法 集合结构如图:
- 轻量级“集合”迭代器-Generator
Generator是PHP 5.5加入的新语言特性.但是,它似乎并没有被很多PHP开发者广泛采用.因此,在我们了解PHP 7对Generator的改进之前,我们先通过一个简单却显而易见的例子来了解下G ...
- Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合
今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...
- 这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)
在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...
- python 数据类型 --- 集合
1. 注意列表和集合的区别 set 列表表现形式: list_1 = [1,3,4]; 集合表现形式:set_1= set() list_1 = [1,2,3,4,23,4,2] print(lis ...
随机推荐
- Java中getAttribute getParameter 区别
网上说的不少,发现都是同一篇,汗..... (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组 ...
- The Glorious Karlutka River =)
sgu438:http://acm.sgu.ru/problem.php?contest=0&problem=438 题意:有一条东西向流淌的河,宽为 W,河中有 N 块石头,每块石头的坐标( ...
- 删除数据表和清空数据表的内容(保存表结构)的SHELL脚本
A,删除指定数据库的所有数据表 #!/bin/bash # 删除mysql中所有表 # 示例: # Usage: ./script user password dbnane # Usage: ./sc ...
- Hibernate 缓存 关于注解方式
要引入 import org.hibernate.annotations.Cache; 在类前面添加: @Cache(usage= CacheConcurrencyStrategy.NONSTRICT ...
- h.264并行解码算法2D-Wave实现(基于多核共享内存系统)
cache-coherent shared-memory system 我们最平常使用的很多x86.arm芯片都属于多核共享内存系统,这种系统表现为多个核心能直接对同一内存进行读写访问.尽管内存的存取 ...
- lc面试准备:Partition List
1 题目 Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- SparkSQL配置和使用初探
1.环境 OS:Red Hat Enterprise Linux Server release 6.4 (Santiago) Hadoop:Hadoop 2.4.1 Hive:0.11.0 JDK:1 ...
- OpenWrt挂载USB储存设备实现Samba共享
没有USB接口的路由器不是好路由器,有了USB接口OpenWrt才有更多的玩法,比如挂载U盘.移动硬盘等USB储存设备实现Samba共享,打造小型家庭服务器. 1.安装与USB相关的软件包: opkg ...
- 【转】Android ROM研究---Android build system增加模块
原文网址:http://hualang.iteye.com/blog/1141315 Android build system就是编译系统的意思 在我们需要向自己编译的源代码中增加模块的时候,需要一些 ...
- 登录MD5加盐处理
一:解决方案资源管理器截图: 二:operatorDAL.cs代码 using System; using System.Collections.Generic; using System.Linq; ...