C# SortedDictionary排序结构测试
2018-01-19 17:11
218 查看
C# SortedDictionary-Dictionary,SortedList-List 这是两种不一样的数据结构,但是他们比较相似。Sorted*意为排序的,这里以SortedDictionary为例进行测试。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AVParser.Data
{
/// <summary>
/// GB1078音视频帮助类
/// </summary>
public class GB1078AVHelper
{
/// <summary>
/// 用于存储解析后的AV数据
/// </summary>
SortedDictionary<ulong, AV> sortedDictionary = new SortedDictionary<ulong, AV>();
/// <summary>
/// 设置AV数据
/// </summary>
/// <param name="av"></param>
public void SetData(AV av)
{
sortedDictionary[av.Head.RTTimeSpan] = av;
}
/// <summary>
/// 获取最大时间戳的AV
/// </summary>
/// <returns></returns>
public AV GetMaxAV()
{
ulong maxKey = sortedDictionary.Keys.Max();
AV av = sortedDictionary[maxKey];
// 取走即清除
sortedDictionary.Remove(maxKey);
return av;
}
/// <summary>
/// 获取最小时间戳的AV
/// </summary>
/// <returns></returns>
public AV GetMinAV()
{
ulong minKey = sortedDictionary.Keys.Min();
AV av = sortedDictionary[minKey];
// 取走即清除
sortedDictionary.Remove(minKey);
return av;
}
}
}
using FFmpeg.AutoGen;
using JX;
using RTForwardServer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AVParser
{
static class Program
{
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Data.GB1078AVHelper helper = new Data.GB1078AVHelper();
for(var i=10; i >= 1; i--)
{
JTRTHead head = new JTRTHead();
head.RTTimeSpan =(ulong) i;
AV av = new AV(AV.AVTYPE.MEDIA_AUDIO, head, new byte[0]);
helper.SetData(av);
}
for (var i = 10; i >= 1; i--)
{
Console.WriteLine(helper.GetMinAV().Head.RTTimeSpan);
}
Console.ReadLine();
}
}
}
2
3
4
5
6
7
8
9
10
注意事项:C#里面Dictionary的Add键不允许重复,会产生异常。
SortedDictionary
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AVParser.Data
{
/// <summary>
/// GB1078音视频帮助类
/// </summary>
public class GB1078AVHelper
{
/// <summary>
/// 用于存储解析后的AV数据
/// </summary>
SortedDictionary<ulong, AV> sortedDictionary = new SortedDictionary<ulong, AV>();
/// <summary>
/// 设置AV数据
/// </summary>
/// <param name="av"></param>
public void SetData(AV av)
{
sortedDictionary[av.Head.RTTimeSpan] = av;
}
/// <summary>
/// 获取最大时间戳的AV
/// </summary>
/// <returns></returns>
public AV GetMaxAV()
{
ulong maxKey = sortedDictionary.Keys.Max();
AV av = sortedDictionary[maxKey];
// 取走即清除
sortedDictionary.Remove(maxKey);
return av;
}
/// <summary>
/// 获取最小时间戳的AV
/// </summary>
/// <returns></returns>
public AV GetMinAV()
{
ulong minKey = sortedDictionary.Keys.Min();
AV av = sortedDictionary[minKey];
// 取走即清除
sortedDictionary.Remove(minKey);
return av;
}
}
}
Program
using AVParser.Parser;using FFmpeg.AutoGen;
using JX;
using RTForwardServer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AVParser
{
static class Program
{
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Data.GB1078AVHelper helper = new Data.GB1078AVHelper();
for(var i=10; i >= 1; i--)
{
JTRTHead head = new JTRTHead();
head.RTTimeSpan =(ulong) i;
AV av = new AV(AV.AVTYPE.MEDIA_AUDIO, head, new byte[0]);
helper.SetData(av);
}
for (var i = 10; i >= 1; i--)
{
Console.WriteLine(helper.GetMinAV().Head.RTTimeSpan);
}
Console.ReadLine();
}
}
}
OutputInfo
12
3
4
5
6
7
8
9
10
注意事项:C#里面Dictionary的Add键不允许重复,会产生异常。
相关文章推荐
- C#集合之SortedList
- C# SortedDictionary<TKey,TValue>排序 用法 Sort()用法
- C# SortedList 和 SortedDictionary 集合类型
- LeetCode Online Judge 题目C# 练习 - Merge k Sorted Lists
- C# 泛型集合SortedList<T, U>
- LeetCode Online Judge 题目C# 练习 - Merge Sorted Array
- LeetCode #4 Median of Two Sorted Arrays C# Solution
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List II
- LeetCode #21 Merge Two Sorted Lists C# Solution
- c# SortedList的使用
- c# SortedList的妙用 (GroupBy)
- C# SortedList 可重复键的排序键/值对集合
- C#集合之SortedList
- C# 中使用有序表SortedList
- C# SortedList
- [C#]泛型与非泛型集合类的区别及使用例程,包括ArrayList,Hashtable,List<T>,Dictionary<Tkey,Tvalue>,SortedList<Tkey,Tvalue>,Queue<T>,Stack<T>等
- C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序
- C#Redis Sorted-Sets
- LeetCode Online Judge 题目C# 练习 - Median of Two Sorted Arrays
- C#SortedList排序列表怎么样逆序输出