1.NSSet
1.1 集合,是无序、没有重复元素的数组
1.2 创建方法(3种)
1.3 自定义类的对象防止重复时需要写以下方法
—1.3.1 粗滤: -(NSUInteger)hash
—1.3.2 细滤: -(BOOL)isEqual:(id)object;
1.4 判断集合中是否拥有指定的元素
1.5 判断两个集合是否相等
1.6 判断某个集合是否是另一个集合的子集
1.7 将集合转换成数组
1.8 遍历(3种)
建一个Student类
.h文件
#import <Foundation/Foundation.h>
@interface CZStudent : NSObject
@property NSString* name;
@property int age;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWithName:(NSString*)name andAge:(int)age;
-(void)show;
@end
.m文件 (在里面重写函数)
#import "CZStudent.h"
@implementation CZStudent
-(id)initWithName:(NSString*)name andAge:(int)age;
{
self = [super init];
if (self) {
self.name=name;
self.age=age;
}
return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age
{
__autoreleasing CZStudent *stu=[[CZStudent alloc]initWithName:name andAge:age];
return stu;
}
-(void)show
{
NSLog(@"name:%@,age:%d",self.name,self.age);
}
-(NSString *)description
{
return [NSString stringWithFormat:@"name:%@,age:%d",self.name,self.age ];
}
-(NSUInteger)hash //粗滤
{
return self.age; //不进行粗滤 return YES;
}
-(BOOL)isEqual:(id)object //细滤
{
if(self==object)
{
return YES; //YES表示重复
}
if([object isMemberOfClass:[CZStudent class]])
{
CZStudent *s=object;
if([self.name isEqualToString:s.name]==YES&&self.age==s.age)
{
return YES;
}
}
return NO;//NO表示没有重复
}
@end
main函数
#import <Foundation/Foundation.h>
#import "CZStudent.h"
#import "CZTeacher.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建方法
NSSet *set1=[NSSet setWithObjects:@"one",@"two",@"three",@"two", nil]; //标准创建
NSLog(@"标准创建:%@",set1);
NSSet *set2=[NSSet setWithSet:set1]; //副本创建
NSLog(@"副本创建:%@",set2);
NSArray *str=@[@"one",@"two",@"three",@"two"];
NSSet *set3=[NSSet setWithArray:str];//将数组转换成集合
NSLog(@"数组转换集合:%@",set3);
//自定义类的对象防止重复,需要在类中重写方法
CZStudent* s1=[CZStudent studentWithName:@"张三" andAge:18];
CZStudent* s2=[CZStudent studentWithName:@"李四" andAge:20];
CZStudent* s3=[CZStudent studentWithName:@"王五" andAge:19];
CZStudent *s4=[CZStudent studentWithName:@"张三" andAge:18];
NSSet *set4=[NSSet setWithObjects:s1,s2,s3,s4, nil];
NSLog(@"自定义类的对象防重复set4:%@",set4);
//判断集合是否有指定元素
if([set1 containsObject:@"two"] ==YES)
{
NSLog(@"集合set1中包含@\"two@\"");
}
//判断两个集合是否相等
if([set1 isEqualToSet:set2]==YES)
{
NSLog(@"set1与set2相等");
}
//判断某集合是否为另一个集合的子集
NSSet *set6=[NSSet setWithObjects:@"one",@"three", nil];
if([set6 isSubsetOfSet:set1]==YES)
{
NSLog(@"set6是set1的子集");
}
//将集合转换成数组
NSArray *array=[set1 allObjects];
NSLog(@"集合转换数组:%@",array);
//遍历
NSArray *array1=[set1 allObjects];
for(int i=0;i<set1.count;i++)//面向过程遍历需要先将集合转换为数组
{
NSLog(@"面向过程遍历:%@",array1[i]);
}
NSLog(@"----------");
for(id obj in set1) //面向对象遍历
{
NSLog(@"面向对象遍历:%@",obj);
}
NSEnumerator *e=[set1 objectEnumerator]; //枚举器遍历
while(str =[e nextObject])//nextObject :每循环一次,取一个元素
{
NSLog(@"枚举器遍历:%@",str);
}
}
return 0;
}
运行结果
2016-08-29 11:10:30.852 cz_day22_01[1690:86427] 标准创建:{(
one,
three,
two
)}
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 副本创建:{(
one,
three,
two
)}
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 数组转换集合:{(
one,
three,
two
)}
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 自定义类的对象防重复set4:{(
name:张三,age:18,
name:王五,age:19,
name:李四,age:20
)}
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 集合set1中包含@"two@"
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] set1与set2相等
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] set6是set1的子集
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 集合转换数组:(
one,
three,
two
)
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 面向过程遍历:one
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 面向过程遍历:three
2016-08-29 11:10:30.853 cz_day22_01[1690:86427] 面向过程遍历:two
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 面向对象遍历:one
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 面向对象遍历:three
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 面向对象遍历:two
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 枚举器遍历:one
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 枚举器遍历:three
2016-08-29 11:10:30.854 cz_day22_01[1690:86427] 枚举器遍历:two
Program ended with exit code: 0
2.NSMutableSet
2.1 是可变集合,是NSSet的子类
2.2 创建方法(3种)
2.3 添加方法
2.4 删除方法
main函数
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建方法
NSMutableSet *set1=[NSMutableSet set]; //空集合,有意义
NSMutableSet *set2=[NSMutableSet setWithCapacity:100]; //预估值
NSMutableSet *set3=[NSMutableSet setWithObjects:@"one",@"two",@"three", nil]; //初始化
NSLog(@"初始化创建:%@",set3);
//添加元素方法
[set3 addObject:@"four"]; //,单个添加,会自动过滤
[set3 addObject:@"four"];
NSLog(@"添加元素:%@",set3);
NSArray *array =@[@"five",@"six"];
[set3 addObjectsFromArray:array]; //批量添加,自动过滤
NSLog(@"批量添加:%@",set3);
//删除方法
[set3 removeObject:@"three"]; //删除指定元素
NSLog(@"删除指定元素:%@",set3);
[set3 removeAllObjects]; //清空集合
NSLog(@"清空集合:%@",set3);
//交集运算方法
NSArray *added1=@[@"one",@"two",@"three"];
[set2 removeAllObjects];
[set2 addObjectsFromArray:added1];
NSArray *added2=@[@"one",@"three",@"four"];
[set3 addObjectsFromArray:added2];
[set2 intersectSet:set3]; //求交集,交集的结果放回set2;
NSLog(@"交集set2:%@",set2);
//并集的运算方法
[set2 unionSet:set3];
NSLog(@"并集set2:%@",set2);
//从一个集合中删除另一个集合的元素
[set2 removeAllObjects];
[set2 addObjectsFromArray:added1];
[set3 removeAllObjects];
[set3 addObjectsFromArray:added2];
[set2 minusSet:set3];
NSLog(@"删除另一个集合的元素:%@",set2);
}
return 0;
}
运行结果
2016-08-29 14:53:52.201 cz_day22_03[2654:172099] 初始化创建:{(
one,
two,
three
)}
2016-08-29 14:53:52.202 cz_day22_03[2654:172099] 添加元素:{(
one,
three,
two,
four
)}
2016-08-29 14:53:52.202 cz_day22_03[2654:172099] 批量添加:{(
one,
five,
three,
two,
four,
six
)}
2016-08-29 14:53:52.202 cz_day22_03[2654:172099] 删除指定元素:{(
one,
five,
two,
four,
six
)}
2016-08-29 14:53:52.202 cz_day22_03[2654:172099] 清空集合:{(
)}
2016-08-29 14:53:52.202 cz_day22_03[2654:172099] 交集set2:{(
one,
three
)}
2016-08-29 14:53:52.203 cz_day22_03[2654:172099] 并集set2:{(
one,
four,
three
)}
2016-08-29 14:53:52.203 cz_day22_03[2654:172099] 删除另一个集合的元素:{(
two
)}
Program ended with exit code: 0