1. 程式人生 > >Java類集總結之二

Java類集總結之二

iter return lib 標準 value next() private 叠代 方法

1)Map接口

關系:Map(接口)

HashMap(非抽象子類)、TreeMap(非抽象子類)

在開發中,Map集合的內容多用來查詢,全部輸出的操作較少;而Collection接口在開發中的主要作用就是用來傳遞內容及輸出的。

2)

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 public class HashMapDemo01 {
 5 
 6     public static void main(String[] args) {
 7         Map<String,String> map = null
; 8 map = new HashMap<String,String>(); //利用HashMap實例化Map 9 10 map.put("mldn", "www.mldn.cn"); 11 map.put("zhangsan", "www.zhangsan.cn"); 12 map.put("lisi", "lisi"); 13 String val = map.get("zhangsan"); 14 15 System.out.print("取出的內容是:"+val);
16 } 17 }

3)

排序子類TreeMap

key進行排序。

Map接口中的內容不能直接使用叠代輸出因為每個位置存放都是一對值

4)

 1 import java.util.Iterator;
 2 import java.util.Map;
 3 import java.util.Set;
 4 import java.util.TreeMap;
 5 
 6 public class TreeMapDemo01 {
 7 
 8     public static void main(String[] args) {
 9         Map<String,String> map = null
; 10 map = new TreeMap<String,String>(); 11 map.put("b", "www.b.cn"); 12 map.put("a", "wwww.a.cn"); 13 map.put("c", "www.c.cn"); 14 15 Set<String> keys = map.keySet(); 16 Iterator it = keys.iterator(); 17 18 while(it.hasNext()){ 19 String str = (String) it.next(); 20 System.out.println(str + "……" + map.get(str)); 21 } 22 } 23 }

5)Map輸出的最標準操作流程:

 1 import java.util.HashMap;
 2 import java.util.Iterator;
 3 import java.util.Map;
 4 import java.util.Set;
 5 
 6 public class IteratorDemo04 {
 7 
 8     public static void main(String[] args) {
 9         Map<String, String> map = null;
10         
11         map = new HashMap<String, String>();
12         map.put("zhangsan", "www.zhangsan.cn");
13         map.put("lisi", "www.lisi.cn");
14         Set<Map.Entry<String, String>> allSet = null;
15         allSet = map.entrySet();
16         Iterator<Map.Entry<String, String>> iter = null;
17         iter = allSet.iterator();
18         while(iter.hasNext()){
19             Map.Entry<String, String> me = iter.next();
20             System.out.println(me.getKey() + "……" + me.getValue());
21         }
22     }
23 }

6)非系統類直接作為key

和在HashSet中存儲非系統類一樣,必須在自定義類中覆寫Object類的hashCode()、

equals()和toString()方法,之後便能和使用系統類(如String類)那樣,可以通過匿名對象找到相應的value

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 class Person{
 5     private String name;
 6     private int age;
 7     public Person(String name, int age){
 8         this.name = name;
 9         this.age = age;
10     }
11     public boolean equals(Object obj){
12         if(this == obj){
13             return true;
14         }
15         if(!(obj instanceof Person)){
16             return false;
17         }
18         Person p = (Person) obj;
19         if(this.name.equals(p.name)&&(this.age ==p.age)){
20             return true;
21         }else{
22             return false;
23         }
24     }
25     public int hashCode(){
26         return this.name.hashCode() * this.age;
27     }
28     public String toString(){
29         return this.name + "……" + this.age;
30     }
31 }
32 
33 public class HashMapDemo08 {
34 
35     public static void main(String[] args) {
36         Map<Person, String> map = null;
37         map = new HashMap<Person, String>();
38         Person per = new Person("張三", 30);
39         map.put(per, "zhangsan");
40         System.out.println(map.get(per));
41     }
42 
43 }

HashMap類和TreeMap類中的key不能重復,如果重復就會被覆蓋。

IdentityHashMap類中,key可以重復, 只要兩個對象的地址不同即可。

Java類集總結之二