NSSet(集合)
集合:集合(NSSet)和数组(NSArray)有相似之处,都是存储不同的对象的地址;不过NSArray是有序的集合,NSSet是无序的集合。
集合是一种哈希表,运用散列算法,查找集合中的元素比数组速度更快,但是它没有顺序。
集合的初始化
int main(int argc, const char * argv[]) {
@autoreleasepool {
//类方法初始化一个集合
//方法1)
//给一个集合赋多个值
NSSet *c = [NSSet setWithObjects:@"one", @"two", @"three", @"four", nil];
//集合是无序的
NSLog(@"%@", c);
//方法2)
//先创建一个包含元素的数组_d
NSArray *_d = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", @"five", nil];
//将数组_d里面的所有元素全部传给集合d
NSSet *d = [NSSet setWithArray:_d];
NSLog(@"%@", d);
//实例化方法初始化一个集合, 加alloc创建一个空间!!!
//方法1)
NSSet *e = [[NSSet alloc]init];
//方法2)
NSSet *f = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil ];
//方法3)
NSSet *g = [[NSSet alloc]initWithSet:f];
//方法4)
NSArray *_h = [NSArray arrayWithObjects:@"6",@"7",@"8",@"9",@"10", nil];
NSSet *h = [[NSSet alloc]initWithArray:_h];
NSLog(@"%@", h);
}
return 0;
}
集合的使用
int main(int argc, const char * argv[]) {
@autoreleasepool {
//新创建一个集合
NSSet *a = [NSSet setWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil];
//打印集合中的所有对象个数
NSLog(@"%lu", (unsigned long)[a count]);
//打印集合中任意一个对象
NSLog(@"%@", [a anyObject]);
//打印所有对象
NSLog(@"%@", [a allObjects]);
}
return 0;
}
集合的比较
int main(int argc, const char * argv[]) {
@autoreleasepool {
//新创建一个集合a
NSSet *a = [NSSet setWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil];
NSSet *b = [NSSet setWithObjects:@"two", @"one", @"three", @"six", @"five", @"four", @"seven", nil];
//判断是不是相同的集合,如果是打印1,如果不是打印0
BOOL x1 = [a isEqualToSet:b];
NSLog(@"%hhd", x1);
NSSet *c = [NSSet setWithObjects:@"one", @"two", @"three", nil];
NSSet *d = [NSSet setWithObjects:@"one", @"two", nil];
//判断集合d是不是集合c的子集合,如果是打印1,如果不是打印0
BOOL x2 = [d isSubsetOfSet:c];
NSLog(@"%hhd", x2);
NSSet *e = [NSSet setWithObjects:@"1", @"2",@"3", nil];
NSSet *f = [NSSet setWithObjects:@"1", @"a", @"b", @"c", nil];
//判断有没有交集,如果有打印1,如果没有打印0
BOOL x3 = [e intersectsSet:f];
NSLog(@"%hhd", x3);
NSSet *g = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];
//判断有没有集合g里面有没有特定元素@"c",如果有打印1,如果没有打印0
BOOL x4 = [g containsObject:@"c"];
NSLog(@"%hhd", x4);
}
return 0;
}
NSMutableSet(可变集合)
可变集合的初始化
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化一个可变集合
NSMutableSet *a = [[NSMutableSet alloc]initWithObjects:@"a", @"b", @"c", nil];
NSLog(@"%@", a);
}
return 0;
}
可变集合的使用
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建一个可变集合a
NSMutableSet *a = [[NSMutableSet alloc]initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", nil];
//创建一个可变集合b
NSMutableSet *b = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil];
//从集合a中剔除集合b相同的对象
[a minusSet:b];
NSLog(@"%@", a);
NSMutableSet *c = [NSMutableSet setWithObjects:@"a", @"b", nil];
NSMutableSet *d = [NSMutableSet setWithObjects:@"c", @"d", nil];
//将集合d添加到集合c中
[c unionSet:d];
NSLog(@"%@", c);
NSMutableSet *e = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil];
NSMutableSet *f = [NSMutableSet setWithObjects:@"a", @"e", @"f", nil];
[e intersectSet:f];
//集合e保留与集合f的交集,其余删掉
NSLog(@"%@", e);
NSMutableSet *g = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil];
//在集合g中添加一个对象@"1"
[g addObject:@"1"];
NSLog(@"%@", g);
//新建一个数组h
NSArray *h = [NSArray arrayWithObjects:@"d", @"e", nil];
//将数组h添加到集合g中
[g addObjectsFromArray:h];
NSLog(@"%@", g);
NSMutableSet *i = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil];
//将集合i中的元素@"b"删掉
[i removeObject:@"b"];
NSLog(@"%@", i);
//删掉集合i中的全部元素
[i removeAllObjects];
NSLog(@"%@", i);
//将集合j中的元素根据数组k删除
//先创建一个可变集合j
NSMutableSet *j = [NSMutableSet setWithObjects:@"a", @"b", @"c", @"d", nil];
//创建一个数组k
NSArray *k = [NSArray arrayWithObjects:@"a", @"d", nil];
//现根据数组k找出每个key@"i"对应的value,再判断集合j里面有没有,如果有,则删掉
for (int i=0; i<k.count; i++) {
if ([j containsObject:[k objectAtIndex:i]]) {
[j removeObject:[k objectAtIndex:i]];
}
}
NSLog(@"%@", j);
}
return 0;
}