List Set

List·Î Áߺ¹ ¸®½ºÆ® Á¦°Å, Â÷ÁýÇÕ, ±³ÁýÇÕ, ÇÕÁýÇÕÀ» ±¸ÇöÇØ º¸ÀÚ.

¿©±â¿¡¼­ »ç¿ëµÈ usingÀº ´ÙÀ½°ú °°´Ù.

using System;
using System.Collections.Generic;
using System.Linq;

1. 1ºÎÅÍ 10±îÁö Ãâ·ÂÇϱâ
            //int[] squares = Enumerable.Range(1, 10).ToArray<int>();
            //var squares = Enumerable.Range(1, 10).ToArray();
            IEnumerable<int> squares = from value in Enumerable.Range(1, 10) select value;
            
            foreach (int num in squares)
            {
                Console.Write("{0} ", num);
            }

Ãâ·Â :   1 2 3 4 5 6 7 8 9 10

2. IEnumerable »ç¿ë
        public void Test()
        {
            Display(new List<bool> { true, false, true});
        }

        static void Display(IEnumerable<bool> argument)
        {
            foreach (bool value in argument)
            {
                Console.Write("{0} ", value);
            }
        }

Ãâ·Â:  True False True

3. Áߺ¹ ¸®½ºÆ® Á¦°Å
            List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
            //distinctList = yourList.Distinct().ToList();
            List<int> distinctList = Enumerable.ToList(Enumerable.Distinct(ages));

            foreach (int age in distinctList)
            {
                Console.Write("{0} ", age);
            }

Ãâ·Â: 21 46 55 17

List.Distinct À¯´ÏƼ C#¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â´Ù.
Enumerable.Distinct À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.

4.Áߺ¹ ¸®½ºÆ® Á¦°Å
            List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
            HashSet<int> set = new HashSet<int>();
            ages.RemoveAll(x => !set.Add(x));

            foreach (int age in ages)
            {
                Console.Write("{0} ", age);
            }

Ãâ·Â: 21 46 55 17

5. Â÷ÁýÇÕ
            //list1 - list2 = Â÷ÁýÇÕ
            List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
            List<int> list2 = new List<int> { 1, 3, 5 };

            List<int> result = Enumerable.ToList(Enumerable.Except(list1, list2));

            foreach (var v in result)
            {
                Console.Write("{0} ", v);
            }

Ãâ·Â: 2 4

List.Except À¯´ÏƼ C#¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â´Ù.
Enumerable.Except À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.

6. Â÷ÁýÇÕ
            //list1 - list2 = Â÷ÁýÇÕ
            List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
            List<int> list2 = new List<int> { 1, 3, 5 };

            IEnumerable<int> result = Enumerable.Except(list1, list2);

            foreach (var v in result)
            {
                Console.Write("{0} ", v);
            }

Ãâ·Â: 2 4

À§ÀÇ Â÷ÁýÇÕ°ú °°´Ù. IEnumerable·Î Ãâ·ÂÇÏ°í ÀÖ´Ù.

7. Â÷ÁýÇÕ
            //Difference between two lists
            List<int> oldList = new List<int> { 1, 2, 5, 8, 9 };
            List<int> newList = new List<int> { 1, 3, 7, 8, 9};
            List<int> diffList = new List<int>();

            for (int n = 0; n < newList.Count; ++n)
            {
                if (oldList.Contains(newList[n]) == false)
                    diffList.Add(newList[n]);
            }

            for (int n = 0; n < diffList.Count; ++n)
            {
                Console.Write("{0} ", diffList[n]);
            }

Ãâ·Â: 3 7

IEnumerableÀ» »ç¿ëÇÏÁö ¾Ê°í Á÷Á¢ ±¸Çö ÇÏ¿´´Ù. À¯´ÏƼ C#¿¡¼­ »ç¿ë °¡´ÉÇÏ´Ù.

8. ±³ÁýÇÕ
            //list1, list2 = ±³ÁýÇÕ
            List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
            List<int> list2 = new List<int> { 1, 3, 5, 6 };

            IEnumerable<int> result = Enumerable.Intersect(list1, list2);

            foreach (var v in result)
            {
                Console.WriteLine(v);
            }

Ç¥½Ã: 1 3 5

List.Intersect À¯´ÏƼ C#¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â´Ù.
Enumerable.Intersect À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.

9. ÇÕÁýÇÕ
            //list1, list2 = ÇÕÁýÇÕ
            List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };
            List<int> list2 = new List<int> { 1, 3, 5, 6 };

            IEnumerable<int> result = Enumerable.Union(list1, list2);

            foreach (var v in result)
            {
                Console.Write("{0} ", v);
            }

Ãâ·Â: 1 2 3 4 5 6

List.Union À¯´ÏƼ C#¿¡¼­ Áö¿øÇÏÁö ¾Ê´Â´Ù.
Enumerable.Union À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.


10. HashSet·Î ÇÕÁýÇÕ ±¸Çö
        //Hash union
        public class Person
        {
            public int Number { get; set; }
            public string Name { get; set; }

            public Person(int number, string name)
            {
                Number = number;
                Name = name;
            }
        }

        class PersonComparer : IEqualityComparer<Person>
        {
            public bool Equals(Person p1, Person p2)
            {
                return p1.Number == p2.Number;
            }

            public int GetHashCode(Person p)
            {
                return p.Number;
            }
        }

        public Test()
        {
            List<Person> list1 = new List<Person> { new Person(1, "one"), new Person(2, "two"), new Person(3, "three") };
            List<Person> list2 = new List<Person> { new Person(1, "one"), new Person(5, "two") };

            var hs = new HashSet<Person>(list1, new PersonComparer());
            hs.UnionWith(list2);
            var merged = hs.ToList();

            foreach (var person in merged)
            {
                Console.Write("{0} ", person.Number);
            }
        }

Ãâ·Â: 1 2 3 5

HashSetÀ¸·Î ÇÕÁýÇÕÀ» ±¸ÇöÇÏ°í ÀÖ´Ù. À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.
ÀÌ¿Ü¿¡µµ ExceptWith, IntersectWithÀ» Áö¿øÇÑ´Ù.

11. Dictionary·Î ÇÕÁýÇÕ ±¸Çö
        public class Person
        {
            public int Number { get; set; }
            public string Name { get; set; }

            public Person(int number, string name)
            {
                Number = number;
                Name = name;
            }
        }

        public Test()
        {
            List<Person> list1 = new List<Person> { new Person(1, "one"), new Person(2, "two"), new Person(3, "three") };
            List<Person> list2 = new List<Person> { new Person(1, "one"), new Person(5, "two") };

            var dic = list2.ToDictionary(p => p.Number);

            foreach (var person in list1)
            {
                dic[person.Number] = person;
            }

            var merged = dic.Values.ToList();

            foreach (var person in merged)
            {
                Console.Write("{0} ", person.Number);
            }
        }

Ãâ·Â: 1 5 2 3

12. Linq Äõ¸® ÀÌ¿ë
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

namespace csharpTest
{
    class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Test
    {
        public Test()
        {
            IEnumerable<People> peopleList = new People[]
            {
                new People { Name = "kim", Age = 20},
                new People { Name = "lee", Age = 20},
                new People { Name = "park", Age = 15},
                new People { Name = "wang", Age = 15}
            };

            var query = from p in peopleList
                        where p.Age == 20
                        select p;

            foreach (var value in query)
            {
                Console.WriteLine(value.Name + ", " + value.Age);
            }
        }
    }
}

Ãâ·Â:
kim, 20
lee, 20

À¯´ÏƼ C#¿¡¼­µµ »ç¿ë °¡´ÉÇÏ´Ù.