c#扩展出MapReduce方法

简介: MapReduce方法主体: 1 public static IDictionary MapReduce(this IList inputList, 2 Func map, Func reduce) 3 { 4 o...

MapReduce方法主体:

 1 public static IDictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(this IList<TInput> inputList,
 2             Func<MapReduceData<TInput>, KeyValueClass<TKey, TValue>> map, Func<TKey, IList<TValue>, TResult> reduce)
 3         {
 4             object locker = new object();
 5             ConcurrentDictionary<TKey, TResult> result = new ConcurrentDictionary<TKey, TResult>();
 6             //保存map出来的结果
 7             ConcurrentDictionary<TKey, IList<TValue>> mapDic = new ConcurrentDictionary<TKey, IList<TValue>>();
 8             var parallelOptions = new ParallelOptions();
 9             parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount;
10             //并行map
11             Parallel.For(0, inputList.Count(), parallelOptions, t =>
12             {
13                 MapReduceData<TInput> data = new MapReduceData<TInput>
14                 {
15                     Data = inputList[t],
16                     Index = t,
17                     List = inputList,
18                 };
19                 var pair = map(data);
20                 if (pair != null && pair.Valid)
21                 {
22                     //锁住防止并发操作list造成数据缺失
23                     lock (locker)
24                     {
25                         //将匹配出来的结果加入结果集放入字典
26                         IList<TValue> list = null;
27                         if (mapDic.ContainsKey(pair.Key))
28                         {
29                             list = mapDic[pair.Key];
30                         }
31                         else
32                         {
33                             list = new List<TValue>();
34                             mapDic[pair.Key] = list;
35                         }
36                         list.Add(pair.Value);
37                     }
38                 }
39             });
40 
41             //并行reduce
42             Parallel.For(0, mapDic.Keys.Count, parallelOptions, t =>
43             {
44                 KeyValuePair<TKey, IList<TValue>> pair = mapDic.ElementAt(t);
45                 result[pair.Key] = reduce(pair.Key, pair.Value);
46             });
47             return result;
48         }
View Code

KeyValueClass定义:

 1 public class KeyValueClass<K, V>
 2     {
 3         public KeyValueClass(K key, V value)
 4         {
 5             Key = key;
 6             Value = value;
 7         }
 8 
 9         public KeyValueClass()
10         {
11 
12         }
13 
14         public K Key { get; set; }
15 
16         public V Value { get; set; }
17     }
View Code

Console测试:

 1 List<TestClass> listTestClass = new List<TestClass>();
 2             listTestClass.Add(new TestClass { a = "a", g = 1 });
 3             listTestClass.Add(new TestClass { a = "b", g = 3 });
 4             listTestClass.Add(new TestClass { a = "c", g = 4 });
 5             listTestClass.Add(new TestClass { a = "d", g = 2 });
 6             listTestClass.Add(new TestClass { a = "e", g = 1 });
 7             listTestClass.Add(new TestClass { a = "f", g = 2 });
 8             listTestClass.Add(new TestClass { a = "g", g = 5 });
 9             listTestClass.Add(new TestClass { a = "h", g = 6 });
10             IDictionary<int, string> dic = listTestClass.MapReduce(t =>
11             {
12                 if (t.g < 5)
13                 {
14                     return new KeyValueClass<int, string>(t.g, t.a);
15                 }
16                 return null;
17             }, (key, values) =>
18            {
19                return string.Join(",", values);
20            });
View Code

TestClass定义:

 1 public class TestClass
 2     {
 3         public string a { get; set; }
 4         public string b { get; set; }
 5 
 6         public string d { get; set; }
 7 
 8         //public DateTime f { get; set; }
 9 
10         public int g { get; set; }
11 
12         public List<TestClass> test { get; set; }
13 
14         public Dictionary<string, string> dic { get; set; }
15     }
View Code

结果:

1:a,e

2:d,f

3:b

4:c

 

词频性能测试

相关文章
|
4月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
4月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
3月前
|
数据采集 数据可视化 测试技术
C#生成Selenium测试报告:实用方法与技巧
在C#中使用Selenium进行自动化测试时,结合代理IP和ExtentReports能增强测试安全性和报告质量。安装必备工具如Selenium WebDriver、NUnit和ExtentReports。在测试设置中,配置代理(如亿牛云爬虫代理)以隐藏IP,通过ChromeOptions定制UserAgent,并添加Cookie。测试代码示例展示了如何打开网页、执行搜索并生成详细的测试报告。使用ExtentReports可创建可视化测试结果,便于团队分析。
C#生成Selenium测试报告:实用方法与技巧
|
11天前
|
C#
C#一分钟浅谈:Lambda 表达式和匿名方法
本文详细介绍了C#编程中的Lambda表达式与匿名方法,两者均可用于定义无名函数,使代码更简洁易维护。文章通过基础概念讲解和示例对比,展示了各自语法特点,如Lambda表达式的`(parameters) =&gt; expression`形式及匿名方法的`delegate(parameters)`结构。并通过实例演示了两者的应用差异,强调了在使用Lambda时应注意闭包问题及其解决策略,推荐优先使用Lambda表达式以增强代码可读性。
19 8
|
19天前
|
图形学 C# 开发者
全面掌握Unity游戏开发核心技术:C#脚本编程从入门到精通——详解生命周期方法、事件处理与面向对象设计,助你打造高效稳定的互动娱乐体验
【8月更文挑战第31天】Unity 是一款强大的游戏开发平台,支持多种编程语言,其中 C# 最为常用。本文介绍 C# 在 Unity 中的应用,涵盖脚本生命周期、常用函数、事件处理及面向对象编程等核心概念。通过具体示例,展示如何编写有效的 C# 脚本,包括 Start、Update 和 LateUpdate 等生命周期方法,以及碰撞检测和类继承等高级技巧,帮助开发者掌握 Unity 脚本编程基础,提升游戏开发效率。
32 0
|
1月前
|
C#
C# async await 异步执行方法
C# async await 异步执行方法
37 0
|
1月前
|
C# 图形学
小功能⭐️C#控制小数点后位数的方法
小功能⭐️C#控制小数点后位数的方法
|
1月前
|
C#
WPF/C#:数据绑定到方法
WPF/C#:数据绑定到方法
29 0
|
3月前
|
C#
技术经验分享:C#DUID的用法及取得整数的几个方法
技术经验分享:C#DUID的用法及取得整数的几个方法
56 1
|
3月前
|
C#
蓝易云 - C#将异步改成同步方法
注意:虽然这样可以将异步方法转为同步,但在实际开发中,我们通常推荐使用异步方法,因为它可以提高应用程序的响应性和并发性。将异步方法转为同步可能会导致死锁或性能问题。
28 2