Hashmap of Java learning notes

Source: Internet
Author: User
Hashmap usage [fold] package hashmap; import Java. util. hashmap; import Java. util. hashtable; import Java. util. iterator; import Java. util. linkedhashmap; import Java. util. map; import Java. util. treemap; public class hashmap1 {// initialize private void Init (MAP map, string kind) {If (map! = NULL) {for (INT I = 1; I <6; I ++) {map. put (string. valueof (I), kind + I) ;}}// the private void output (MAP map) {If (map! = NULL) {object key = NULL; object value = NULL; iterator iterater = map. keyset (). iterator (); While (iterater. hasnext () {key = iterater. next (); value = map. get (key); system. out. print (Key + ":" + value + "/t");} system. out. println ("/N") ;}} public static void main (string ARGs []) {hashmap = new hashmap (); hashmap. put ("X", "1"); hashmap. put ("u", "2"); hashmap. put ("Z", "3"); hashmap. put ("H", "4"); hashmap. put ("A", "5"); hashmap. put ("O", "6"); hashmap. put ("g", "7"); hashmap. put ("u", "8"); hashmap. put ("A", "9"); hashmap. put ("N", "10"); hashmap. put ("g", "11"); object key = NULL; object value = NULL; iterator iterater = hashmap. keyset (). iterator (); While (iterater. hasnext () {key = iterater. next (); value = hashmap. get (key); system. out. print (Key + ":" + value + "/t");} system. out. println ("/N");} // declare the hashmap object private void sethashmap () {hashmap = new hashmap (); Init (hashmap, "hashmap"); hashmap. put (null, "the key value is null"); hashmap. put ("the value is null", null); system. out. println ("this is the key and value of the hashmap object:"); output (hashmap);} // declare the hashtable object private void sethashtable () {hashtable = new hashtable (); init (hashtable, "hashtable"); // hashtable. put (null, "the key value is null"); hashtable does not allow the key or the value is null; // hashtable. put ("the value is null", null); system. out. println ("this is the key and value of the hashtable object:"); output (hashtable);} // declare the linkedhashmap object private void setlinkedhashmap () {linkedhashmap = new linkedhashmap (); init (linkedhashmap, "linkedhashmap"); linkedhashmap. put (null, "the key value is null"); linkedhashmap. put ("the value is null", null); system. out. println ("this is the key and value of the linkedhashmap object:"); output (linkedhashmap);} // declare the treemap object private void settreemap () {treemap = new treemap (); // treemap = new treemap (New mysort (); // sort Init (treemap, "treemap") by custom method; treemap. put ("0", "inserted value"); // treemap. put (null, "the key value is null"); treemap does not allow the key or the value is null // treemap. put ("the value is null", null); system. out. println ("this is the key and value of the treemap object:"); output (treemap);} // public static void main (string [] ARGs) {// hashmapdemo TM = new hashmapdemo (); // TM. sethashmap (); // TM. sethashtable (); // TM. setlinkedhashmap (); // TM. settreemap (); // map hashmap = new hashmap (); // hashmap. put (null, "key value is null"); // hashmap. put ("value: NULL", null); // system. out. println ("the number of records for creating a hashmap object element is:" + hashmap. size (); // hashmap. remove (null); // system. out. println ("the number of records for deleting a hashmap object element whose key value is null is:" + hashmap. size ());//}

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.