Use the hashtable set (1)

Source: Internet
Author: User
1. hashtable Overview

Elements in the hashtable set exist as key/value. Key is used for quick search; value is used to store the value corresponding to the key. It is worth noting that both key and value are of the object type.

Ii. hashtable operations
Hashtable operations include:

1) Add a set Element
2) retrieve collection Elements
3) Traversal
4) sorting set Elements
5) modify the collection Element
6) Delete collection Elements

The following simple ConsoleProgramPerform the preceding operations (C #)

Namespace Hashtableop
{
Using System;
Using System. collections;

Class Sample
{

Static   Void Main ( String [] ARGs)
{

// Create a hashtable set
Hashtable HT = New Hashtable ();

// Add a key/value pair to the Collection
Ht. Add ( " 1 " , " Apple " );
Ht. Add ( " 2 " , " Orange " );
Ht. Add ( " 3 " , " Strawberry " );
Ht. Add ( " 0 " , " Fruit " );

// Locate value using key
Console. writeline ( " Locate value using key " );
String V = ( String ) HT [ " 1 " ];
Console. writeline (v );

// Traverse a set
Console. writeline ( " \ N traversal set " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());

// Sort the set
Console. writeline ( " \ N sorts the set " );
Arraylist akeys = New Arraylist (HT. Keys );
Akeys. Sort ();
Foreach ( String Key In Akeys)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , Key, HT [Key]. tostring ());

// Modifies the value of a specified key.
HT [ " 0 " ] = " Animal " ;
HT [ " 1 " ] = " Monkey " ;
HT [ " 2 " ] = " Horse " ;
HT [ " 3 " ] = " Donkey " ;
Console. writeline ( " \ N traverse the modified set " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());

// Delete A set Element
Ht. Remove ( " 2 " );
Ht. Remove ( " 1 " );
Console. writeline ( " \ N traverses the set after the deleted Element " );
Foreach (Dictionaryentry de In HT)
Console. writeline ( " Key = \ " { 0 }\ " \ Tvalue = \ " { 1 }\ "" , De. Key. tostring (), De. value. tostring ());

//Delete collection Elements
Ht. Clear ();
}
}
}

 

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.