ios 什么时候在 NSArray 上使用 NSSet 更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10997404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is it better to use an NSSet over an NSArray?
提问by geminiCoder
回答by James Webster
The image from Apple's Documentationdescribes it very well:
Apple 文档中的图像很好地描述了它:
Array
is an ordered(order is maintained when you add) sequence of elements
Array
是有序的(添加时保持顺序)元素序列
[array addObject:@1];
[array addObject:@2];
[array addObject:@3];
[array addObject:@4];
[array addObject:@6];
[array addObject:@4];
[array addObject:@1];
[array addObject:@2];
[1, 2, 3, 4, 6, 4, 1, 2]
Set
is a distinct(no duplicates), unorderedlist of elements
Set
是一个独特的(没有重复的)、无序列表的元素
[set addObject:@1];
[set addObject:@2];
[set addObject:@3];
[set addObject:@4];
[set addObject:@6];
[set addObject:@4];
[set addObject:@1];
[set addObject:@2];
[1, 2, 6, 4, 3]
回答by Ole Begemann
When the order of the items in the collection is not important, sets offer better performance for finding items in the collection.
当集合中项目的顺序不重要时,集合为查找集合中的项目提供更好的性能。
The reason is that a set uses hash values to find items (like a dictionary) while an array has to iterate over its entire contents to find a particular object.
原因是集合使用哈希值来查找项目(如字典),而数组必须遍历其整个内容才能找到特定对象。
回答by woz
The best answer is to this is Apple's own documentation.
最好的答案是Apple 自己的文档。
The main difference is that NSArray
is for an ordered collection and NSSet
is for an unordered collection.
主要区别在于它NSArray
用于有序集合和NSSet
用于无序集合。
There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet
is great. However, in many cases, you need to do things that only an NSArray
can do, so you sacrifice the speed for those abilities.
有几篇文章讨论了两者之间的速度差异,就像这篇文章。如果您正在遍历无序集合,那就NSSet
太好了。然而,在很多情况下,你需要做只有一个NSArray
人才能做的事情,所以你为了这些能力而牺牲了速度。
NSSet
NSSet
- Primarily access items by comparison
- Unordered
- Does not allow duplicates
- 主要通过比较访问项目
- 无序
- 不允许重复
NSArray
数组
- Can access items by index
- Ordered
- Allows duplicates
- 可以通过索引访问项目
- 已订购
- 允许重复
That's all there really is to it! Let me know if that helps.
这就是它的全部内容!如果这有帮助,请告诉我。
回答by Jason
NSOrderedSet is available in iOS 5+ so with that the main difference becomes whether you want duplicate objects in the data structure.
NSOrderedSet 在 iOS 5+ 中可用,因此主要区别在于您是否想要数据结构中的重复对象。
回答by iOS Lifee
NSArray:
数组:
- Ordered collection of data
- Allows duplicates
- It is collection type object
- 有序收集数据
- 允许重复
- 它是集合类型对象
NSSet:
NSSet:
- Unordered collection of data
- Does not allow duplicates
- It is also collection type object
- 无序的数据集合
- 不允许重复
- 它也是集合类型对象
回答by Sulthan
An array is used to access items by their index. Any item can be inserted into the array multiple times. Arrays mantain the order of their elements.
数组用于通过索引访问项目。任何项目都可以多次插入到数组中。数组维护其元素的顺序。
A set is used basically only to check if the item is in the collection or not. The items have no concept of order or indexing. You cannot have an item in a set twice.
集合基本上仅用于检查项目是否在集合中。这些项目没有顺序或索引的概念。你不能在一个集合中拥有一个项目两次。
If an array wants to check if it contains an element, it has to check all its items. Sets are designed to use faster algorithms.
如果一个数组想要检查它是否包含一个元素,它必须检查它的所有项目。集合旨在使用更快的算法。
You can imagine a set like a dictionary without values.
你可以想象一个像没有值的字典一样的集合。
Note that array and set are not the only data structures. There are other, e.g. Queue, Stack, Heap, Fibonacci's Heap. I would recommend reading a book about algorithms and data structures.
请注意,数组和集合不是唯一的数据结构。还有其他的,例如队列、堆栈、堆、斐波那契堆。我建议阅读一本关于算法和数据结构的书。
See wikipediafor more information.
有关更多信息,请参阅维基百科。
回答by abc221
NSArray *Arr;
NSSet *Nset;
Arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"2",@"1", nil];
Nset=[NSSet setWithObjects:@"1",@"2",@"3",@"3",@"5",@"5", nil];
NSLog(@"%@",Arr);
NSLog(@"%@",Nset);
the array
数组
2015-12-04 11:05:40.935 [598:15730] ( 1, 2, 3, 4, 2, 1 )
2015-12-04 11:05:40.935 [598:15730] ( 1, 2, 3, 4, 2, 1 )
the set
集合
2015-12-04 11:05:43.362 [598:15730] { ( 3, 1, 2, 5 )}
2015-12-04 11:05:43.362 [598:15730] { ( 3, 1, 2, 5 )}
回答by joakim
The main differences have already been given in other answers.
其他答案中已经给出了主要区别。
I'd just like to note that because of the way sets and dictionaries are implemented (i.e. using hashes),one should be careful not to use mutable objects for the keys.
我只想指出,由于集合和字典的实现方式(即使用散列),应该小心不要使用可变对象作为键。
If a key is mutated then the hash will (probably) change too, pointing to a different index/bucket in the hash table. The original value won't be deleted and will actually be taken into account when enumerating or asking the structure for its size/count.
如果键发生变异,则哈希(可能)也会发生变化,指向哈希表中的不同索引/存储桶。原始值不会被删除,并且在枚举或询问结构的大小/计数时实际上会被考虑在内。
This can lead to some really hard to locate bugs.
这可能会导致一些非常难以定位的错误。
回答by Che
Hereyou can find a pretty thorough comparison of the NSArray
and NSSet
datastructures.
在这里您可以找到NSArray
和 数据NSSet
结构的非常彻底的比较。
Short conclusions:
简短的结论:
Yes, NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. Lesson: if you only need to iterate contents, don't use an NSSet.
Of course, if you need to test for inclusion, work hard to avoid NSArray. Even if you need both iteration and inclusion testing, you should probably still choose an NSSet. If you need to keep your collection ordered and also test for inclusion, then you should consider keeping two collections (an NSArray and an NSSet), each containing the same objects.
NSDictionary is slower to construct than NSMapTable — since it needs to copy the key data. It makes up for this by being faster to lookup. Of course, the two have different capabilities so most of the time, this determination should be made on other factors.
是的,NSArray 比 NSSet 更简单地保持和迭代。构建速度快 50%,迭代速度快 500%。教训:如果您只需要迭代内容,请不要使用 NSSet。
当然,如果你需要测试包含,努力避免NSArray。即使您需要迭代和包含测试,您可能仍然应该选择 NSSet。如果您需要保持集合有序并测试是否包含,那么您应该考虑保留两个集合(一个 NSArray 和一个 NSSet),每个集合都包含相同的对象。
NSDictionary的速度低于NSMapTable建设 - 因为它需要复制的关键数据。它通过更快的查找来弥补这一点。当然,这两个具有不同的功能,所以大部分的时间,这个决定应该取决于其他因素做出。
回答by iDevAmit
You would typically use a Set when access speed is of the essence and order doesn't matter, or is determined by other means (through a predicate or sort descriptor). Core Data for example uses sets when managed objects are accessed via a to-many relationship
当访问速度至关重要且顺序无关紧要,或者由其他方式(通过谓词或排序描述符)确定时,您通常会使用Set。例如,Core Data 在通过一对多关系访问托管对象时使用集合