zoukankan      html  css  js  c++  java
  • 集合-字典(Lookup/SortedDictionary)

    Lookup<TKey, TElement>非常类似于Dictionary<TKey, TValue>,但是把键映射在一个值集上。

    必须调用ToLookup方法创建Lookup<TKey, TElement>对象。Lookup<TKey, TElement>对象是不可变的,无法向对象添加,移除元素。

    public class User
        {
            public string Address;
            public string Name;
            public int Age; 
        }
        class Program
        {
            static void Main(string[] args)
            {
                List<User> users = new List<User> { new User{Address="bj",Name="rxm",Age=27},
                                                      new User{Address="bj",Name="cwr",Age=24},
                                                      new User{Address="sh",Name="zcl",Age=28},
                                                      new User{Address="zz",Name="hst",Age=29}};
    
                Lookup<char, string> needUsers = (Lookup<char, string>)users.ToLookup(p => Convert.ToChar(p.Address.Substring(0, 1)),
                                                                                  p => p.Name + "-" + p.Age.ToString());
    
    
                foreach (IGrouping<char, string> item in needUsers)
                {
                    Console.WriteLine(item.Key); //b s z
                    foreach (string str in item)
                    {
                        Console.WriteLine("   {0}", str);
                    }
                }
                //-----
                Console.WriteLine("--------------");
                IEnumerable<string> results = needUsers['b'];
                foreach (var item in results)
                {
                    Console.WriteLine(item);
                }
                Console.Read();
            }
        }

    SortedDictionary<Tkey,Tvalue>中的键都必须唯一。与这个Dictionary<Tkey,Tvalue>类不同的是,它排了序。

     SortedDictionary<string, string> items = new SortedDictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);//忽略大小写
                items.Add("masanli", "逗你玩");
                items.Add("maji", "宇宙牌香烟");
                items.Add("liubaorui", "连升三级");
                items.Add("houbaolin", "北京话");
                foreach (KeyValuePair<string, string> item in items)
                {
                    Console.WriteLine(string.Format("key:{0},value:{1}", item.Key, item.Value));
                }
  • 相关阅读:
    ASP.NET 2.0 中的主版頁面 Master Pages
    初探ERP的数据库框架
    node.js 入门,
    mysql 测试
    zendframework 内置view
    session.save_Handler
    读mysql技术内幕 InnoDB 第三章
    php 的命名空间 看鸟哥后的随笔
    mysql innodb技术内幕 ~读 1,2,3 复习
    php 无乱码截取中文
  • 原文地址:https://www.cnblogs.com/hometown/p/3223366.html
Copyright © 2011-2022 走看看