zoukankan      html  css  js  c++  java
  • C# 两个集合对比获取不同

    public class CompareCollection
    {
      public List<string> CompareList(List<string> oldList, List<string> newList)
      {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        AddDictionary(dict, oldList);
        AddDictionary(dict, newList);

        return dict.Where(r => r.Value==string.Empty).Select(c => c.Key).ToList();
      }
      private void AddDictionary(Dictionary<string, string> dict, List<string> list)
      {
        foreach (var ls in list)
        {
          if (dict.Keys.Contains(ls))
          {
            dict[ls] = ls;
          }
          else
          {
            dict.Add(ls, string.Empty);
          }
        }
      }
    }
    class Program
    {
      static void Main(string[] args)
      {
        //从数据库读取的数组
        List<string> newlist = new List<string> { "001", "002", "003", "999" };
        //初始化0~999数组
        List<string> oldlist = new List<string>();
        for (int i = 0; i < 1000; i++)
        {
          if (i < 10)
            oldlist.Add("00" + i);
          if (i >= 10 && i < 100)
            oldlist.Add("0" + i);
          if (i >= 100)
            oldlist.Add(i.ToString());
        }
        CompareCollection col = new CompareCollection();
        List<string> list = col.CompareList(oldlist, newlist);
      }
    }

  • 相关阅读:
    如何写一个完整课堂管理系统(数据库增删改查)
    关于java异常处理的思考
    java03类与对象相关问题
    java02实验:方法
    java02动手动脑
    java课堂动手动脑及课后实验总结
    java测试银行系统源代码
    JAVA程序系统测试感受
    2018年第八周暑假进度报告
    2018第七周进度总结报告
  • 原文地址:https://www.cnblogs.com/yuming1983/p/3546364.html
Copyright © 2011-2022 走看看