Comparison of map collections in generic collections directory and Java in C #

Source: Internet
Author: User
Tags set set

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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.