我应该如何在两个列表比较值? [英] How should I compare values in two lists?

查看:115
本文介绍了我应该如何在两个列表比较值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名单

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, E }



我需要检查的一个元素清单02 存在于清单01 ,所以下面应该是

I need to check if one element of List 02 exists in List 01, so the following should be false.

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, F } // no element matches



在这里,它应该是真正

List 01 => { A, B, C, D, E }
List 02 => { F, F, F, F, B } // last element matches



我如何检查?

How can I check that?

我很担心性能以及

推荐答案

有几种不同的方式做到这一点:

There are a few different ways to do it:

如果交集结果1的结果。或以上的元素,这意味着至少有一个等于元件

If the result of the intersection result in 1 or more elements, it means that at least one equal element.

var result = list01.Intersect(list02);
bool hasElement = result.Any();



我建议使用此方法。

I recommend the use of this method.

有可能通过一个的IEqualityComparer< T> 作为的情况下,第二个参数,你需要比较复杂的类型

It is possible to pass a IEqualityComparer<T> as the second parameter in case you need to compare complex types.

如果一个除了具有不同的量在总的元素,这意味着有至少一个等于元件的结果。

If the result of a except has different amount of elements in total, it means that there is at least one equal element.

var result = list01.Except(list02);
bool hasElement = result.Count() != list01.Count;

有可能通过一个的IEqualityComparer< T> 在情况下,第二个参数,你需要比较复杂的类型。

It is possible to pass a IEqualityComparer<T> as the second parameter in case you need to compare complex types.

如果在list01任何元素等于中的任何元素。在list02,就意味着有至少一个等于元件

If any element in the list01 is equal any element in the list02, it means that there is at least one equal element.

bool hasElement = list01.Any(e => list02.Any(o => o == e));



任何Ë的的IndexOf



如果在list01任何元件在list02被找到,这意味着存在于。租赁1等于元素

Any e IndexOf

If any element in the list01 is found in list02, it means that there is at lease one equal element.

bool hasElement = list01.Any(e => list02.IndexOf(e) != -1);



的IndexOf 的缺点是,你可以'T传递的IEqualityComparer< T> ,而是将始终使用默认值, EqualityComparer< T> .DEFAULT

The disadvantage of IndexOf is that you can't pass a IEqualityComparer<T>, instead it will always use the default, EqualityComparer<T>.Default.

在一个大名单, list01.Any(E => list02.Any(O =≠0 == E))将有上佳表现的仅在的一个值从第一开头在包含在第二列表中。否则将表现糟糕,因为迭代是连续

In a big list, list01.Any(e => list02.Any(o => o == e)) will have a good performance only if one of the values from the beginning of the first in contained in the second list. Otherwise the performance will be awful, as the iterations are sequential.

在性能测试中,我得到了以下结果:

In a performance test I got the following results:

具有5个元素列出每个,测试千万次

Lists with 5 elements each, tested 10000000 times.

Intersect     : 00:00:02.9260135
Except        : 00:00:03.4404527
AnyAny        : 00:00:06.5709693
AnyIndexOf    : 00:00:01.9882278

100000元素列出每个,测试500次。 list02的最后一个元素是等于list01第三个元素:

Lists with 100000 elements each, tested 500 times. The last element of list02 is equal to the third element in list01:

Intersect     : 00:00:02.4397784
Except        : 00:00:04.2595364
AnyAny        : 00:00:02.9761128
AnyIndexOf    : 00:00:00.0919344

100000元素列出每个,测试500次。 。list02的最后一个元素是等于list01最后一个元素

Lists with 100000 elements each, tested 500 times. The last element of list02 is equal to the last element in list01.

Intersect     : 00:00:02.4927969
Except        : 00:00:04.2668677
AnyAny        : more than a minute and I dropped the test
AnyIndexOf    : more than a minute and I dropped the test

这篇关于我应该如何在两个列表比较值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆