【问题标题】:Best data structure to store key value pairs when the following operations are required需要以下操作时存储键值对的最佳数据结构
【发布时间】:2018-02-22 19:53:57
【问题描述】:

我需要存储一个键值对列表。键值对将被多个类使用,因此理想情况下它类似于我定义的结构列表。

Public Structure Person
    Public ID As Integer
    Public Weight As Decimal
End Structure

'all use Person
    class census
    class importpeople
    class determinebestdiet
    etc
  1. 我需要按值排序
  2. 我需要删除一个人(通过 Person.ID)或添加一个新的键值对(并在每次添加后重新排序)
  3. 我需要对所有值求和

似乎人们经常使用字典,然后随意破解。

【问题讨论】:

    标签: vb.net list sorting


    【解决方案1】:

    您可以使用List<Person>来帮助您的工作

    1. 使用 List.Sort 方法:
      theList.Sort(Function(x, y) x.age.CompareTo(y.age))
      使用 OrderBy 扩展方法:
      theList = theList.OrderBy(Function(x) x.age).ToList()
      详情可以看这个帖子:https://stackoverflow.com/a/11736001/1050927

    2. 只需从列表中删除并致电SortOrderBy
      取决于你想做什么,你可以使用OrderBySkipTake 来获得排序后的特定nth 人。

    3. Sum
      PersonList.Sum(Function(per) per.Weight)

    而且我相信Dictionary 也可以做到这一点,只是你不能只重复使用Person

    【讨论】:

      猜你喜欢
      • 2013-01-14
      • 2010-09-05
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多