简体   繁体   English

查找NSMutableArrays的交集

[英]Finding Intersection of NSMutableArrays

I have three NSMutableArray containing names that are added to the lists according to different criterieas. 我有三个NSMutableArray包含根据不同的标准添加到列表中的名称。

Here are my arrays pseudocode: 这是我的数组伪代码:

NSMutableArray *array1 = [@"Jack", @"John", @"Daniel", @"Lisa"];
NSMutableArray *array2 = [@"Jack", @"Bryan", @"Barney", @"Lisa",@"Penelope",@"Angelica"];
NSMutableArray *array3 = [@"Jack", @"Jerome", @"Dan", @"Lindsay", @"Lisa"];

I want to find a fourth array which includes the intersection of those three arrays. 我想找到第四个数组,其中包括这三个数组的交集。 In this case for example it will be: 在这种情况下,例如它将是:

NSMutableArray *array4 = [@"Jack",@"Lisa"];

Because all the three array have jack and lisa as an element. 因为所有三个阵列都有jack和lisa作为元素。 Is there way of simply doing this? 有没有办法简单地这样做?

Use NSMutableSet : 使用NSMutableSet

NSMutableSet *intersection = [NSMutableSet setWithArray:array1];
[intersection intersectSet:[NSSet setWithArray:array2]];
[intersection intersectSet:[NSSet setWithArray:array3]];

NSArray *array4 = [intersection allObjects];

The only issue with this is that you lose ordering of elements, but I think (in this case) that that's OK. 唯一的问题是你丢失了元素的排序,但我认为(在这种情况下)那没关系。


As has been pointed out in the comments (thanks, Q80 !), iOS 5 and OS X 10.7 added a new class called NSOrderedSet (with a Mutable subclass) that allows you to perform these same intersection operations while still maintaining order. 正如评论中指出的那样(感谢, Q80 !),iOS 5和OS X 10.7增加了一个名为NSOrderedSet (带有Mutable子类)的新类,它允许您在保持顺序的同时执行这些相同的交集操作。

Have a look at this post . 看看这篇文章

In short: if you can use NSSet instead of NSArray, then it's trivial (NSMutableSet has intersectSet: ). 简而言之:如果你可以使用NSSet而不是NSArray,那么它是微不足道的(NSMutableSet有intersectSet: :)。

Otherwise, you can build an NSSet from your NSArray and go back to the above case. 否则,您可以从NSArray构建NSSet并返回上述情况。

This is cleaner than the NSSet approach, and you won't lose the original ordering. 这比NSSet方法更干净,您不会丢失原始排序。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@ AND self IN %@", array2, array3];
NSArray *array4 = [array1 filteredArrayUsingPredicate:predicate];
NSMutableArray *first = [[NSMutableArray alloc] initWithObjects:@"Jack", @"John", @"Daniel", @"Lisa",nil];

NSMutableArray *seconds =[[NSMutableArray alloc] initWithObjects:@"Jack", @"Bryan", @"Barney", @"Lisa",@"Penelope",@"Angelica",nil];

NSMutableArray *third = [ [ NSMutableArray alloc]init];


for (id obj in first) {

    if ([seconds  containsObject:obj] ) {


        [third addObject:obj];

    }


}


NSLog(@"third is : %@ \n\n",third);

OUTPUT: OUTPUT:

third is : ( 第三是:(

Jack,

Lisa

)

Here is a working variant from links above 以下是上述链接的工作变体

NSPredicate *intersectPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@500, @400, @600]];
NSArray *intersect = [@[@200, @300, @400] filteredArrayUsingPredicate:intersectPredicate];

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

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