Linq .Any VS .存在-区别是什么?

时间:2022-10-19 15:07:18

Using Linq on collections, what is the difference between the following lines of code?

在集合中使用Linq,以下代码行之间的区别是什么?

if(!coll.Any(i => i.Value))

and

if(!coll.Exists(i => i.Value))

Update 1

更新1

When I disassemble .Exists it looks like there is no code.

当我分解。exist时,它看起来没有代码。

Update 2

更新2

Anyone know why there is not code there for this one?

有人知道为什么这里没有代码吗?

6 个解决方案

#1


344  

See documentation

看文档

List.Exists (Object method - MSDN)

列表。存在(对象方法- MSDN)

Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.

确定列表(T)是否包含符合指定谓词定义的条件的元素。

This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList)

这在。net 2.0之后就存在了,在LINQ之前也是如此。表示与谓词委托一起使用,但lambda表达式是向后兼容的。同样,List也有这个(甚至不是IList)

IEnumerable.Any (Extension method - MSDN)

IEnumerable。Any(扩展方法- MSDN)

Determines whether any element of a sequence satisfies a condition.

确定序列中的任何元素是否满足条件。

This is new in .NET 3.5 and uses Func(TSource, bool) as argument, so this was intended to be used with lambda expressions and LINQ.

这在。net 3.5中是全新的,并且使用Func(TSource, bool)作为参数,因此这将用于lambda表达式和LINQ。

In behaviour, these are identical.

在行为上,它们是相同的。

#2


175  

The difference is that Any is an extension method for any IEnumerable<T> defined on System.Linq.Enumerable. It can be used on any IEnumerable<T> instance.

不同之处在于Any是在System.Linq.Enumerable. enumerable . enumerable中定义的任何IEnumerable 的扩展方法。它可以用于任何IEnumerable 实例。

Exists does not appear to be an extension method. My guess is that coll is of type List<T>. If so Exists is an instance method which functions very similar to Any.

exist似乎不是扩展方法。我猜coll是类型列表 。如果存在,则是一个实例方法,其功能与任何方法都非常相似。

In short, the methods are essentially the same. One is more general than the other.

总之,这些方法本质上是相同的。一个比另一个更普遍。

  • Any also has an overload which takes no parameters and simply looks for any item in the enumerable.
  • Any也有一个不带参数的重载,只在可枚举项中查找任何项。
  • Exists has no such overload.
  • 存在没有这样的超负荷。

#3


39  

TLDR; Performance-wise Any seems to be slower (if I have set this up properly to evaluate both values at almost same time)

TLDR;就性能而言,任何一种似乎都比较慢(如果我正确地设置它,几乎同时评估两个值)

        var list1 = Generate(1000000);
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;
            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s +=" Any: " +end1.Subtract(start1);
            }

            if (!s.Contains("sdfsd"))
            {

            }

testing list generator:

测试生成器列表:

private List<string> Generate(int count)
    {
        var list = new List<string>();
        for (int i = 0; i < count; i++)
        {
            list.Add( new string(
            Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 13)
                .Select(s =>
                {
                    var cryptoResult = new byte[4];
                    new RNGCryptoServiceProvider().GetBytes(cryptoResult);
                    return s[new Random(BitConverter.ToInt32(cryptoResult, 0)).Next(s.Length)];
                })
                .ToArray())); 
        }

        return list;
    }

With 10M records

与10米记录

" Any: 00:00:00.3770377 Exists: 00:00:00.2490249"

" Any: 00:00:01 .37 .存在的

With 5M records

与5米记录

" Any: 00:00:00.0940094 Exists: 00:00:00.1420142"

"任何:00:00.0940094存在:00:00:00.1420142"

With 1M records

与1 m记录

" Any: 00:00:00.0180018 Exists: 00:00:00.0090009"

" Any: 00:00:01 .0180018 exist: 00:00:00.0090009"

With 500k, (I also flipped around order in which they get evaluated to see if there is no additional operation associated with whichever runs first.)

对于500k,(我还对它们的计算顺序进行了翻转,以查看是否没有与第一次运行的操作相关的附加操作)。

" Exists: 00:00:00.0050005 Any: 00:00:00.0100010"

"存在"

With 100k records

与100 k记录

" Exists: 00:00:00.0010001 Any: 00:00:00.0020002"

"存在"

It would seem Any to be slower by magnitude of 2.

它的速度似乎比2要慢。

Edit: For 5 and 10M records I changed the way it generates the list and Exists suddenly became slower than Any which implies there's something wrong in the way I am testing.

编辑:对于5条和10万条记录,我改变了它生成列表的方式,它的存在突然变得比任何提示我正在测试的方式有问题的记录都要慢。

New testing mechanism:

新测试机制:

private static IEnumerable<string> Generate(int count)
    {
        var cripto = new RNGCryptoServiceProvider();
        Func<string> getString = () => new string(
            Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 13)
                .Select(s =>
                {
                    var cryptoResult = new byte[4];
                    cripto.GetBytes(cryptoResult);
                    return s[new Random(BitConverter.ToInt32(cryptoResult, 0)).Next(s.Length)];
                })
                .ToArray());

        var list = new ConcurrentBag<string>();
        var x = Parallel.For(0, count, o => list.Add(getString()));
        return list;
    }

    private static void Test()
    {
        var list = Generate(10000000);
        var list1 = list.ToList();
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;

            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s += " Any: " + end1.Subtract(start1);
            }

            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            if (!s.Contains("sdfsd"))
            {

            }
        }

Edit2: Ok so to eliminate any influence from generating test data I wrote it all to file and now read it from there.

为了消除生成测试数据所带来的影响,我把这些都写进了文件中,然后从那里开始读。

 private static void Test()
    {
        var list1 = File.ReadAllLines("test.txt").Take(500000).ToList();
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;
            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s += " Any: " + end1.Subtract(start1);
            }

            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            if (!s.Contains("sdfsd"))
            {
            }
        }
    }

10M

10米

" Any: 00:00:00.1640164 Exists: 00:00:00.0750075"

" Any: 00:01 .1640164 exist: 00:00:01 .0750075"

5M

5米

" Any: 00:00:00.0810081 Exists: 00:00:00.0360036"

存在

1M

1米

" Any: 00:00:00.0190019 Exists: 00:00:00.0070007"

" Any: 00:00:01 .0190019 exist: 00:00:00.0070007"

500k

500 k

" Any: 00:00:00.0120012 Exists: 00:00:00.0040004"

" Any: 00:00:00:01 . 02存在:00:00:00.0040004"

Linq .Any VS .存在-区别是什么?

#4


8  

As a continuation on Matas' answer on benchmarking.

作为Matas关于基准测试的回答的延续。

TL/DR: Exists() and Any() are equally fast.

TL/DR: exist()和Any()同样快。

First off: Benchmarking using Stopwatch is not precise (see series0ne's answer on a different, but similiar, topic), but it is far more precise than DateTime.

首先:使用秒表进行基准测试并不精确(请参阅series0ne关于另一个不同但类似的主题的回答),但它比DateTime精确得多。

The way to get really precise readings is by using Performance Profiling. But one way to get a sense of how the two methods' performance measure up to each other is by executing both methods loads of times and then comparing the fastest execution time of each. That way, it really doesn't matter that JITing and other noise gives us bad readings (and it does), because both executions are "equally misguiding" in a sense.

获得精确读数的方法是使用性能分析。但是,有一种方法可以了解两种方法的性能度量方法是如何通过同时执行两种方法,然后比较每种方法的最快执行时间。这样一来,JITing和其他噪音给我们带来了糟糕的读数(确实如此)就不重要了,因为从某种意义上来说,这两个执行都是“同样的误导”。

static void Main(string[] args)
    {
        Console.WriteLine("Generating list...");
        List<string> list = GenerateTestList(1000000);
        var s = string.Empty;

        Stopwatch sw;
        Stopwatch sw2;
        List<long> existsTimes = new List<long>();
        List<long> anyTimes = new List<long>();

        Console.WriteLine("Executing...");
        for (int j = 0; j < 1000; j++)
        {
            sw = Stopwatch.StartNew();
            if (!list.Exists(o => o == "0123456789012"))
            {
                sw.Stop();
                existsTimes.Add(sw.ElapsedTicks);
            }
        }

        for (int j = 0; j < 1000; j++)
        {
            sw2 = Stopwatch.StartNew();
            if (!list.Exists(o => o == "0123456789012"))
            {
                sw2.Stop();
                anyTimes.Add(sw2.ElapsedTicks);
            }
        }

        long existsFastest = existsTimes.Min();
        long anyFastest = anyTimes.Min();

        Console.WriteLine(string.Format("Fastest Exists() execution: {0} ticks\nFastest Any() execution: {1} ticks", existsFastest.ToString(), anyFastest.ToString()));
        Console.WriteLine("Benchmark finished. Press any key.");
        Console.ReadKey();
    }

    public static List<string> GenerateTestList(int count)
    {
        var list = new List<string>();
        for (int i = 0; i < count; i++)
        {
            Random r = new Random();
            int it = r.Next(0, 100);
            list.Add(new string('s', it));
        }
        return list;
    }

After executing the above code 4 times (which in turn do 1 000 Exists() and Any() on a list with 1 000 000 elements), it's not hard to see that the methods are pretty much equally fast.

在执行上述代码4次之后(依次执行1000个()和一个包含1000个元素的列表中的任何()),不难看出这些方法的速度几乎是一样的快。

Fastest Exists() execution: 57881 ticks
Fastest Any() execution: 58272 ticks

Fastest Exists() execution: 58133 ticks
Fastest Any() execution: 58063 ticks

Fastest Exists() execution: 58482 ticks
Fastest Any() execution: 58982 ticks

Fastest Exists() execution: 57121 ticks
Fastest Any() execution: 57317 ticks

There is a slight difference, but it's too small a difference to not be explained by background noise. My guess would be that if one would do 10 000 or 100 000 Exists() and Any() instead, that slight difference would disappear more or less.

虽然有细微的差别,但是这个差别太小了,不能用背景噪音来解释。我的猜测是,如果一个人做10,000或100,000存在()和Any(),那么这个细微的差异将或多或少地消失。

#5


5  

When you correct the measurements - as mentioned above: Any and Exists, and adding average - we'll get following output:

当你校正测量值——如上面提到的:Any和exist,并加上average——我们将得到以下输出:

Executing search Exists() 1000 times ... 
Average Exists(): 35566,023
Fastest Exists() execution: 32226 

Executing search Any() 1000 times ... 
Average Any(): 58852,435
Fastest Any() execution: 52269 ticks

Benchmark finished. Press any key.

#6


4  

Additionally, this will only work if Value is of type bool. Normally this is used with predicates. Any predicate would be generally used find whether there is any element satisfying a given condition. Here you're just doing a map from your element i to a bool property. It will search for an "i" whose Value property is true. Once done, the method will return true.

此外,只有当值为bool类型时,该方法才有效。通常,它与谓词一起使用。任何谓词都将被广泛使用,以确定是否有满足给定条件的元素。这里你要做的是从元素i到bool属性的映射。它将搜索值属性为true的“i”。一旦完成,该方法将返回true。

#1


344  

See documentation

看文档

List.Exists (Object method - MSDN)

列表。存在(对象方法- MSDN)

Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.

确定列表(T)是否包含符合指定谓词定义的条件的元素。

This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList)

这在。net 2.0之后就存在了,在LINQ之前也是如此。表示与谓词委托一起使用,但lambda表达式是向后兼容的。同样,List也有这个(甚至不是IList)

IEnumerable.Any (Extension method - MSDN)

IEnumerable。Any(扩展方法- MSDN)

Determines whether any element of a sequence satisfies a condition.

确定序列中的任何元素是否满足条件。

This is new in .NET 3.5 and uses Func(TSource, bool) as argument, so this was intended to be used with lambda expressions and LINQ.

这在。net 3.5中是全新的,并且使用Func(TSource, bool)作为参数,因此这将用于lambda表达式和LINQ。

In behaviour, these are identical.

在行为上,它们是相同的。

#2


175  

The difference is that Any is an extension method for any IEnumerable<T> defined on System.Linq.Enumerable. It can be used on any IEnumerable<T> instance.

不同之处在于Any是在System.Linq.Enumerable. enumerable . enumerable中定义的任何IEnumerable 的扩展方法。它可以用于任何IEnumerable 实例。

Exists does not appear to be an extension method. My guess is that coll is of type List<T>. If so Exists is an instance method which functions very similar to Any.

exist似乎不是扩展方法。我猜coll是类型列表 。如果存在,则是一个实例方法,其功能与任何方法都非常相似。

In short, the methods are essentially the same. One is more general than the other.

总之,这些方法本质上是相同的。一个比另一个更普遍。

  • Any also has an overload which takes no parameters and simply looks for any item in the enumerable.
  • Any也有一个不带参数的重载,只在可枚举项中查找任何项。
  • Exists has no such overload.
  • 存在没有这样的超负荷。

#3


39  

TLDR; Performance-wise Any seems to be slower (if I have set this up properly to evaluate both values at almost same time)

TLDR;就性能而言,任何一种似乎都比较慢(如果我正确地设置它,几乎同时评估两个值)

        var list1 = Generate(1000000);
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;
            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s +=" Any: " +end1.Subtract(start1);
            }

            if (!s.Contains("sdfsd"))
            {

            }

testing list generator:

测试生成器列表:

private List<string> Generate(int count)
    {
        var list = new List<string>();
        for (int i = 0; i < count; i++)
        {
            list.Add( new string(
            Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 13)
                .Select(s =>
                {
                    var cryptoResult = new byte[4];
                    new RNGCryptoServiceProvider().GetBytes(cryptoResult);
                    return s[new Random(BitConverter.ToInt32(cryptoResult, 0)).Next(s.Length)];
                })
                .ToArray())); 
        }

        return list;
    }

With 10M records

与10米记录

" Any: 00:00:00.3770377 Exists: 00:00:00.2490249"

" Any: 00:00:01 .37 .存在的

With 5M records

与5米记录

" Any: 00:00:00.0940094 Exists: 00:00:00.1420142"

"任何:00:00.0940094存在:00:00:00.1420142"

With 1M records

与1 m记录

" Any: 00:00:00.0180018 Exists: 00:00:00.0090009"

" Any: 00:00:01 .0180018 exist: 00:00:00.0090009"

With 500k, (I also flipped around order in which they get evaluated to see if there is no additional operation associated with whichever runs first.)

对于500k,(我还对它们的计算顺序进行了翻转,以查看是否没有与第一次运行的操作相关的附加操作)。

" Exists: 00:00:00.0050005 Any: 00:00:00.0100010"

"存在"

With 100k records

与100 k记录

" Exists: 00:00:00.0010001 Any: 00:00:00.0020002"

"存在"

It would seem Any to be slower by magnitude of 2.

它的速度似乎比2要慢。

Edit: For 5 and 10M records I changed the way it generates the list and Exists suddenly became slower than Any which implies there's something wrong in the way I am testing.

编辑:对于5条和10万条记录,我改变了它生成列表的方式,它的存在突然变得比任何提示我正在测试的方式有问题的记录都要慢。

New testing mechanism:

新测试机制:

private static IEnumerable<string> Generate(int count)
    {
        var cripto = new RNGCryptoServiceProvider();
        Func<string> getString = () => new string(
            Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 13)
                .Select(s =>
                {
                    var cryptoResult = new byte[4];
                    cripto.GetBytes(cryptoResult);
                    return s[new Random(BitConverter.ToInt32(cryptoResult, 0)).Next(s.Length)];
                })
                .ToArray());

        var list = new ConcurrentBag<string>();
        var x = Parallel.For(0, count, o => list.Add(getString()));
        return list;
    }

    private static void Test()
    {
        var list = Generate(10000000);
        var list1 = list.ToList();
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;

            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s += " Any: " + end1.Subtract(start1);
            }

            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            if (!s.Contains("sdfsd"))
            {

            }
        }

Edit2: Ok so to eliminate any influence from generating test data I wrote it all to file and now read it from there.

为了消除生成测试数据所带来的影响,我把这些都写进了文件中,然后从那里开始读。

 private static void Test()
    {
        var list1 = File.ReadAllLines("test.txt").Take(500000).ToList();
        var forceListEval = list1.SingleOrDefault(o => o == "0123456789012");
        if (forceListEval != "sdsdf")
        {
            var s = string.Empty;
            var start1 = DateTime.Now;
            if (!list1.Any(o => o == "0123456789012"))
            {
                var end1 = DateTime.Now;
                s += " Any: " + end1.Subtract(start1);
            }

            var start2 = DateTime.Now;
            if (!list1.Exists(o => o == "0123456789012"))
            {
                var end2 = DateTime.Now;
                s += " Exists: " + end2.Subtract(start2);
            }

            if (!s.Contains("sdfsd"))
            {
            }
        }
    }

10M

10米

" Any: 00:00:00.1640164 Exists: 00:00:00.0750075"

" Any: 00:01 .1640164 exist: 00:00:01 .0750075"

5M

5米

" Any: 00:00:00.0810081 Exists: 00:00:00.0360036"

存在

1M

1米

" Any: 00:00:00.0190019 Exists: 00:00:00.0070007"

" Any: 00:00:01 .0190019 exist: 00:00:00.0070007"

500k

500 k

" Any: 00:00:00.0120012 Exists: 00:00:00.0040004"

" Any: 00:00:00:01 . 02存在:00:00:00.0040004"

Linq .Any VS .存在-区别是什么?

#4


8  

As a continuation on Matas' answer on benchmarking.

作为Matas关于基准测试的回答的延续。

TL/DR: Exists() and Any() are equally fast.

TL/DR: exist()和Any()同样快。

First off: Benchmarking using Stopwatch is not precise (see series0ne's answer on a different, but similiar, topic), but it is far more precise than DateTime.

首先:使用秒表进行基准测试并不精确(请参阅series0ne关于另一个不同但类似的主题的回答),但它比DateTime精确得多。

The way to get really precise readings is by using Performance Profiling. But one way to get a sense of how the two methods' performance measure up to each other is by executing both methods loads of times and then comparing the fastest execution time of each. That way, it really doesn't matter that JITing and other noise gives us bad readings (and it does), because both executions are "equally misguiding" in a sense.

获得精确读数的方法是使用性能分析。但是,有一种方法可以了解两种方法的性能度量方法是如何通过同时执行两种方法,然后比较每种方法的最快执行时间。这样一来,JITing和其他噪音给我们带来了糟糕的读数(确实如此)就不重要了,因为从某种意义上来说,这两个执行都是“同样的误导”。

static void Main(string[] args)
    {
        Console.WriteLine("Generating list...");
        List<string> list = GenerateTestList(1000000);
        var s = string.Empty;

        Stopwatch sw;
        Stopwatch sw2;
        List<long> existsTimes = new List<long>();
        List<long> anyTimes = new List<long>();

        Console.WriteLine("Executing...");
        for (int j = 0; j < 1000; j++)
        {
            sw = Stopwatch.StartNew();
            if (!list.Exists(o => o == "0123456789012"))
            {
                sw.Stop();
                existsTimes.Add(sw.ElapsedTicks);
            }
        }

        for (int j = 0; j < 1000; j++)
        {
            sw2 = Stopwatch.StartNew();
            if (!list.Exists(o => o == "0123456789012"))
            {
                sw2.Stop();
                anyTimes.Add(sw2.ElapsedTicks);
            }
        }

        long existsFastest = existsTimes.Min();
        long anyFastest = anyTimes.Min();

        Console.WriteLine(string.Format("Fastest Exists() execution: {0} ticks\nFastest Any() execution: {1} ticks", existsFastest.ToString(), anyFastest.ToString()));
        Console.WriteLine("Benchmark finished. Press any key.");
        Console.ReadKey();
    }

    public static List<string> GenerateTestList(int count)
    {
        var list = new List<string>();
        for (int i = 0; i < count; i++)
        {
            Random r = new Random();
            int it = r.Next(0, 100);
            list.Add(new string('s', it));
        }
        return list;
    }

After executing the above code 4 times (which in turn do 1 000 Exists() and Any() on a list with 1 000 000 elements), it's not hard to see that the methods are pretty much equally fast.

在执行上述代码4次之后(依次执行1000个()和一个包含1000个元素的列表中的任何()),不难看出这些方法的速度几乎是一样的快。

Fastest Exists() execution: 57881 ticks
Fastest Any() execution: 58272 ticks

Fastest Exists() execution: 58133 ticks
Fastest Any() execution: 58063 ticks

Fastest Exists() execution: 58482 ticks
Fastest Any() execution: 58982 ticks

Fastest Exists() execution: 57121 ticks
Fastest Any() execution: 57317 ticks

There is a slight difference, but it's too small a difference to not be explained by background noise. My guess would be that if one would do 10 000 or 100 000 Exists() and Any() instead, that slight difference would disappear more or less.

虽然有细微的差别,但是这个差别太小了,不能用背景噪音来解释。我的猜测是,如果一个人做10,000或100,000存在()和Any(),那么这个细微的差异将或多或少地消失。

#5


5  

When you correct the measurements - as mentioned above: Any and Exists, and adding average - we'll get following output:

当你校正测量值——如上面提到的:Any和exist,并加上average——我们将得到以下输出:

Executing search Exists() 1000 times ... 
Average Exists(): 35566,023
Fastest Exists() execution: 32226 

Executing search Any() 1000 times ... 
Average Any(): 58852,435
Fastest Any() execution: 52269 ticks

Benchmark finished. Press any key.

#6


4  

Additionally, this will only work if Value is of type bool. Normally this is used with predicates. Any predicate would be generally used find whether there is any element satisfying a given condition. Here you're just doing a map from your element i to a bool property. It will search for an "i" whose Value property is true. Once done, the method will return true.

此外,只有当值为bool类型时,该方法才有效。通常,它与谓词一起使用。任何谓词都将被广泛使用,以确定是否有满足给定条件的元素。这里你要做的是从元素i到bool属性的映射。它将搜索值属性为true的“i”。一旦完成,该方法将返回true。