Java Collection, java Collection class details
--- Restore content start ---
Map set: HashMap, HashTable, and LinkedHashMap
HashTable implementation principle: Based on the implementation of the hash table, the object used as the key must implement the hashCode () and equals () methods, because the key in the Map is unique. Note that the key and value of HashTable cannot be null.
Thread Synchronization and security.
The implementation principle of HashMap is the same as that of HashTable, but the key in HashMap can be null, and the value can also be null. Jdk1.2 appears in step jdk1.0. The thread is not synchronized and is not secure.
Package collection; import java. util. hashtable; import java. util. *; import java. util. map. entry; import java. util. set; public class MyMap1 {public static void main (String [] args) {Map <Person, Contact> m1 = new Hashtable <Person, Contact> (); person p1 = new Person (13, "zhangsan"); Person p2 = new Person (14, "lisi"); Person p3 = new Person (13, "zhangsan "); person p4 = new Person (13, "zhangsan"); Contact c1 = new Contact (378989, "Hunan"); Contact c2 = new Contact (378989, "Beijing "); contact c3 = new Contact (378989, "Beijing"); Contact c4 = new Contact (378989, "Beijing"); m1.put (p1, c1); m1.put (p2, c2 ); m1.put (p3, c3); m1.put (p4, c4); for (Entry <Person, Contact> entry: m1.entrySet () {System. out. println (entry. getKey () + "-" + entry. getValue ();} // System. out. println (m1) ;}} class Person {int id; String name; public Person (int id, String name) {// super (); this. id = id; this. name = name ;}@ Override public String toString () {return "Person [id =" + id + ", name =" + name + "]" ;}} class Contact {int telephont; String address; public Contact (int telephont, String address) {// super (); this. telephont = telephont; this. address = address ;}@ Override public String toString () {return "Contact [telephont =" + telephont + ", address =" + address + "]" ;}}View Code
--- Restore content start --- Map set: HashMap, HashTable, and LinkedHashMap HashTable implementation principle: Based on the hash table implementation, the objects used as keys must implement the hashCode () and equals () methods, because the key in the Map is unique. Note that the key and value of HashTable cannot be null. Thread Synchronization and security. The implementation principle of HashMap is the same as that of HashTable, but the key in HashMap can be null, and the value can also be null. Jdk1.2 appears in step jdk1.0. The thread is not synchronized and is not secure.View Code
Summary: although the Person method does not implement the hashCode and equals methods, there are two methods in the Object class. When m1 calls the put method, it calls the two methods in the Object to implement storage.
First, call the hashCode method to generate a hash code value for this object, and find the position of this object in the hash table through the hash code value, if the location is already occupied by other objects, compare the hash values of the two objects.
If the two hash values are the same, the equals method is called for comparison. If the calculation results are the same, the comparison results are not stored. If the comparison results are different, the object will be stored in this location.
If the two hash values are different, the object will be stored in this location.