当前位置: 首页>>代码示例>>C#>>正文


C# HashedSet.IntersectWith方法代码示例

本文整理汇总了C#中HashedSet.IntersectWith方法的典型用法代码示例。如果您正苦于以下问题:C# HashedSet.IntersectWith方法的具体用法?C# HashedSet.IntersectWith怎么用?C# HashedSet.IntersectWith使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HashedSet的用法示例。


在下文中一共展示了HashedSet.IntersectWith方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

    static void Main()
    {
        HashedSet<int> set = new HashedSet<int>();
        set.Add(5);
        set.Add(5);
        set.Add(6);
        set.Add(1);
        set.Add(3);
        set.Add(3);
        set.Add(8);
        set.Remove(6);

        Console.WriteLine("First set contains 6 - {0}", set.Contains(6));
        Console.WriteLine("First set contains 8 - {0}", set.Contains(8));

        Console.WriteLine("First set");
        foreach (var item in set)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        Console.WriteLine("Second set");
        HashedSet<int> set2 = new HashedSet<int>();
        set2.Add(13);
        set2.Add(3);
        set2.Add(1);
        foreach (var item in set2)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet<int> union = set.UnionWith(set2);
        Console.WriteLine("Union: ");
        foreach (var item in union)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();

        HashedSet<int> intersection = set.IntersectWith(set2);
        Console.WriteLine("Intersection: ");
        foreach (var item in intersection)
        {
            Console.Write("{0} ", item);
        }
        Console.WriteLine();
    }
开发者ID:bahtev,项目名称:TelerikAcademy,代码行数:49,代码来源:Program.cs

示例2: TestIntersectMethod

        public void TestIntersectMethod()
        {
            var hashedSet = new HashedSet<int>();

            Assert.IsTrue(hashedSet.Add(1));
            Assert.IsTrue(hashedSet.Add(2));
            Assert.IsTrue(hashedSet.Add(3));
            Assert.IsTrue(hashedSet.Add(4));
            Assert.AreEqual(4, hashedSet.Count);

            int[] intersectArray = { 2, 3 };
            hashedSet.IntersectWith(intersectArray);

            Assert.AreEqual(2, hashedSet.Count);
            CollectionAssert.AreEqual(hashedSet.Keys.ToList(), new List<int>() { 2, 3 });
        }
开发者ID:g-yonchev,项目名称:Telerik-Academy,代码行数:16,代码来源:05.HashSetTests.cs

示例3: Main

    static void Main()
    {
        HashedSet<int> hashSet1 = new HashedSet<int>();

        for (int i = 0; i < 10; i++)
        {
            hashSet1.Add(i);
        }

        Console.WriteLine("Find element with key = 4");
        Console.WriteLine(hashSet1.Find(4));

        Console.WriteLine("Find element with key = 2 and write the count of elements");
        hashSet1.Remove(2);
        Console.WriteLine(hashSet1.Count);

        HashedSet<int> hashSet2 = new HashedSet<int>();
        hashSet2.Add(5);
        hashSet2.Add(9);
        hashSet2.Add(33);

        Console.WriteLine("Union: ");
        hashSet1.UnionWith(hashSet2);
        for (int i = 0; i < hashSet1.Container.Count; i++)
        {
            Console.WriteLine(hashSet1.Container.Keys[i]);
        }
        Console.WriteLine();

        Console.WriteLine("Intersect: ");
        hashSet1.IntersectWith(hashSet2);

        for (int i = 0; i < hashSet1.Container.Count; i++)
        {
            Console.WriteLine(hashSet1.Container.Keys[i]);
        }
        Console.WriteLine();

        Console.WriteLine("count after clear");
        hashSet2.Clear();
        Console.WriteLine(hashSet2.Container.Count);
    }
开发者ID:hristo11111,项目名称:TelerikAcademy-HristoBratanov,代码行数:42,代码来源:TestHashSet.cs

示例4: IntersectShouldReturnSetWithElementsDoubledElelemt

        public void IntersectShouldReturnSetWithElementsDoubledElelemt()
        {
            var firstSet = new HashedSet<int>();
            var secondSet = new HashedSet<int>();
            for (int i = 0; i < 10; i++)
            {
                if (i < 7)
                {
                    firstSet.Add(i);
                }

                if (i > 3)
                {
                    secondSet.Add(i);
                }
            }

            var result = firstSet.IntersectWith(secondSet);

            Assert.AreEqual(3, result.Count);

            foreach (var item in result)
            {
                Assert.IsTrue(item >= 3 && item < 7);
            }
        }
开发者ID:NikitoG,项目名称:TelerikAcademyHomeworks,代码行数:26,代码来源:HashSetTests.cs

示例5: Intersection

        public void Intersection()
        {
            int[] oddNumbers = new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
            int[] digits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            HashedSet<int> setOdds = new HashedSet<int>(oddNumbers);
            HashedSet<int> setDigits = new HashedSet<int>(digits);

            setOdds.IntersectWith(setDigits);

            int[] expectedArray = new int[] { 1, 3, 5, 7, 9 };

            int[] actualArray = setOdds.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds = new HashedSet<int>(oddNumbers);

            setDigits.IntersectWith(setOdds);

            actualArray = setDigits.ToArray();
            Array.Sort(actualArray);
            CollectionAssert.AreEqual(expectedArray, actualArray);

            setOdds.IntersectWith(setOdds);
            Assert.AreEqual(oddNumbers.Length, setOdds.Count);
        }
开发者ID:VesiBaleva,项目名称:TelerikAcademy,代码行数:26,代码来源:HashedSetTest.cs

示例6: IntersectShouldProducesSetContainingCommonUniqueItems

        public void IntersectShouldProducesSetContainingCommonUniqueItems()
        {
            ICollection<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };

            var hashset = new HashedSet<int>();

            new int[] { 6, 7, 6, 6, 8, 9, 10, 11, 12 }.ForEach(x => hashset.Add(x));

            var result = hashset.IntersectWith(numbers);

            new int[] { 6, 7, 8 }.ForEach(x => Assert.IsTrue(result.Contains(x)));

            Assert.AreEqual(3, result.Count);
        }
开发者ID:KonstantinSimeonov,项目名称:Studying-with-Pesho-and-Mimeto,代码行数:14,代码来源:HashedSetTests.cs

示例7: Main

    static void Main()
    {
        HashedSet<string> myHashSet = new HashedSet<string>();

        // Add method test
        Console.WriteLine("Add and foreach tests");
        myHashSet.Add("kir4o");
        myHashSet.Add("lili");
        myHashSet.Add("tanq");
        Console.WriteLine();
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
        Console.WriteLine();

        // Find method test
        Console.WriteLine("Find method test");
        Console.WriteLine("Find tanq = {0}", myHashSet.Find("tanq"));
        Console.WriteLine("Find sdfasdf = {0}", myHashSet.Find("sdfasdf"));
        Console.WriteLine();

        // Remove method test
        Console.WriteLine("Remove method test");
        myHashSet.Remove("lili");
        Console.WriteLine("lili removed");
        Console.WriteLine();

        // Union with test

        HashedSet<string> otherHashSet = new HashedSet<string>();
        otherHashSet.Add("venera");
        otherHashSet.Add("jupiter");
        otherHashSet.Add("kir4o");

        Console.WriteLine("Union with:");
        foreach (string element in otherHashSet)
        {
            Console.WriteLine(element);
        }
        myHashSet.UnionWith(otherHashSet);
        Console.WriteLine("Resulted hashSet:");
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
        Console.WriteLine();

        // Intersect with test
        Console.WriteLine("Intersect with :");
        foreach (string element in otherHashSet)
        {
            Console.WriteLine(element);
        }

        myHashSet.IntersectWith(otherHashSet);

        Console.WriteLine();
        Console.WriteLine("Resulted hashSet:");
        foreach (string element in myHashSet)
        {
            Console.WriteLine(element);
        }
    }
开发者ID:KirilToshev,项目名称:Projects,代码行数:64,代码来源:HashedSetMain.cs


注:本文中的HashedSet.IntersectWith方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。