Click here to Skip to main content
Click here to Skip to main content

Tagged as

CustomDictionary Class

, 10 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.

Introduction

I was working on a project and I needed to use an object like Dictionary object in .net framework but I needed to use an object that accepts duplicate keys! I read few articles including: http://www.codeproject.com/KB/recipes/multikey-dictionary.aspx but I finally decided to write my own version of it.

The CustomDictionary is a C# class which extends the functionality of the Dictionary object provided in .net framework. It accepts multiple keys (duplicate key entries) and implements similar interface to IDictionary which developers are familiar with. However I suggest you to look at the Lookup<tkey,telement> class under System.Linq namespace as well if you are looking for using a class within .net framework.

Using the Code

The next section shows the source code of the CustomInterface interface, CustomDictionary class and an example which illustrates how to instantiate and use the CustomDictionary class in your application:


public interface ICustomDictionary<tkey,> 
{      
   ICollection<tkey> Keys { get; }
   ICollection<tvalue> Values { get; }
   bool ContainsKey(TKey key);
   void Add(TKey key, TValue value);
   void Add(KeyValuePair<tkey,> item);
   bool Remove(TKey key);
   bool TryGetValues(TKey key, out IList<tvalue> values);
}
    /// <summary>
    /// Represents a collection of keys and values. The custom dictionary class which accepts duplicate keys. 
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    public class CustomDictionary<tkey,>: ICustomDictionary<tkey,tvalue>       
    {
        private IDictionary<guid,tkey> dictionaryKeys = new Dictionary<guid,tkey>();
        private IDictionary<guid,tvalue> dictionaryValues = new Dictionary<guid,tvalue>();
        ICollection<tkey> ICustomDictionary<tkey,>.Keys
        {
            get { return dictionaryKeys.Values; }
        }
        ICollection<tvalue> ICustomDictionary<tkey,>.Values
        {
            get { return dictionaryValues.Values; }
        }
        bool ICustomDictionary<tkey,>.ContainsKey(TKey key)
        {
            return dictionaryKeys.Any(keyValuePair => keyValuePair.Value.Equals(key));            
        }
        void ICustomDictionary<tkey,>.Add(TKey key, TValue value)
        {
            var association = Guid.NewGuid();
            dictionaryKeys.Add(association, key);
            dictionaryValues.Add(association, value);
        }
        bool ICustomDictionary<tkey,>.Remove(TKey key)
        {
            //find associations
            var associationsList =
            (from keyValuePair in dictionaryKeys 
             where keyValuePair.Value.Equals(key)
             select keyValuePair.Key).ToList();
            //remove from Dictionaries
            foreach (Guid guid in associationsList)
            {
                dictionaryKeys.Remove(guid);
                dictionaryValues.Remove(guid);
            }
            return (associationsList.Count > 0);
        }
        bool ICustomDictionary<tkey,>.TryGetValues(TKey key, out IList<tvalue> values)
        {
            IList<tvalue> list = new List<tvalue>();
            foreach (KeyValuePair<guid,> keyValuePair in dictionaryKeys)
            {
                if (keyValuePair.Value.Equals(key))
                {
                    //get the GUID (association key) 
                    var association = keyValuePair.Key;
                    // use the GUID to find the TB in the Values dictionary
                    list.Add(dictionaryValues[association]);
                }
            }
            values = list;
            return (list.Count > 0);
        }
        void ICustomDictionary<tkey,>.Add(KeyValuePair<tkey,> item)
        {
            var association = Guid.NewGuid();
            dictionaryKeys.Add(association, item.Key);
            dictionaryValues.Add(association, item.Value);
        }
    }</tvalue>

How to Use

private void button2_Click(object sender, EventArgs e)
        {
            ICustomDictionary<string,> customDictionary = new CustomDictionary<string,>();
            customDictionary.Add("1", "1A");
            customDictionary.Add("2", "2A");
            customDictionary.Add("3", "3A");
            customDictionary.Add("4", "4A");
            customDictionary.Add("1", "11A");
            customDictionary.Add("1", "111A");
            customDictionary.Add("2", "22A");
            customDictionary.Add(new KeyValuePair<string,>("2", "222A"));
            IList<string> list = new List<string>();
            bool re = customDictionary.TryGetValues("1", out list);
            ICollection<string> Keys = customDictionary.Keys;
            ICollection<string> vlaues = customDictionary.Values;
            bool contains = customDictionary.ContainsKey("4");
            bool removed = customDictionary.Remove("1");
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kurush Rastkar
Software Developer
New Zealand New Zealand
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 2 You should have used Dictionary < st... PinmemberSledgeHammer0110-Nov-10 14:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411022.1 | Last Updated 10 Nov 2010
Article Copyright 2010 by Kurush Rastkar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid