zoukankan      html  css  js  c++  java
  • 去除list集合中重复项的几种方法

    因为用到list,要去除重复数据,尝试了几种方法。记录于此。。。

    测试数据:

            List<string> li1 = new List<string> { "8", "8", "9", "9" ,"0","9"};
                List<string> li2 = new List<string> { "张三", "张三", "李四", "张三", "王五", "李四" };
                List<string> li3 = new List<string> { "A", "A", "C", "A", "C", "D" };
                List<string> li4 = new List<string> { "12", "18", "19", "19", "10", "19" };

    方法一:

    HashSet<string> hs = new HashSet<string>(li1); //此时已经去掉重复的数据保存在hashset中

    方法二:

    for (int i = 0; i < li2.Count; i++)  //外循环是循环的次数
                {
                    for (int j = li2.Count - 1 ; j > i; j--)  //内循环是 外循环一次比较的次数
                    {
    
                        if (li2[i] == li2[j])
                        {
                            li2.RemoveAt(j);
                        }
    
                    }
                }

    方法三:

          //把相同的用null代替。
                for (int i = 0; i < li3.Count; i++)
                {
                    for (int j = 0; j < li3.Count; j++)
                    {
                        if (i == j) continue;
    
                        if (li3[i] == li3[j])
                        {
                            li3[j] = "null";
                        }
    
                    }
                }

    方法四:

            //这方法跟上面的一样,只是变了逻辑
                for (int i = 0; i < li4.Count - 1; i++)
                {
                    for (int j = 0; j < li4.Count ; j++)
                    {
                        if (i != j)
                        {
                            if (li4[i] == li4[j])
                            {
                                li4[j] = "null";
                            }
                        }
                    }
                }

    最后输出看结果

            Console.WriteLine("li1去除重复后的值为");
                hs.ToList().ForEach(item => Console.WriteLine(item));
    
                Console.WriteLine("li2去除重复后的值为");
                li2.ForEach(item => Console.WriteLine(item));
    
                Console.WriteLine("li3去除重复后的值为");
                li3.ForEach(item => Console.WriteLine(item));
    
                Console.WriteLine("li4去除重复后的值为");
                li4.ForEach(item => Console.WriteLine(item));

    null我没去掉。用的时候去掉即可。

    当然。还有许多办法。比如linq  Distinct  等等都可以,看看网上的这个例子:去掉modelList中title重复的内容,不区分大小写

     class Program
        {
            static void Main(string[] args)
            {
                List<Model> modelList = new List<Model>() 
                { new Model() { ID = 1, Title = "abcde" },
                    new Model() { ID = 2, Title = "ABCDE" },
                    new Model(){ ID = 3, Title = "AbcdE" },
                    new Model() { ID = 4, Title = "A" }, 
                    new Model() { ID = 5, Title = "a" } 
                };
                Console.Read();
            }
        }
        public class Model
        {
            public int ID { get; set; }
            public string Title { get; set; }
        }

    解决方案一:这里比较的前提是对象的哈希代码相等。否则不会比较,因为哈希代码不相等。两个对象显然不相等

    //定义一个类继承IEqualityComparer接口
        public class ModelComparer : IEqualityComparer<Model>
        {
            public bool Equals(Model x, Model y)
            {
                return x.Title.ToUpper() == y.Title.ToUpper();
            }
            public int GetHashCode(Model obj)
            {
                return obj.Title.ToUpper().GetHashCode();
            }
        }

    调用:

    modelList = modelList.Distinct(new ModelComparer()).ToList();

    解决方案二:

    var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.FirstOrDefault().ID });
                modelList = modelList.Where(m => title.Select(mo => mo.ID).Contains(m.ID)).ToList(); 
                foreach (var item in modelList)
                {
                    Console.WriteLine(item.ID + "\t" + item.Title);
                }

    当然。如果你仅仅比较两个值是否相等。

     List<string> li1 = new List<string> { "8", "8", "9", "8", "0", "9" };
                li1 = li1.Distinct().ToList();
    作者: nsky
    出处: http://nsky.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,共同学习;共同进步;但不能乱搞!
  • 相关阅读:
    LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
    LeetCode 1041. Robot Bounded In Circle (困于环中的机器人)
    LeetCode 1037. Valid Boomerang (有效的回旋镖)
    LeetCode 1108. Defanging an IP Address (IP 地址无效化)
    LeetCode 704. Binary Search (二分查找)
    LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
    LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
    LeetCode 817. Linked List Components (链表组件)
    LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)
    29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/nsky/p/2936522.html
Copyright © 2011-2022 走看看