Roy
Roy

Reputation: 537

Dictionary vs SortedList for data storage and processing

I have created a dictionary, consisting of DateTime as the Key and a custom class for Value.

class Class2
{
    public decimal Value1 { get; set; }
    public decimal Value2 { get; set; }
    public decimal Value3 { get; set; }
}

class TestClass
{
    public static void Main(string[] args) 
    {
        var dict = new Dictionary<DateTime, Class2>();

        dict.Add(new DateTime(2014, 6, 1), new Class2() { Value1 = 1, Value2 = 2, Value3 = 3 });
        dict.Add(new DateTime(2014, 6, 2), new Class2() { Value1 = 4, Value2 = 5, Value3 = 6 });
        dict.Add(new DateTime(2014, 6, 3), new Class2() { Value1 = 10, Value2 = 20, Value3 = 40 });
        dict.Add(new DateTime(2014, 6, 4), new Class2() { Value1 = -5, Value2 = -6, Value3 = -8 });

        // form a list consisting of Value1 and pass this list to a function for processing
    }
}

In the comment line, I want to select some particular data points for Value1 and form a list. These data points selection criteria will be based on time (that is why I need a DateTime field). This list will be passed to another function for further processing.

My question is two-fold:

  1. Should I use a Dictionary to store this kind of data? I have seen other posts, and some are talking about the use of a SortedList. The data I am going to add to these container classes will be in sequential order (of time).

  2. What's the best practice for creating the list (of Value1)? I could have created a separate list from scratch, but I would like to know if there is any better alternative other than this (given that I already have stored in the data in the Dictionary instance).

Upvotes: 1

Views: 1929

Answers (1)

Habib
Habib

Reputation: 223277

Use SortedDictionary<TKey, TValue> Class.

SortedDictionary<DateTime, Class2> dict = new SortedDictionary<DateTime, Class2>();

Following from the documentation explains well about the difference between using a SortedList vs SortedDictionary

The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:

  • SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey, TValue>.

  • SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for SortedList<TKey, TValue>.

  • If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

For your question:

What's the best practice for creating the list (of Value1)?

List<decimal> list = dict.Values.Select(r=> r.Value1).ToList();

For your comment:

how to form a list consisting of Values1, say, for the last 3 days

List<decimal> listOfValue1 = dict.Where(r=> r.Key >= DateTime.Today.AddDays(-3) 
                                            &&  r.Key <= DateTime.Today)
                                 .Select(r=> r.Value.Value1)
                                 .ToList();

Upvotes: 2

Related Questions