java集合

---恢复内容开始---

Map集合:HashMap、HashTable、LinkedHashMap、TreeMap

    一:HashTable、HashMap

    HashTable实现原理:基于哈希表实现,用作键的对象必须实现hashCode()和equals()方法,因为在Map中键是唯一的。注意HashTable中键和值都不允许为null。

              线程同步,安全。

    HashMap的实现原理:与HashTable原理一样,但是在HashMap中键允许为null,值也允许为null。jdk1.2中出现步jdk1.0中出现。线程不同步,不安全。

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,"湖南");
        Contact c2 = new Contact(378989,"北京");
        Contact c3 = new Contact(378989,"北京");
        Contact c4 = new Contact(378989,"北京");
        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
---恢复内容开始---

Map集合:HashMap、HashTable、LinkedHashMap

    HashTable实现原理:基于哈希表实现,用作键的对象必须实现hashCode()和equals()方法,因为在Map中键是唯一的。注意HashTable中键和值都不允许为null。

              线程同步,安全。

    HashMap的实现原理:与HashTable原理一样,但是在HashMap中键允许为null,值也允许为null。jdk1.2中出现步jdk1.0中出现。线程不同步,不安全。

 
View Code

  总结1:虽然Person没有实现hashCode和equals方法,但是Object类中有这两个方法,m1在调用put方法时会调用Object里面的这两个方法,实现存储。

       首先调用hashCode方法生成一个该对象的哈希码值,通过哈希码值找到该对象在哈希表中的位置,如果该位置已经被其他对象占用了则比较这两个对象的哈希码值。

     若这两个哈希码值一样,那么调用equals方法比较,若计较结果一样,则不予存储,若比较结果不一样,则在此位置顺延一下存储该对象。

      若这两个哈希码值不一样则在该位置顺延一下存储该对象。

 1 package collection;
 2 
 3 import java.util.Map;
 4 import java.util.*;
 5 
 6 public class MyMap_entrySet {
 7     public static void main(String[] args) {
 8         
 9         Person3  p1 = new Person3(373726,"xiaoming");
10         
11         Person3  p2 = new Person3(373726,"xiaoming");
12         System.out.println(p1==p2);
13         Contact3 c1 = new Contact3(13188778777l,"shanghai");
14         Contact3 c2 = new Contact3(13188778777l,"beijing");
15         Map m1 = new HashMap();
16         m1.put(p1, c1);
17         m1.put(p2, c2);
18         System.out.println(m1);
19         
20     }
21 
22 }
23 class Person3 {
24     int id ;
25     String name;
26     public Person3(int id, String name) {
27         //super();
28         this.id = id;
29         this.name = name;
30     }
31     
32     
33 
34     
35     @Override
36     public int hashCode() {
37         final int prime = 31;
38         int result = 1;
39         result = prime * result + id;
40         result = prime * result + ((name == null) ? 0 : name.hashCode());
41         //通过name属性调用String 里面的hashCode方法生成哈希码值,当name一样时返回的哈希码值是一样的
42         return result;
43     }
44 
45     @Override//该equals方法比较的是对象里面的内容
46     public boolean equals(Object obj) {
47         if (this == obj)//比较两个对象的地址
48             return true;
49         if (obj == null)
50             return false;
51         if (getClass() != obj.getClass())
52             return false;
53         Person3 other = (Person3) obj;
54         if (id != other.id)//比较两个对象的id
55             return false;
56         if (name == null) {
57             if (other.name != null)
58                 return false;
59         } else if (!name.equals(other.name))//比较两个对象的name
60             return false;
61         return true;
62     }
63 
64     @Override
65     public String toString() {
66         return "Person3 [id=" + id + ", name=" + name + "]";
67     }
68     
69 }
70 class Contact3{
71     long telephont;
72     String address;
73     public Contact3(long telephont, String address) {
74         //super();
75         this.telephont = telephont;
76         this.address = address;
77     }
78     @Override
79     public String toString() {
80         return "Contact3 [telephont=" + telephont + ", address=" + address + "]";
81     }
82 }
HashMap_Override_hashCode()&equals()
false
{Person3 [id=373726, name=xiaoming]=Contact3 [telephont=13188778777, address=beijing]}

 总结2:以上代码是自己在“key”类中实现hashCode()方法和equals()方法,此时就不会调用Object 里面的hashCode方法和equals()方法。原理总结1一样

二:TreeSet

  TreeMap原理:底层是通过二叉树实现。TreeMap中的key对象的类中必须实现Comparable接口,实现Comparable里面的compareTo方法,二叉树根据该方法的返回值

        按照二叉树的规律来实现存储。

 1 package collection;
 2 import java.util.Map;
 3 import java.util.*;
 4 public class MyMap3 {
 5     public static void main(String[] args) {
 6         Map  m1 = new TreeMap(); 
 7         Person4 p1 = new Person4(1332,"xiaoming");
 8         Person4 p2 = new Person4(8332,"ahangsan");
 9         Contact4 c1 = new Contact4(13172673363l,"广州");
10         Contact4 c2 = new Contact4(15172673363l,"杭州");
11         m1.put(p1, c1);
12         m1.put(p2, c2);
13         System.out.println(m1);
14     }
15 
16 }
17 class Person4 implements Comparable {
18     int id ;
19     String name;
20     public Person4(int id, String name) {
21         //super();
22         this.id = id;
23         this.name = name;
24     }
25     public int compareTo(Object obj){
26         Person4 p4 = (Person4)obj ;
27             return this.name.compareTo(p4.name);//根据返回值,按照二叉树结构,进行存储。
28     }                                            //P84《数据结构与算法分析java语言描述》佛罗里达国际大学                
29     @Override
30     
31     public String toString() {
32         return "Person4 [id=" + id + ", name=" + name + "]";
33     }
34     
35     
36     
37 }
38 class Contact4{
39     int telephone;
40     String address;
41     public Contact4(long telephont, String address) {
42         //super();
43         this.telephone = telephone;
44         this.address = address;
45     }
46     @Override
47     public String toString() {
48         return "Contact4 [telephont=" + telephone + ", address=" + address + "]";
49     }
50     
51 }
TreeSet_implements_Comparable

 

 

    

原文地址:https://www.cnblogs.com/huxuebing/p/5752212.html