发布网友 发布时间:2022-04-23 13:53
共3个回答
热心网友 时间:2023-10-16 18:07
软糖二话不说直接上代码
/// <summary>
/// 按Key排序再按Value排序的SortedList,Key和Value都可以重复,但禁止键值对完全相同。
/// </summary>
public class MutiSortedList<TKey, TValue> : SortedList<TKey, TValue> where TKey : IComparable
{
/// <summary>
/// 相等的键也返回不等
/// </summary>
private class MyComparer<TCKey> : IComparer<TCKey> where TCKey : IComparable
{
public int Compare(TCKey x, TCKey y)
{
int i = x.CompareTo(y);
// (-1) x在y之前 // (1) x在y之后
if (i == 0) { return (-1); } else { return i; }
}
}
public MutiSortedList() : base(new MyComparer<TKey>()) { }
public new void Add(TKey key, TValue value)
{
foreach (KeyValuePair<TKey, TValue> item in this)
{ //检查是否具备这个key,并且检查value是否重复
if (item.Key.Equals(key) && item.Value.Equals(value)) { return; }
}
base.Add(key, value);
}
public new void Remove(TKey key)
{
for (int i = 0; i < this.Count; i++)
{
if (Keys[i].Equals(key)) { base.RemoveAt(i); }
}
}
public void Remove(TValue value)
{
for (int i = 0; i < this.Count; i++)
{
if (Values[i].Equals(value)) { base.RemoveAt(i); }
}
}
public void Remove(TKey key, TValue value)
{
for (int i = 0; i < this.Count; i++)
{
if (Keys[i].Equals(key) & Values[i].Equals(value)) { base.RemoveAt(i); }
}
}
public new TValue this[TKey key]
{
get { return base[key]; }
}
/// <summary>
/// 返回键对应的值列表
/// </summary>
/// <param name="key"></param>
/// <param name="升序"></param>
/// <returns>返回IList(Of 值)</returns>
public IList<TValue> this[TKey key, bool 升序]
{
get {
var 结果 = new List<TValue>();
for (int i = 0; i < Count; i++)
{
if (Keys[i].Equals(key))
{ 结果.Add(Values[i]); }
}
return 结果;
}
}
}
/// <summary>
/// 按默认顺序排序的Dictionary,Key和Value都可以重复,但禁止键值对完全相同。
/// <para>继承于 <see cref="List{Tuple}"/><see cref="Tuple{TKey, TValue}"/></para>
/// </summary>
public class TupleList<TKey, TValue> : List<Tuple<TKey, TValue>> {
public void Add(TKey key, TValue value) {
foreach (var item in this) { //检查是否具备这个key,并且检查value是否重复
if (item.Item1.Equals(key) && item.Item2.Equals(value)) { return; }
}
base.Add(new Tuple<TKey, TValue>(key, value));
}
public void Remove(TKey key) {
foreach (var item in this) {
if (item.Item1.Equals(key)) { Remove(item); }
}
}
/// <summary>
/// 返回键对应的多个值组成的列表<see cref="IList{TValue}"/>
/// </summary>
/// <param name="key"></param>
/// <param name="升序"></param>
/// <returns>返回IList(Of 值)</returns>
public IList<TValue> this[TKey key, bool 升序 = false] {
get {
var 结果 = new List<TValue>();
foreach (var item in this) { //检查是否具备这个key,并且检查value是否重复
if (item.Item1.Equals(key)) { 结果.Add(item.Item2); }
}
return 结果;
}
}
}
热心网友 时间:2023-10-16 18:07
JAVA里有个IndentityHashMap可以实现重复key的集合热心网友 时间:2023-10-16 18:08
写一个新类KeyValue,有成员key和value,重写Equals方法,为return key.Eqauls(other.key)&&value.Equals(other.value);热心网友 时间:2023-10-16 18:07
软糖二话不说直接上代码
/// <summary>
/// 按Key排序再按Value排序的SortedList,Key和Value都可以重复,但禁止键值对完全相同。
/// </summary>
public class MutiSortedList<TKey, TValue> : SortedList<TKey, TValue> where TKey : IComparable
{
/// <summary>
/// 相等的键也返回不等
/// </summary>
private class MyComparer<TCKey> : IComparer<TCKey> where TCKey : IComparable
{
public int Compare(TCKey x, TCKey y)
{
int i = x.CompareTo(y);
// (-1) x在y之前 // (1) x在y之后
if (i == 0) { return (-1); } else { return i; }
}
}
public MutiSortedList() : base(new MyComparer<TKey>()) { }
public new void Add(TKey key, TValue value)
{
foreach (KeyValuePair<TKey, TValue> item in this)
{ //检查是否具备这个key,并且检查value是否重复
if (item.Key.Equals(key) && item.Value.Equals(value)) { return; }
}
base.Add(key, value);
}
public new void Remove(TKey key)
{
for (int i = 0; i < this.Count; i++)
{
if (Keys[i].Equals(key)) { base.RemoveAt(i); }
}
}
public void Remove(TValue value)
{
for (int i = 0; i < this.Count; i++)
{
if (Values[i].Equals(value)) { base.RemoveAt(i); }
}
}
public void Remove(TKey key, TValue value)
{
for (int i = 0; i < this.Count; i++)
{
if (Keys[i].Equals(key) & Values[i].Equals(value)) { base.RemoveAt(i); }
}
}
public new TValue this[TKey key]
{
get { return base[key]; }
}
/// <summary>
/// 返回键对应的值列表
/// </summary>
/// <param name="key"></param>
/// <param name="升序"></param>
/// <returns>返回IList(Of 值)</returns>
public IList<TValue> this[TKey key, bool 升序]
{
get {
var 结果 = new List<TValue>();
for (int i = 0; i < Count; i++)
{
if (Keys[i].Equals(key))
{ 结果.Add(Values[i]); }
}
return 结果;
}
}
}
/// <summary>
/// 按默认顺序排序的Dictionary,Key和Value都可以重复,但禁止键值对完全相同。
/// <para>继承于 <see cref="List{Tuple}"/><see cref="Tuple{TKey, TValue}"/></para>
/// </summary>
public class TupleList<TKey, TValue> : List<Tuple<TKey, TValue>> {
public void Add(TKey key, TValue value) {
foreach (var item in this) { //检查是否具备这个key,并且检查value是否重复
if (item.Item1.Equals(key) && item.Item2.Equals(value)) { return; }
}
base.Add(new Tuple<TKey, TValue>(key, value));
}
public void Remove(TKey key) {
foreach (var item in this) {
if (item.Item1.Equals(key)) { Remove(item); }
}
}
/// <summary>
/// 返回键对应的多个值组成的列表<see cref="IList{TValue}"/>
/// </summary>
/// <param name="key"></param>
/// <param name="升序"></param>
/// <returns>返回IList(Of 值)</returns>
public IList<TValue> this[TKey key, bool 升序 = false] {
get {
var 结果 = new List<TValue>();
foreach (var item in this) { //检查是否具备这个key,并且检查value是否重复
if (item.Item1.Equals(key)) { 结果.Add(item.Item2); }
}
return 结果;
}
}
}
热心网友 时间:2023-10-16 18:07
JAVA里有个IndentityHashMap可以实现重复key的集合热心网友 时间:2023-10-16 18:08
写一个新类KeyValue,有成员key和value,重写Equals方法,为return key.Eqauls(other.key)&&value.Equals(other.value);