黑马Java学习笔记之-----集合框架
----------------------?android培訓、java培訓、期待與您交流!?----------------------
?
一.概述:
Java的集合類是一種特別有用的工具類,它可以用于存儲數(shù)量不等的多個對象(實際上是對象的引用),并可以實現(xiàn)常用的數(shù)據(jù)結構,如棧,隊列等。除此之外,還可以用于保存具有映射關系的關聯(lián)數(shù)組。Java的集合大致上可分為:Set,List和Map三種體系,其中,Set代表無序,不可重復的集合;List代表有序,重復的集合;而Map則代表具有映射關系的集合。
Java的集合類主要由兩個接口派生而出:Collection和Map。(HashSet,ArrayList和HashMap是最常用的實現(xiàn)類)
?
?
(二)Collection和Iterator接口:
1.?Collection接口定義的操作集合元素的常用方法:
?
2.?Iterator接口主要用于遍歷(即迭代訪問)Collection中的元素,Iterator對象也被稱為 ??迭代器。Iterator接口定義了如下三個方法:
?
(三)Set接口:
Set集合與Collection基本上完全一樣,它沒有提供任何額外的方法。實際上Set就是Collection,只是行為不同(Set不允許出現(xiàn)重復元素)。
Set判斷兩個對象相同不是使用==運算符,而是根據(jù)equals方法。也就是說,如果只要兩個對象用equals方法比較返回true,Set就不會接受這兩個對象。
1.?HashSet類:
HashSet是Set接口的典型實現(xiàn)。
HashSet具有以下特點:
a.?不能保證元素的排列順序,順序有可能發(fā)生變化。
b.?HashSet不是同步的。
c.?集合元素值可以是null。
? ??HashSet集合判斷兩個元素相等的標準是兩個對象通過equals方法比較相等,并且兩 ??個對象的hashCode()方法返回值也相等。
注意:如果需要某個類的對象保存到HashSet集合中,重寫這個類的equals()方法和
??hashCode()方法時,應該保證兩個對象通過equals比較返回true時,他們的hashCode ??方法返回值也相等。
重寫hashCode()方法的基本原則:
1.?當兩個對象通過equals方法比較返回true時,這兩個對象的hashCode應該相等。
2.?對象中用作equals比較標準的屬性,都應該用來計算hashCode值。
?
2.?TreeSet類:
TreeSet是SortedSet接口的唯一實現(xiàn),TreeSet可以保證集合元素處于排序狀態(tài)。
與HashSet相比,TreeSet還提供了幾個額外的方法:
?
注意:如果試圖把一個對象添加進TreeSet時,則該對象的類必須實現(xiàn)Comparable接口,否 ??則程序可能跑出異常。
?
?
(四)List接口:
List集合代表一個有序集合,允許元素重復。List集合默認按元素的添加順序設置元素的索引。
1.?List接口和ListIterator接口
List集合常用方法:
?
ListIterator接口繼承了Iterator接口,提供了專門操作List的方法。ListIterator接口在Iterator接口的基礎上增加了以下方法:
?
ListIterator增加了向前迭代的功能。
?
2.?ArrayList和Vector實現(xiàn)類:
?
??ArrayList和Vector類都是基于數(shù)組實現(xiàn)的List類,所以ArrayList和Vector類封裝了 ??一個動態(tài)再分配的Object[?]數(shù)組。
?
??ArrayList是線程不安全的,程序必須手動保證該集合的同步性;Vector是線程安全的。
?
?各種容器的性能比較:
?
(五)Map:
Map用于保存具有映射關系的數(shù)據(jù)。Map的key不允許重復,即同一個Map對象的任何兩個key通過equals方法比較總是返回false。
Map接口定義的常用方法:
?
Map中包含一個內部類:Entry。該類封裝了一個key--value對,Entry包含三個方法:
?
1.?HashMap和Hashtable實現(xiàn)類:
Hashtable和HashMap的區(qū)別:
1.?Hashtable是一個線程安全的實現(xiàn),但HashMap是線程不安全的實現(xiàn)。
2.?Hashtable不允許使用null作為key和value,如果試圖把null值放進Hashtable ?中,將會引發(fā)NullPointrException異常;但HashMap可以使用null作為key和value。
?
1 /* 2 Map集合的兩種取出方式: 3 1.Set<k> keySet():將Map集合中的所有鍵存入Set中,疑問Set具備迭代器。 4 所以可以根據(jù)迭代方式取出鍵,再根據(jù)get方法,獲取每一個鍵對應的值。 5 6 2.Set<Map.Entry<k,v>> entrySet():將Map集合中的映射關系存入到Set集合中, 7 而這個映射關系的數(shù)據(jù)類型就是:Map.Entry。 8 9 */ 10 11 import java.util.*; 12 13 class Test1 14 { 15 public static void main(String[] args) 16 { 17 HashMap<String,String> hm = new HashMap<String,String>(); 18 hm.put("null","null"); 19 hm.put("java01","3344"); 20 hm.put("java02","3444"); 21 hm.put("java03","33244"); 22 23 Set<String> s = hm.keySet(); 24 Iterator<String> it = s.iterator(); 25 while (it.hasNext()) 26 { 27 String key = it.next(); 28 System.out.println(key+":"+hm.get(key)); 29 } 30 } 31 } 32 33 34 class Test2 35 { 36 public static void main(String[] args) 37 { 38 HashMap<String,String> hm = new HashMap<String,String>(); 39 hm.put("null","null"); 40 hm.put("java01","3344"); 41 hm.put("java02","3444"); 42 hm.put("java03","33244"); 43 //將Map集合中的映射關系取出,存入到Set集合中。 44 Set<Map.Entry<String,String>> s = hm.entrySet(); 45 Iterator<Map.Entry<String,String>> it = s.iterator(); 46 while (it.hasNext()) 47 { 48 Map.Entry<String,String> me = it.next(); 49 String key = me.getKey(); 50 String value = me.getValue(); 51 System.out.println(key+":"+value); 52 } 53 } 54 }?
1 /* 2 練習: 3 每一個學生都有對應的歸屬地, 4 學生Student,地址String 5 注意:姓名和年齡相同的視為同一學生 6 保證學生的唯一性。 7 8 1. 描述學生類,重寫equals和hashCode方法。(有序則重寫Comparable接口中的compareTo方法) 9 2. 定義Map容器,將學生作為鍵,地址作為值,存入。 10 3. 獲取Map集合中的元素,兩種方式:keySet和entrySet。 11 12 */ 13 14 15 import java.util.*; 16 17 class Student 18 { 19 private String name; 20 private int age; 21 22 Student(String name, int age) 23 { 24 this.name = name; 25 this.age = age; 26 } 27 public String toString() //重寫toString方法 28 { 29 return name+":"+age; 30 } 31 public boolean equals(Object obj) //重寫equals方法 32 { 33 if (!(obj instanceof Student)) 34 { 35 throw new ClassCastException("類型不匹配!"); 36 } 37 Student s = (Student)obj; 38 return this.name.equals(s.name)&&this.age==s.age; //名字和年齡都相等則返回true 39 } 40 public int hashCode() //重寫hashCode方法 41 { 42 return name.hashCode()+age*17; 43 } 44 45 } 46 47 public class Practice 48 { 49 public static void main(String[] args) 50 { 51 Map<Student,String> hm = new HashMap<Student,String>(); 52 hm.put(new Student("xiaok",22),"Beijing"); 53 hm.put(new Student("xiaok",22),"Nanjing"); 54 hm.put(new Student("xiaoh",19),"Tianjing"); 55 hm.put(new Student("xiaol",23),"Fuzhou"); 56 57 //獲取Map元素第一種方式:keySet 58 Set<Student> s = hm.keySet(); 59 Iterator<Student> it = s.iterator(); 60 while (it.hasNext()) 61 { 62 Student key = it.next(); 63 System.out.println(key+":::"+hm.get(key)); 64 } 65 66 System.out.println("###########################"); 67 68 //獲取Map元素第二種方式:entrySet 69 70 Set<Map.Entry<Student,String>> entrySet = hm.entrySet(); 71 Iterator<Map.Entry<Student,String>> it2 = entrySet.iterator(); 72 while (it2.hasNext()) 73 { 74 Map.Entry<Student,String> me = it2.next(); 75 Student ss = me.getKey(); 76 String addr = me.getValue(); 77 System.out.println(ss+"....."+addr); 78 } 79 } 80 } 1 /* 2 練習: 3 獲取字符串中字母出現(xiàn)的次數(shù) 4 如:"adfadff" 打印:a(2)d(2)f(3)... 5 6 思路: 7 1. 將字符串轉換成字符數(shù)組,因為要對每一個字母進行操作。 8 2. 定義一個Map集合,因為打印結果的字母有順序,所以使用TreeMap集合。 9 3. 遍歷字符數(shù)組: 10 將每一個字母作為鍵去查Map集合,get(key)方法。 11 如果返回null,將該字母和1存入到Map集合 12 如果返回不是null,說明該字母在Map集合中存在并有對應次數(shù)。 13 那么就獲取該次數(shù)并進行自增,然后將該字母和自增后的次數(shù)存入到Map集合中, 14 4. 將Map集合中的數(shù)據(jù)編程指定的字符串形式返回。 15 16 */ 17 import java.util.*; 18 19 public class Practice2 20 { 21 public static void main(String[] args) 22 { 23 charCount("adfadfads"); 24 } 25 26 public static void charCount(String str) 27 { 28 char[] chArr = str.toCharArray(); //將字符串轉換成字符數(shù)組 29 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(); //定義一個Map集合 30 for (int i=0; i<chArr.length ; i++ ) //遍歷字符數(shù)組 31 { 32 Integer value=tm.get(chArr[i]); 33 if (value==null) 34 { 35 tm.put(chArr[i],1); 36 } 37 else 38 { 39 value++; 40 tm.put(chArr[i],value); 41 } 42 } 43 Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet(); 44 Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator(); 45 while (it.hasNext()) 46 { 47 Map.Entry<Character,Integer> me = it.next(); 48 Character key = me.getKey(); 49 Integer value = me.getValue(); 50 System.out.print(key+"("+value+")"); 51 } 52 53 } 54 }?
?
?
----------------------?android培訓、java培訓、期待與您交流!?----------------------
?
?
轉載于:https://www.cnblogs.com/MercyK/archive/2013/04/29/3014737.html
總結
以上是生活随笔為你收集整理的黑马Java学习笔记之-----集合框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西门子安装未找到ssf文件_西门子Ste
- 下一篇: Java垃圾回收机制(GC原理)解析