What is the advantage of using a Two Item Tuple versus a Dictionary?
Asked Answered
R

4

12

I have code where I return a list of IWebElements and their corresponding names? My understanding is that a tuple with two items is basically the same thing but the Dictionary uses hash mapping to relate the two values. What is the advantage of using a Two Item Tuple over a Dictionary or vice versa?

public Dictionary<IWebElement, string> SelectAllOptions(IWebDriver driver, ref DataObject masterData)
    {
        //Get the ID of the dropdown menu
        DatabaseRetrieval.GetObjectRepository(ref masterData);
        var strDropMenuId = masterData.DictObjectRepository["ID"];
        //Find the dropdown menu and pull all options into a list
        try
        {
            var dropMenu = new SelectElement(driver.FindElement(By.Id(strDropMenuId)));
            // TODO want to know how we want this list to return. 
            var options = dropMenu.Options as List<IWebElement>;
            if (options != null)
            {
                var values = options.ToDictionary(option => option, option => option.Text);
                return values;
            }
        }
Riess answered 28/4, 2014 at 17:22 Comment(3)
A Tuple<T1, T2> is analogous to KeyValuePair<TKey, TValue>, not a dictionary. (a dictionary contains a series of key value pairs.Membranous
I'm curious why you even need a list of objects with corresponding names when the object already refers to its own name (option.Text)...Stans
I thought the same thing when I looked at it, but a colleague needs a list or string array for some reason.Riess
P
11

A Tuple<T1, T2> represents a pair of values. That pair don't necessarily have to mean "These two items are related". When using a KeyValuePair<TKey, TValue>, you would expect that given a key, you would get a value and the two would have some sort of connection between one another.

Tuples implement IComparable and IStructuralEquatable, which makes it easier to compare Tuples. Other than that, I would look at it from a logical perspective, do I need to match a given key to a value?, or do I just need to couple together two values and a class might be a bit of an overhead for that.

One downside of Tuples as I see it, is that you have to deal with properties labeled Item1 and Item2, which might make it a bit less readable.

Also, remember that a Tuple is a class (an Immutable one) and KeyValuePair is a struct, so when you passing them as arguments you pass Tuple by reference and KeyValuePair by value (except for explicitly declaring ref or out)

Petitioner answered 28/4, 2014 at 17:28 Comment(1)
You can use for now naming tuples.Gaselier
S
26

As a general rule, you do not want to "pay" * for possibilities that your program does not need. For example, if your program is interested in retrieving and processing a sequence of pairs (also known as "two-member tuples") but it does not need to perform lookups from the first member of a tuple to the second, then providing a collection of pairs is more efficient:

IEnumerable<Tuple<IWebElement, string>> SelectAllOptions(...)
  • This approach takes less memory, because you do not allocate space for hash buckets
  • This approach takes less CPU, because there is no hash key computation or collision resolution costs
  • This approach does not suggest to a reader that the data is intended for lookups.

Of course if the data structure that you return is intended for lookups, then you should either return a dictionary, or construct one on the client side to transfer some of the CPU load from the server to the client.

* With memory, CPU cycles, decreased readability, etc.

Skeptic answered 28/4, 2014 at 17:29 Comment(1)
I think this should be marked as the right answer as it illustrates the differences in use-cases better.Gallager
P
11

A Tuple<T1, T2> represents a pair of values. That pair don't necessarily have to mean "These two items are related". When using a KeyValuePair<TKey, TValue>, you would expect that given a key, you would get a value and the two would have some sort of connection between one another.

Tuples implement IComparable and IStructuralEquatable, which makes it easier to compare Tuples. Other than that, I would look at it from a logical perspective, do I need to match a given key to a value?, or do I just need to couple together two values and a class might be a bit of an overhead for that.

One downside of Tuples as I see it, is that you have to deal with properties labeled Item1 and Item2, which might make it a bit less readable.

Also, remember that a Tuple is a class (an Immutable one) and KeyValuePair is a struct, so when you passing them as arguments you pass Tuple by reference and KeyValuePair by value (except for explicitly declaring ref or out)

Petitioner answered 28/4, 2014 at 17:28 Comment(1)
You can use for now naming tuples.Gaselier
A
2

To add to the other answers, there are sometimes advantages to storing key-value data as a list of tuples instead of in a dictionary.

Depending on your needs, it might not be important that your lookups are fast, but it might be important that the order you insert into the list remains fixed. You can iterate through the keys and the values in a dictionary but the order is not defined.

Another advantage is that you can put as many pairs with the same first element into the list as you want, where with a dictionary you can only have one value per unique key.

Arturo answered 28/4, 2014 at 17:34 Comment(0)
O
2

One advantage of tuples over dictionaries is that tuples can be named. Using List<(string text, string url)> links is more meaningful than Dictionary<string, string> links.

Oxus answered 5/3, 2020 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.