簡體   English   中英

使用哪個 c# 集合而不是 List <KeyValuePair<string, double> &gt;?

[英]which c# collection to use instead of List<KeyValuePair<string, double>>?

我想存儲數據,例如

{
 {"apple",15   }
 {"pear",12.5  }
 {"", 10       }
 {"", 0.45     }
}

數據將繪制在條形圖上(字符串將是圖例,雙精度將是值)插入順序很重要。 性能無所謂。 字符串可以重復或為空。 (值也可以重復)

我需要獲得最小值和最大值(如果可能的話很容易)來設置比例。

我用

List<KeyValuePair<string, double>> data = new List<KeyValuePair<string, double>>();
data.Add(new KeyValuePair<string,double>("",i));

相當無聊和不可讀。 有沒有更干凈的方法來做到這一點?

StringDoubleCollection data = new StringDoubleCollection();
data.add("apple",15);
data.add("",10);

double max = data.values.Max(); 
double min = data.values.Min();

如果不是如何獲得List<KeyValuePair<string, double>>的最大值而不會太麻煩

NameValueCollection 看起來不錯,但它是一個<string,string>我需要一個<string,double>

您可以創建一個如下所示的類:

class X
{
     public string Name { get; set; }
     public double Value { get; set; }

     // name is an optional parameter (this means it can be used only in C# 4)
     public X(double value, string name = "")
     {
         this.Name = name;
         this.Value = value;
     }

     // whatever
}

然后使用帶有選擇器的 LINQ 獲取最大值和最小值:

var data = new List<X>();
data.Add(new X(35.0, "Apple"))
data.Add(new X(50.0));

double max = data.Max(a => a.Value);
double min = data.Min(a => a.Value);

編輯:如果上面的代碼對您來說仍然不可讀,請嘗試使用運算符改進它,以便在您只想擁有該值的情況下使用它。

// Inside X class...
public static implicit operator X(double d)
{
    return new X(d);
}

// Somewhere else...
data.Add(50.0);

要確定您真正想要哪種數據結構,讓我們看看您的使用模式。

  • 插入順序很重要。
  • 您不能通過密鑰訪問您的項目。
  • 你想要最小值和最大值。

堆提供最小值最大值,但不保留順序。 基於哈希的字典也不保留順序。 列表實際上是您的數據結構的不錯選擇。 它可用並提供出色的支持。

您可以通過為數據結構和條形數據定義類來美化您的代碼。 您可以向集合添加最小/最大功能。 注:我沒有使用LINQ的最小/最大功能,因為它們返回的最低,而不是最小元素

public class BarGraphData {
    public string Legend { get; set; }
    public double Value { get; set; }
}

public class BarGraphDataCollection : List<BarGraphData> {
    // add necessary constructors, if any

    public BarGraphData Min() {
        BarGraphData min = null;
        // finds the minmum item
        // prefers the item with the lowest index
        foreach (BarGraphData item in this) {
            if ( min == null )
                min = item;
            else if ( item.Value < min.Value )
                min = item;
        }
        if ( min == null )
            throw new InvalidOperationException("The list is empty.");
        return min;
    }

    public BarGraphData Max() {
        // similar implementation as Min
    }
}

你看過LookUp嗎?

唯一的問題是它是不可變的,所以你需要能夠一次性創建你的集合。

正如 Anthony Pegram 所指出的,創建一個有點痛苦。 這取決於您的數據來自何處。 看看ToLookup方法。

如果它對於可用性來說是值得的(即您在任何地方都使用了笨拙的List<KeyValuePair<string, double>>集合,那么實現StringDoubleCollection可能是值得的。用您在示例中描述的更友好的語法。

而且,正如其他評論/答案所暗示的那樣,該框架似乎並未提供滿足您所有要求的更簡單的解決方案......

至於“最大值”,我假設您指的是具有最大值的鍵值對。 它可以像這樣檢索:

var max = list.Select(kvp => kvp.Value).Max();

只需定義您自己的模型類來保存數據,而不是依賴於 KeyValuePair,一切都會變得更清晰:

using System;
using System.Collections.Generic;

public class Fruit
{
    public string Name {get; set;}
    public double Price {get; set;}
}

public class Program
{
    public static void Main()
    {
        List<Fruit> _myFruit = new List<Fruit>();

        _myFruit.Add(new Fruit{Name="apple", Price=15   });
        _myFruit.Add(new Fruit{Name="pear", Price=12.5  });
        _myFruit.Add(new Fruit{Name="",  Price=10       });
        _myFruit.Add(new Fruit{Name="",  Price=0.45     });

        // etc...
    }
}

如何實現 StringDoubleCollection 像你想要的那樣工作......

public class StringDoubleCollection
{
    private List<KeyValuePair<string, double>> myValues;
    public List<double> values
    {
        get { return myValues.Select(keyValuePair => keyValuePair.Value).ToList(); }
    }

    public void add(string key, double value)
    {
        myValues.Add(new KeyValuePair<string,double>(key,value));
    }
}

您可以實現 Dictionary<key, value>

   Dictionary<string, string> openWith = new Dictionary<string, string>();


   openWith.Add("txt", "notepad.exe");
   openWith.Add("bmp", "paint.exe");
   openWith.Add("dib", "paint.exe");
   openWith.Add("rtf", "wordpad.exe");

https://docs.microsoft.com/pt-br/dotnet/api/system.collections.generic.dictionary-2?view=net-5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM