In Java, how does hashtable store and read data?

Source: Internet
Author: User

Hashtable-Hash Table class

Store data in the form of a hash table, and the data form is a key-value pair.
Features:
Fast search speed and slow Traversal
The key value cannot have null pointers or duplicate data.

Create
Hashtable <integer, string> ht = new hashtable <integer, string> ();

Add Value

Ht. Put (1, "Andy ");
Ht. Put (2, "Bill ");
Ht. Put (3, "Cindy ");
Ht. Put (4, "Dell ");
Ht. Put (5, "felex ");
Ht. Put (6, "Edinburg ");
Ht. Put (7, "green ");

Value

String STR = Ht. Get (1 );
System. Out. println (STR); // Andy

Traverse keys

Iterator it = Ht. keyset (). iterator ();

While (it. hasnext ()){
Integer key = (integer) it. Next ();
System. Out. println (key );
}

Traverse the value

Iterator it = Ht. Values (). iterator ();

While (it. hasnext ()){
String value = (string) it. Next ();
System. Out. println (value );
}

Number of hashtable records

Hashtable <integer, string> ht = new hashtable <integer, string> ();

Ht. Put (1, "Andy ");
Ht. Put (2, "Bill ");
Ht. Put (3, "Cindy ");
Ht. Put (4, "Dell ");
Ht. Put (5, "felex ");
Ht. Put (6, "Edinburg ");
Ht. Put (7, "green ");

Int I = HT. Size (); // 7

Delete Element

Hashtable <integer, string> ht = new hashtable <integer, string> ();

Ht. Put (1, "Andy ");
Ht. Put (2, "Bill ");
Ht. Put (3, "Cindy ");
Ht. Put (4, "Dell ");
Ht. Put (5, "felex ");
Ht. Put (6, "Edinburg ");
Ht. Put (7, "green ");

Ht. Remove (1 );
Ht. Remove (2 );
Ht. Remove (3 );
Ht. Remove (4 );

System. Out. println (HT. Size ());// 3

Iterator it = Ht. Values (). iterator ();

While (it. hasnext ()){
// Get value
String value = (string) it. Next ();
System. Out. println (value );
}

 

 

Truncates a string to a specified position in Java.

 

String Path = "http: /// localhost: 8080/client/index. JSP "; now I want to add the last slash ,'. 'previous parts (that is, 'index') are intercepted. How can we cut them?

Path. substring (path. lastindexof ("/") + 1, path. lastindexof ("."))

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.