HashSet that preserves ordering

后端 未结 3 1382
滥情空心
滥情空心 2020-11-27 05:30

I need a HashSet that preserves insertion ordering, are there any implementations of this in the framework?

3条回答
  •  自闭症患者
    2020-11-27 05:41

    If you need constant complexity of Add, Remove, Contains and order preservation, then there's no such collection in .NET Framework 4.5.

    If you're okay with 3rd party code, take a look at my repository (permissive MIT license): https://github.com/OndrejPetrzilka/Rock.Collections

    There's OrderedHashSet collection:

    • based on classic HashSet source code (from .NET Core)
    • preserves order of insertions and allows manual reordering
    • features reversed enumeration
    • has same operation complexities as HashSet
    • Add and Remove operations are 20% slower compared to HashSet
    • consumes 8 more bytes of memory per item

提交回复
热议问题