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


C# HashedSet.UnionWith方法代码示例

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


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

示例1: TestUnionMethod

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

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

            int[] unionArray = { 2, 3, 4, 5 };
            hashedSet.UnionWith(unionArray);

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

示例2: 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

示例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: UnionShouldReturnSetWithElementsFromBothSets

        public void UnionShouldReturnSetWithElementsFromBothSets()
        {
            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.UnionWith(secondSet);

            Assert.AreEqual(10, result.Count);

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

示例5: Union

        public void Union()
        {
            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.UnionWith(setDigits);

            int[] expectedArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 17, 19, 21, 23, 25 };

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

            setOdds = new HashedSet<int>(oddNumbers);

            setDigits.UnionWith(setOdds);

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

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

示例6: UnionShouldReturnUniqueValues

        public void UnionShouldReturnUniqueValues()
        {
            ICollection<int> numbers = new List<int>() { 1, 2, 2, 2, 3, 3, 4, 4, 5 };
            var hashedset = new HashedSet<int>();

            new int[] { 6, 2, 6, 7, 3 }.ForEach(n => hashedset.Add(n));

            var result = hashedset.UnionWith(numbers);

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

示例7: UnionShouldProduceSetContainingElementsFromBothSets

        public void UnionShouldProduceSetContainingElementsFromBothSets()
        {
            ICollection<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
            var hashedset = new HashedSet<int>();

            sampleNames.ForEach(n => hashedset.Add(n.Length));

            var result = hashedset.UnionWith(numbers);

            numbers.ForEach(x => Assert.IsTrue(result.Contains(x)));

            hashedset.ForEach(x => Assert.IsTrue(result.Contains(x)));
        }
开发者ID:KonstantinSimeonov,项目名称:Studying-with-Pesho-and-Mimeto,代码行数:13,代码来源:HashedSetTests.cs

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