JAVA learning lesson 39th (Common Object API)-set framework (7)-Map set and its subclass object
I. Common subclasses of Map sets
HashTable: the internal structure is a hash table for synchronization. This implementation provides all optional ing operations and does not allow the use of null values and null keys.
(HashTable has subclasses.Properties,Frequently Used, used to store key-Value Pair configuration file information and IO technology)
HashMap: the internal structure is a hash table, which is not synchronized. This implementation provides all optional ing operations and allows the use of null values and null keys.
TreeMap: the internal structure is a binary tree. If the data is not synchronized, the keys in the Map set can be sorted.
Ii. HashMap demonstration
Import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. set; class Man {private String name; private int age; 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;} public Man (String name, int age) {super (); this. name = name; this. ag E = age;} public Man () {super (); // TODO Auto-generated constructor stub} @ Overridepublic int hashCode () {final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + (name = null )? 0: name. hashCode (); return result ;}@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Man other = (Man) obj; if (age! = Other. age) return false; if (name = null) {if (other. name! = Null) return false;} else if (! Name. equals (other. name) return false; return true;} public class Main {public static void main (String [] args) {HashMapDemo ();} public static void HashMapDemo () {// people and their origins are stored in HashMap through key-value pairs
Hm = new HashMap
(); Hm. put (new Man ("Week", 1), "Shanghai"); hm. put (new Man ("Week", 1), "Northeast"); // to ensure the hash table, the HashCode and equals methods in Man must be rewritten by hm. put (new Man ("Wang", 4), "Beijing"); hm. put (new Man ("sun", 2), "Guangzhou"); hm. put (new Man ("Zhao", 6), "Shandong"); Iterator
It = hm. keySet (). iterator (); while (it. hasNext () {Man man = it. next (); String bir = hm. get (man); System. out. println ("man-bir:" + man. getName () + "," + man. getAge () + "-" + bir );}}}
3. TreeMap demonstration
Import java. util. collection; import java. util. comparator; import java. util. iterator; import java. util. map; import java. util. set; import java. util. treeMap; class Man extends Object {private String name; private int age; 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;} public Man (String name, Int age) {super (); this. name = name; this. age = age;} public Man () {super (); // TODO Auto-generated constructor stub} @ Overridepublic int hashCode () {final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + (name = null )? 0: name. hashCode (); return result ;}@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Man other = (Man) obj; if (age! = Other. age) return false; if (name = null) {if (other. name! = Null) return false;} else if (! Name. equals (other. name) return false; return true ;}} class Compararule implements Comparator{Public int compare (Object arg0, Object arg1) {if (! (Arg0 instanceof Man) throw new ClassCastException (); Man man = (Man) arg0; Man man2 = (Man) arg1; int te = man. getAge ()-man2.getAge (); return te = 0? Man. getName (). compareTo (man2.getName (): te ;}} public class Main {public static void main (String [] args) {TreeMapDemo ();} public static void TreeMapDemo () {TreeMap
Tm = new TreeMap
(New Compararule (); tm. put (new Man ("zhou", 1), "Shanghai"); tm. put (new Man ("zhou", 1), "Northeast"); // to ensure the hash table, the HashCode and equals methods in Man must be rewritten to tm. put (new Man ("wang", 4), "Beijing"); tm. put (new Man ("sun", 2), "Guangzhou"); tm. put (new Man ("zhao", 6), "Shandong"); Iterator
> It = tm. entrySet (). iterator (); while (it. hasNext () {Map. Entry
MEntry = it. next (); Man man = mEntry. getKey (); String bir = mEntry. getValue (); System. out. println ("man-bir:" + man. getName () + "," + man. getAge () + "-" + bir );}}}
The three major collections of the Collection framework: List, Set, and Map. This is the end.
See array, array, badge, fast query speed
Link, linked list, fast addition/deletion, add, get, remove frist/last Method
Hash, hash table, element uniqueness, overwrite the HashCode method and equals Method
Tree, binary tree, sorting, two interfaces: Comparator and Comparable
While Map itself does not have an iterator, but it can return the Set ing through keySet and entry, and then access the key-value pair through the Set iterator.
Tomorrow is generic learning. You need to master it. Come on !!!