JAVA里IndentityHashMap可以实现重复key的集合 C# 里 NameValueCollection

JAVA里有个IndentityHashMap可以实现重复key的集合 C# 里当然肯定必须也会有这样的类了 NameValueCollection 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace SamplesNameValueCollection
{
    class Program
    {
        
        public static void Main()
        {

            // Creates and initializes a new NameValueCollection.
            NameValueCollection myCol = new NameValueCollection();
            myCol.Add("red", "ro,jo");
            myCol.Add("green", "verde");
            myCol.Add("blue", "azul");
            myCol.Add("red", "rouge");

            // Displays the values in the NameValueCollection in two different ways.
            Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:");
            PrintKeysAndValues(myCol);
            PrintKeysAndValues2(myCol);
            PrintKeysAndValues3(myCol);


        }

        public static void PrintKeysAndValues(NameValueCollection myCol)
        {
            Console.WriteLine("   KEY        VALUE");
            foreach (String s in myCol.AllKeys)
            {
                string values = myCol[s];
                Console.WriteLine("   {0,-10} {1}", s, values);
            }
            Console.WriteLine("-------------------------------------------");
        }

        public static void PrintKeysAndValues2(NameValueCollection myCol)
        {
            Console.WriteLine("   [INDEX] KEY        VALUE");
            for (int i = 0; i < myCol.Count; i++)
                Console.WriteLine("   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i));
            Console.WriteLine("=========================================");
        }

        public static void PrintKeysAndValues3(NameValueCollection myCol)
        {
            foreach (string key in myCol.Keys)
            {
                string[] values = myCol.GetValues(key);
                foreach (string value in values)
                {
                    Console.WriteLine(key + " - " + value);
                }
            }
            Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }

    }
}
/*
Displays the elements using the AllKeys property and the Item (indexer) property
:
   KEY        VALUE
   red        ro,jo,rouge
   green      verde
   blue       azul
-------------------------------------------
   [INDEX] KEY        VALUE
   [0]     red        ro,jo,rouge
   [1]     green      verde
   [2]     blue       azul
=========================================
red - ro,jo
red - rouge
green - verde
blue - azul
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
请按任意键继续. . . 
 */

 

posted @ 2017-06-24 13:39  sky20080101  阅读(407)  评论(0)    收藏  举报