简体   繁体   English

集合与有序集合

[英]Set vs OrderedSet

Can you please give me an example when to use OrderedSet instead of Set ?您能否举例说明何时使用OrderedSet而不是Set I've run a couple of tests and even tho the immutable-js documentation says我已经运行了几个测试,甚至immutable-js 文档

Iteration order of a Set is undefined, however is stable Set 的迭代顺序是未定义的,但是是稳定的

it seems element order within Set is always the same as the one in which elements were added.似乎Set中的元素顺序总是与添加元素的顺序相同。 That's what seems to be the sole benefit of the OrderedSet structure:这似乎是OrderedSet结构的唯一好处:

A type of Set that has the additional guarantee that the iteration order of values will be the order in which they were added.一种 Set 类型,它额外保证值的迭代顺序将是它们添加的顺序。

It coincidally does add elements at the end, but it is not guaranteed to always be that way.恰好在末尾添加元素,但不能保证总是那样。 It might change in the next release and it does not always have to be predictable.它可能会在下一个版本中发生变化,并且不一定总是可以预测的。 All it promises is to be stable across multiple iterations over the same data.它所承诺的是在同一数据的多次迭代中保持稳定。 To be honest, I can't see any useful use case for OrderedSet.老实说,我看不到 OrderedSet 有任何有用的用例。 Depending on the needs, List , Map or OrderedMap or even Set are better suited.根据需要, ListMapOrderedMap甚至Set更适合。

If you manage to update a Set, it can change the order.如果您设法更新一个集合,它可以更改顺序。 Again, this is usually a bad choice for a data structure and you probably should rearrange your data structure, eg use OrderedMap or List instead.同样,这对于数据结构通常是一个糟糕的选择,您可能应该重新安排您的数据结构,例如改用 OrderedMap 或 List。

The following sample shows that the order can be a bit unexpected:以下示例显示顺序可能有点出乎意料:

 function modifySet(set) { set = set.add(0); set = set.add(1); set = set.add(2); return set.remove(0); } let unorderedSet = Immutable.Set([4,5]); unorderedSet = modifySet(unorderedSet); console.log('Set:'); for (const value of unorderedSet) { console.log(value); } let orderedSet = Immutable.OrderedSet([4,5]); orderedSet = modifySet(orderedSet); console.log('OrderedSet:'); for (const value of orderedSet) { console.log(value); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>

Since you can modify keys (,) of a set: it can reorder the elements too:由于您可以修改集合的键 (,):它也可以对元素重新排序:

 let set = Immutable.Set([ Immutable.Map({b:1, a:true}), Immutable.Map({b:2,a:true}), Immutable.Map({b:3,a:true}) ]).map((t) => { if (t.get('b') === 2) return t.set('a', false); return t; }); console.log('2 is now at the end'); console.log(set.toJS());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.js"></script>

To add insult to injury, there is a bug in ImmutableJs RC12, which makes OrderedSet behave in the same way (moving the updated element to the end of the list).雪上加霜的是,ImmutableJs RC12 中存在一个错误,这使得 OrderedSet 的行为方式相同(将更新的元素移动到列表的末尾)。 That one is already fixed in the (so far) unreleased upcoming 4.0 release.该问题已在(到目前为止)未发布的即将发布的 4.0 版本中修复。

Ok that was an interesting excursion, you made us (the loose group of maintainers) look again into how this rarely used structure works.好的,这是一次有趣的旅行,你让我们(松散的维护者小组)再次研究这个很少使用的结构是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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