linked hash map

child pages:

page index:

links:



using System;
using System.Collections.Generic;

 

// code from https://stackoverflow.com/questions/29205934/c-sharp-equivalent-of-linkedhashmap
// another https://stackoverflow.com/questions/486948/linkedhashmap-in-net

// java linked hashmap https://www.geeksforgeeks.org/linkedhashmap-class-in-java/

// c# iterators https://www.geeksforgeeks.org/iterators-in-c-sharp/

// expose an iterator google search https://www.google.com/search?q=c%23+what+does+expose+an+iterator+mean&rlz=1C1GCEA_enUS940US940&oq=c%23+what+does+expose+an+iterator+mean&aqs=chrome..69i57j69i58.11563j0j3&sourceid=chrome&ie=UTF-8

 

namespace linkedhashmaptest
{
       internal class linkedhashmaptest
       {
             static void Main(string[] args)
             {
                    var lhm = new LinkedHashMap<string, string>();

                    lhm["apple"] = "theapplestring";
                    lhm["bstuff"] = "bstring";
                    lhm["anotheraentry"] = "secondastring";

                    lhm.DoIteration();

 

                    Console.WriteLine(lhm["apple"]);
                    Console.WriteLine(lhm["bstuff"]);
                    //Console.WriteLine(lhm["cnotthere"]);  // throws not there exception

                    Console.WriteLine(lhm.PopFirst());
                    Console.WriteLine(lhm.PopFirst());
                    Console.WriteLine(lhm.PopFirst());
                   
             }
       }

 

       class LinkedHashMap<T, U>
       {
             Dictionary<T, LinkedListNode<Tuple<U, T>>> D = new Dictionary<T, LinkedListNode<Tuple<U, T>>>();
             LinkedList<Tuple<U, T>> LL = new LinkedList<Tuple<U, T>>();
             LinkedList<Tuple<U, T>>.Enumerator myEnumerator;

             public void DoIteration()
             {
                    //myEnumerator = LL.GetEnumerator();
                    foreach (var item in LL)
                    {
                           Console.WriteLine(item);
                    }
             }

             public U this[T c]
             {
                    get
                    {
                           return D[c].Value.Item1;
                    }

                    set
                    {
                           if (D.ContainsKey(c))
                           {
                                 LL.Remove(D[c]);
                           }

                           D[c] = new LinkedListNode<Tuple<U, T>>(Tuple.Create(value, c));
                           LL.AddLast(D[c]);
                    }
             }

             public bool ContainsKey(T k)
             {
                    return D.ContainsKey(k);
             }

             public U PopFirst()
             {
                    var node = LL.First;
                    LL.Remove(node);
                    D.Remove(node.Value.Item2);
                    return node.Value.Item1;
             }

             public int Count
             {
                    get
                    {
                           return D.Count;
                    }
             }
       }
}

 


 

last updated:    Sun 2022-04-03 2:52 AM