1 module LinkedHashMapDemo;
2 
3 import common;
4 
5 import hunt.collection.LinkedHashMap;
6 import hunt.collection.HashMap;
7 import hunt.collection.Map;
8 import hunt.collection.Iterator;
9 
10 import std.stdio;
11 import std.conv;
12 import std.range;
13 
14 class LinkedHashMapDemo
15 {
16     // http://java2novice.com/java-collections-and-util/linkedhashmap/
17     void testBasic()
18     {
19         // http://java2novice.com/java-collections-and-util/linkedhashmap/basic-operations/
20         LinkedHashMap!(string, string) lhm = new LinkedHashMap!(string, string)();
21         lhm.put("one", "This is first element");
22         lhm.put("two", "This is second element");
23         lhm.put("four", "this element inserted at 3rd position");
24 
25         writeln(lhm.toString());
26         writeln("Getting value for key 'one': " ~ lhm.get("one"));
27         writeln("Size of the map: " ~ lhm.size().to!string());
28         writeln("Is map empty? " ~ lhm.isEmpty().to!string());
29         writeln("Contains key 'two'? " ~ lhm.containsKey("two").to!string());
30         writeln("Contains value 'This is first element'? " ~ lhm.containsValue(
31                 "This is first element").to!string());
32 
33         assert(lhm.size() == 3);
34         writeln("delete element 'one': " ~ lhm.remove("one"));
35         assert(lhm.size() == 2);
36         writeln(lhm.toString());
37 
38         lhm.clear();
39         assert(lhm.size() == 0);
40         writeln(lhm.toString());
41     }
42 
43     void testIterator()
44     {
45         //
46         writeln("Testing LinkedHashMap...");
47         LinkedHashMap!(int, string) lhmap = new LinkedHashMap!(int, string)();
48 
49         //Adding elements to LinkedHashMap
50         lhmap.put(22, "Abey");
51         lhmap.put(33, "Dawn");
52         lhmap.put(1, "Sherry");
53         lhmap.put(2, "Karon");
54         lhmap.put(100, "Jim");
55 
56         assert(lhmap[1] == "Sherry");
57 
58         foreach (int key, string v; lhmap)
59         {
60             writeln("Key is: " ~ key.to!string ~ " & Value is: " ~ v);
61         }
62 
63         writeln("\nTesting LinkedHashMap byValue...");
64         foreach (size_t index, string value; lhmap.byValue)
65         {
66             writefln("value[%d] is: %s ", index, value);
67         }
68     }
69 
70     // http://java2novice.com/java-collections-and-util/linkedhashmap/duplicate-key/
71     void testObjectKey()
72     {
73         LinkedHashMap!(Price, string) hm = new LinkedHashMap!(Price, string)();
74         hm.put(new Price("Banana", 20), "Yellow Banana");
75         hm.put(new Price("Apple", 40), "Red Apple");
76         hm.put(new Price("Orange", 30), "Juicy Orange");
77         printMap(hm);
78 
79         assert(hm.size() == 3);
80         Price key = new Price("Banana", 20);
81         writeln("Adding duplicate key...");
82         hm.put(key, "Grape");
83         
84         assert(hm.size() == 3);
85         writeln("After adding dulicate key:");
86         printMap(hm);
87 
88         writeln("Does key available? " ~ hm.containsKey(key).to!string());
89 
90 		writeln("Deleting key...");
91 		hm.remove(key);
92 		writeln("After deleting key:");
93         writeln("Does key available? " ~ hm.containsKey(key).to!string());
94 		printMap(hm);
95     }
96 
97     void printMap(LinkedHashMap!(Price, string) map)
98     {
99         foreach (Price p; map.byKey)
100         {
101             writeln(p.toString() ~ " ==> " ~ map.get(p));
102         }
103     }
104 }
105