ConcurrentDictionary数据结构的使用方法
2013-01-14 15:32
92 查看
ConcurrentDictionary数据结构是4.0之后才加进去的,次数据结构最大的有点就是线程安全的
编码实现using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections.Concurrent; namespace 字典排序测试 { public partial class Form1 : Form { ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>(); public Form1() { InitializeComponent(); dic.TryAdd(1, 9); dic.TryAdd(2, 2); dic.TryAdd(3, 3); dic.TryAdd(4, 4); dic.TryAdd(5, 5); dic.TryAdd(6, 6); } private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int j = 0; //foreach (KeyValuePair<int, int> kvp in dic) //{ // listBox1.Items.Add(j.ToString()+": "+kvp.Key + "," + kvp.Value); // j++; //} for (int i = 0; i < dic.Count; i++) { var item = dic.ElementAt(i); listBox1.Items.Add(j.ToString() + ": " + item.Key + "," + item.Value); j++; } } private void button2_Click(object sender, EventArgs e) { int i=0; if (dic.TryRemove(Convert.ToInt32(textBox1.Text), out i)) { MessageBox.Show("remove ok"); } listBox1.Items.Clear(); button1_Click(null, null); } private void button3_Click(object sender, EventArgs e) { if (dic.TryAdd(Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text))) { MessageBox.Show("TryAdd ok"); } listBox1.Items.Clear(); button1_Click(null, null); } private void button4_Click(object sender, EventArgs e) { Array arr = dic.ToArray(); //1.按照X排序:OrderBy升序,OrderByDescending降序 var temp=dic.OrderBy((kv => kv.Value)); //.取值Max,Min,last最后一个元素,sum,take从序列的开头返回指定数量的连续元素, //其他参考 http://msdn.microsoft.com/zh-cn/library/dd287191.aspx int max = dic.Max(kv => kv.Value); // List<int> l = temp.Select(kv => kv.Key).ToList(); listBox1.Items.Clear(); foreach (var item in l) listBox1.Items.Add(item.ToString()); } } }
相关文章推荐
- Java数据结构之递归与三角函数的运用,使用3种方法实现三角
- Python中list的append方法添加,和使用下标取得。之中的元素还可以是其他的数据结构
- C语言数据结构中定位函数Index的使用方法
- python3 一些经常使用的数据结构和内置方法。
- JavaScript数据结构之双向链表定义与使用方法示例
- UUID数据结构和使用方法
- 数据结构--remove方法对LinkedList类的使用注意
- Python数据结构之哈夫曼树定义与使用方法示例
- 慕课网----大话PHP设计模式 二(数据结构的php实现,链式操作,php魔方方法的使用)
- 使用qt dbus 传递复杂数据结构的方法
- Python数据结构之双向链表的定义与使用方法示例
- Java LinkedList特有方法程序小解 && 使用LinkedList 模拟一个堆栈或者队列数据结构。
- 数据结构教材中hash的使用及一些基本概念和方法
- Java数据结构之队列的简单定义与使用方法
- 算法设计与分析——第二篇,论算法与数据结构的使用方法及技巧
- 剪切(使用File类的renameTo 方法和递归实现非空目录的剪切)
- 迅雷 非vip用户使用vip功能的设置方法
- 转载 Android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- MacBook使用技巧:苹果笔记本的PrintScreen截屏快捷键使用方法
- 使用CompilerResults 方法动态的编译类