JAVA Study Notes (22)-set HashMap and Hashtable, hashmaphashtable
HashMap class
Import java. util. hashMap; import java. util. map;/** HashMap class, which implements the Map interface and stores data key-value */public class Test04 {public static void main (String [] args) in the form of key-value pairs) {// create a HashMap set Map map = new HashMap (); // store the key-value pair data map in the set. put ("one", "tom"); map. put ("two", "mike"); map. put ("three", "alice"); // The key must be unique. If the key is the same, the corresponding map value is updated. put ("three", "jack"); map. put ("four", "alex"); // retrieves the value of value Object obj = map based on the key. get (" Two "); System. out. println ("two:" + obj); // Delete the specified key-value pair System based on the key. out. println (map. remove ("four"); // determines whether a specified key or value System exists. out. println ("does the element whose key is four exist? "+ Map. containsKey (" four "); System. out. println (" does the element whose value is alice exist? "+ Map. containsValue (" jack "); System. out. println (" is the set empty? "+ Map. isEmpty (); System. out. println ("number of elements in the Set:" + map. size (); System. out. println ("elements in the HashMap set:" + map );}}
Iterative Map set
Import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. map. entry; import java. util. set;/** iterative Map Set */public class Test05 {public static void main (String [] args) {// 1. create a HashMap set and save the student information Map students = new HashMap (); // 2. create Student stu1 = new Student (9527, "Tang bohu", 20); Student stu2 = new Student (3306, "Tang bolong", 22); Student stu3 = new Student (1521, "Tang Bo pig", 18); // 3. add students to the collection, student ID as key, and student object as value students. put (stu1.getId (), stu1); students. put (stu2.getId (), stu2); students. put (stu3.getId (), stu3); // output the student information System. out. println ("Total" + students. size () + "student! "); System. out. println (":"); // 1. traverse the key Set keys = students in the Set. keySet (); // The System that obtains the key. out. println ("student ID \ t name \ t age"); System. out. println ("*************** Traverse key Method 1 ****************** *"); for (Object key: keys) // traverse all keys {Student stu = (Student) students. get (key); // obtain the value System based on the key. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge ();} System. out. println ("*************** Traverse key method 2 ****************** *"); iterator it = keys. iterator (); // gets the iterator corresponding to the key set while (it. hasNext () {Student stu = (Student) students. get (it. next (); // obtain the value System based on the key. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge ();} // 2. traverse the value Collection c = students in the set. values (); // a collection of values. out. println ("*************** traverse the value method ****************** *"); for (Object obj: c) {Student stu = (Student) obj; // gets the value, that is, the Student Object System. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge ();} System. out. println ("*************** traverse value method 2 ****************** *"); iterator it2 = c. iterator (); while (it2.hasNext () {Student stu = (Student) it2.next (); System. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge ();} // 3. directly traverse the entire key-value Set set = students in the Set. entrySet (); System. out. println ("***************** traverse entry (key-value) method 1 ******************* "); for (Object obj: set) {Map. entry entry = (Map. entry) obj; int id = (Integer) entry. getKey (); // use Map. the getKey () method of Entry gets key Student stu = (Student) entry. getValue (); // use Map. the getValue () method of the Entry gets the value System. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge ();} System. out. println ("***************** traverse entry (key-value) method 2: ******************* "); Iterator it3 = set. iterator (); while (it3.hasNext () {Map. entry entry = (Map. entry) it3.next (); int id = (Integer) entry. getKey (); Student stu = (Student) entry. getValue (); System. out. println (stu. getId () + "\ t" + stu. getName () + "\ t" + stu. getAge () ;}}/ ** Student */class Student {private int id; // Student id private String name; // name private int age; // age public Student (int id, String name, int age) {super (); this. id = id; this. name = name; this. age = age;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
Hashtable class
Import java. util. enumeration; import java. util. hashMap; import java. util. hashtable; import java. util. set;/** Hashtable class, which implements the Map interface. Its usage is similar to HashMap */public class Test06 {public static void main (String [] args) {Hashtable ht = new Hashtable (); // store data into Hashtable. put (1, "Li Yuzhang"); ht. put (5, "Renaissance Celebration"); ht. put (25, "Chen Cheng"); // ht. put (null, "Wu Wei"); // the key and value of Hashtable cannot be null HashMap hm = new HashMap (); hm. put (null, "Ge Jun"); // the key and value of HashMap can be empty, but the key must be unique hm. put (13, null); System. out. println (hm); // obtain data from Hashtable String name = (String) ht. get (5); System. out. println ("key: 5 \ nvalue:" + name); System. out. println (ht);/** traverses the key set */System. out. println ("**************** use the keySet () method to traverse the key Set through the foreach loop"); set Set set = ht. keySet (); for (Object obj: set) {int id = (Integer) obj; String n = (String) ht. get (id); System. out. println (id + "," + n);}/** use Enumeration to iterate */System. out. println ("**************** use the elements () method to traverse the value set through Enumeration"); Enumeration e = ht. elements (); // traverses the value of the Enumeration instance while (e. hasMoreElements () {// determine whether the Element Object obj = e. nextElement (); // obtain the current element, that is, the current value System. out. println (obj);} System. out. println ("**************** use the keys () method to traverse the key set through Enumeration"); Enumeration e2 = ht. keys (); while (e2.hasMoreElements () {int id = (Integer) e2.nextElement (); // obtain the current key String n = (String) ht. get (id); System. out. println (id + "," + n );}}}