简体   繁体   中英

What is the best data structure to use for this?

I have to store a list of 3 values A,B and C. A and B are a pair of values. C contains two values that describe what the record is, let's just say 1 and 2.

For my purpose, I will be constantly looking up A and/or B to see whether a value exists or the pair exists.

C is only used at the end of the processing to split it into two lists.

Further explaining what the data means -> A and B are just IDs and C is an action like Merge and Demerge. So it looks like this:

1, 2, Merge

3, 4, Demerge

I have a bunch of rules that require me to look up whether the next A or B exist in 'any' IDs, or whether A and B are a pair in a record

Would it be better to use a List<Tuple<A,B,C>> or Dictionary<Tuple<A,B>,C>

Thanks

In this case, a Dictionary seems to suit you better. As you said, you do not care about the C value until afterwards, nor is it part of your unique key. A dictionary will force you to only have a single entry for any given Tuple<A,B> which it seems like you prefer (because you're only checking if a value or tuple already exists). A List would allow you to have duplicate entries.

Depending on your requirements, perhaps you can create a class to represent this object better (to avoid the additional overhead from Dictionary , as you seem to be using a minor subset of its features). For example, do you need to iterate over each Tuple ?

我最终使用了DataTable,它可以完美地访问我需要的任意数量的列,以及进行更新,删除等操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM