Basic usage of directory in C #
1, creation and initialization of dictionary<int, string> mydictionary = new Dictionary<int, string> (); 2. Add element Mydictionary.add ("C #", 0); Mydictionary.add ("C + +", 1); Mydictionary.add ("C", 2); Mydictionary.add ("VB", 2); 3, find the element by Key Java Chinese call method keyset into the set set, and then use the iterator iterator to traverse the set if (Mydictionary.containskey ("C #")) {Console.writel INE ("Key:{0},value:{1}", "C #", mydictionary["C #"]); } 4. Traverse element by KeyValuePair This is a relationship, and map.entry< in Java, > similar to C #:
foreach (keyvaluepair<string, int> kvp in MyDictionary) {Console.WriteLine ("Key = {0}, Value = {1}", kvp. Key, kvp. Value); }
Java:
set<map.entry<, >> entryset=map.entryset ();
iterator<map,entry<, >> it=entryset.iterator ();//Use iterators to facilitate relationship map.entry<, >
while (It.hasnext ())
{
map.entry<, > Me= it.next ();//It's a relationship.
Me.getkey ();
Me.getvalue ();
}
5. Only traverse key by Keys property dictionary<string, Int>. KeyCollection keycol = Mydictionary.keys; foreach (string key in Keycol/*string key in mydictionary.keys*/) {Console.WriteLine ("key = {0}", key); } 6, only traverse the value by Valus property dictionary<string, Int>. ValueCollection valuecol = mydictionary.values; foreach (int value in Valuecol) {Console.WriteLine ("value = {0}", value); }
Comparison of map collections in generic collections directory and Java in C #