NSSet、NSMutableSet、NSOrderedSet、NSMutableOrderedSet

NSSet、NSMutableSet是无序的,但是它可以保证数据的唯一性。当插入相同数据时,不会有任何的效果。从内部的实现来说是hash表,所以可以常数时间内查找到一个数据。

NSOrderedSet、NSMutableOrderedSet是有序的。

    NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"8",@"6",@"7",@"4", nil];
    NSLog(@"%@", set);
    // 获取set中的数据
    NSArray *arr1 = [set allObjects];
    NSLog(@"%@", arr1);

// 打印结果:
2017-02-28 15:53:44.359 UsingWebView[83398:10002574] {(
    7,
    3,
    8,
    4,
    9,
    5,
    1,
    6,
    2
)}
2017-02-28 15:53:44.360 UsingWebView[83398:10002574] (
    7,
    3,
    8,
    4,
    9,
    5,
    1,
    6,
    2
)
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];

    NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] init];
    for (NSString *value in array) {
        [mutableOrderSet addObject:value];
    }
    NSLog(@"%@", mutableOrderSet);
    
    NSEnumerator *enumerator = [mutableOrderSet objectEnumerator];
    for (NSString *value in enumerator) {
        NSLog(@"%@", value);
    }
    
    NSArray *arr2 = [mutableOrderSet array];
    NSLog(@"%@",arr2);
    
    NSSet *set = [mutableOrderSet set];
    NSLog(@"%@", set);

// 打印结果:
2017-02-28 15:57:17.184 UsingWebView[83465:10010708] {(
    1,
    9,
    3,
    2,
    5,
    6,
    4
)}
2017-02-28 15:57:17.186 UsingWebView[83465:10010708] 1
2017-02-28 15:57:17.186 UsingWebView[83465:10010708] 9
2017-02-28 15:57:17.187 UsingWebView[83465:10010708] 3
2017-02-28 15:57:17.188 UsingWebView[83465:10010708] 2
2017-02-28 15:57:17.189 UsingWebView[83465:10010708] 5
2017-02-28 15:57:17.192 UsingWebView[83465:10010708] 6
2017-02-28 15:57:17.194 UsingWebView[83465:10010708] 4
2017-02-28 15:57:17.194 UsingWebView[83465:10010708] (
    1,
    9,
    3,
    2,
    5,
    6,
    4
)
2017-02-28 15:57:17.195 UsingWebView[83465:10010708] {(
    1,
    9,
    3,
    2,
    5,
    6,
    4
)}

  NSMutableSet、NSMutableOrderedSet中的三个方法

// 两个集合的交集
- (void)intersectSet:(NSSet<ObjectType> *)other;
//  向集合中删除other集合中的所有元素
- (void)minusSet:(NSSet<ObjectType> *)other;
//  向集合中添加other集合中的所有元素 
- (void)unionSet:(NSSet<ObjectType> *)other;
    NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
    NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
    
    [mutableOrderSet intersectSet:set];    
    NSLog(@"%@", mutableOrderSet);

// 返回结果
2017-02-28 16:06:10.558 UsingWebView[83631:10032695] {(
    1,
    9,
    3
)}
    NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
    NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
    
    [mutableOrderSet minusSet:set];    
    NSLog(@"%@", mutableOrderSet);

//返回结果
2017-02-28 16:07:52.490 UsingWebView[83671:10036861] {(
    2,
    5,
    6,
    4
)}
    NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
    NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
    
    [mutableOrderSet unionSet:set];
    NSLog(@"%@", mutableOrderSet);

// 返回结果
2017-02-28 16:08:44.067 UsingWebView[83700:10039896] {(
    1,
    9,
    3,
    2,
    5,
    6,
    4,
    0
)}

 

 

posted @ 2017-02-28 16:09  紫洁  阅读(1455)  评论(0编辑  收藏  举报