Best Collection for Fast String Lookup

后端 未结 7 1659
别跟我提以往
别跟我提以往 2020-12-30 22:28

I need a list of strings and a way to quickly determine if a string is contained within that list.

To enhance lookup speed, I considered SortedList and

7条回答
  •  情歌与酒
    2020-12-30 23:01

    I know the question is old as hell, but I just had to solve the same problem, only for a very small set of strings(between 2 and 4).

    In my case, I actually used manual lookup over an array of strings which turned up to be much faster than HashSet(I benchmarked it).

    for (int i = 0; i < this.propertiesToIgnore.Length; i++)
    {
        if (this.propertiesToIgnore[i].Equals(propertyName))
        {
            return true;
        }
    }
    

    Note, that it is better than hash set for only for tiny arrays!

    EDIT: works only with a manual for loop, do not use LINQ, details in comments

提交回复
热议问题