Usage of the hashtable class

Source: Internet
Author: User

Implemented the map interface, which is a synchronized hash table and does not allow key names and key values of the null type. Hash Tables are mainly used to store some mappings. This class is special. It is not the same as other classes in the collection. First, it is synchronous, And it inherits from the java. util. dictionary class. A typical application is to provide various parameters when connecting to the database, including the host, port, Database ID, user name, and password. The information can be stored in the hash table first, then use the most parameter. The following example describes how to use hashtable. Package COM. li. collection; import Java. util. hashtable; public class hashtabletest {public static void main (string [] ARGs) {hashtabletest = new hashtabletest (); hashtabletest. test ();} public void test () {hashtable colors = new hashtable (); // create the hashtable object colors. put ("red", "Red"); colors. put ("black", "black"); colors. put ("gray", "gray"); colors. put ("blue", "blue"); colors. put ("green", "green"); colors. put ("yellow", "yellow"); colors. put ("white", "White"); // Add the system element to hashtable. out. println ("element:" + colors. tostring (); Boolean B = colors. containskey ("red"); // determines whether the key red system is included. out. println ("include key RED:" + B); B = colors. containsvalue ("red"); // determines whether the value contains red system. out. println ("include value RED:" + B); string temp = (string) colors. get ("black"); // obtain the system with the black key. out. println ("the value of the key black is:" + temp); colors. remove ("gray"); // Delete the system element whose key is gray. out. println ("after Gray is deleted:" + colors. tostring (); system. out. println ("number of elements:" + colors. size (); // obtain the number of elements colors. clear (); // clear the object system. out. println ("after clearing:" + colors. tostring () ;}} running result: element: {Blue = blue, gray = gray, white = white, green = green, red = red, yellow = yellow, black = black} whether the key RED: True contains the value RED: false the corresponding value of the key black is: Black deleted after Gray: {Blue = blue, white = white, green = green, red = red, yellow = yellow, black = black} the number of elements is: 6

After clearing :{}

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.