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);
      }
    }

  • 相关阅读:
    Deployment.yaml文件
    Python 闭包
    github使用方法(一)
    7.Python 正则表达式学习笔记
    kindle网络爬虫续集
    windows下如何快速搭建web.py开发框架
    Python强大的自省简析
    11--Python 备份文件程序
    10- python 网络爬虫分析
    Python基础学习7---异常处理
  • 原文地址:https://www.cnblogs.com/yuming1983/p/3546364.html
Copyright © 2011-2022 走看看