Java集合(三)
一、Map接口
- 實現Map接口的類用來存儲“鍵–值”對。
- Map接口的實現類有HaspMap和TreeMap等
- Map類中存儲的“鍵–值”對通過鍵來表示,所有鍵值不能重複,也就是說鍵值之間不能equals,實際當中他們比較的是hashcode是否相同,因爲這樣效率會比較高。
- Map接口常見的方法:
Object put(Object key,Object value);//添加一個鍵值對
Object get(Object key);//通過key找到相對應的value對象
Object remove(Object key);//通過key把對應的value去掉,key也會自動會被去掉
boolean containKey(Object key);//是否包含某個鍵
boolean containsValue(Object value);//是否包含某個值
int size();//一共裝了多少對對象
boolean isEmpty();//是否爲空
void putAll(Map t);//把另外一個Map集合裏面的所有元素添加進去
void clear();//清空集合
示例如下:
public class MapTest {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new TreeMap();
m1.put("one",new Integer(1));
m1.put("two",new Integer(2));
m1.put("three",new Integer(3));
m2.put("A",new Integer(1));
m2.put("B",new Integer(2));
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
System.out.println(m2.containsValue(new Integer(1)));//其實內部仍然會執行equals和hashcode方法,但是Integer類已經重寫了,所以可找到
if (m1.containsKey("two")){
int i = ((Integer) m1.get("two")).intValue();
System.out.println(i);
}
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
打印結果如下:
二、自動打包和解包
Auto-boxing、Auto-unboxing
在合適的時機自動打包、解包
自動將基礎類型轉換成對象(打包)
自動將對象轉換爲基礎類型(解包)
根據這個特性上面的程序可以修改爲:
public class MapTest {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new TreeMap();
// m1.put("one",new Integer(1));
m1.put("one",1);
// m1.put("two",new Integer(2));
m1.put("two",2);
// m1.put("three",new Integer(3));
m1.put("three",3);
// m2.put("A",new Integer(1));
m2.put("A",1);
// m2.put("B",new Integer(2));
m2.put("B",2);
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
// System.out.println(m2.containsValue(new Integer(1)));
System.out.println(m2.containsValue(1));//其實內部仍然會執行equals和hashcode方法,但是Integer類已經重寫了,所以可找到
if (m1.containsKey("two")){
// int i = ((Integer) m1.get("two")).intValue();
int i = (int) m1.get("two");
System.out.println(i);
}
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
三、泛型
起因–JDK1.4以前類型不明確:
- 裝入集合的類型都被裝作Object對待,從而失去自己的實際類型。
- 從集合中取出時往往需要轉型,效率低,並且容易產生錯誤。
解決辦法:
- 在定義的時候同時定義集合中對象的類型。
- 可以在定義Collection的時候指定
- 可以在循環時用Iterator指定
- 好處:增強程序的可讀性和穩定性
- 示例如下:
import java.util.*;
public class BasicGeneric {
public static void main(String[] args) {
//指定list集合內部只能是String對象
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
for(int i = 0;i < list.size();i++){
String s = list.get(i);
System.out.println(s);
}
Collection<String> collection = new HashSet<>();
collection.add("aaa");
collection.add("bbb");
collection.add("ccc");
for(Iterator<String> iterator = collection.iterator();iterator.hasNext();){
//在循環時用iterator指定泛型
String s = iterator.next();
System.out.println(s);
}
}
}
輸出結果:
Comparable接口也可以指定泛型,同類之間比較纔有意義:
class MyName implements Comparable<MyName>{
int age;
public int compareTo(MyName mm){
if(this.age > mm.age){
return 1;
}else if(this.age < mm.age){
return -1;
}else{
return 0;
}
}
}
泛型示例2:
public class MapTest {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("one",1);
map.put("two",2);
map.put("three",3);
System.out.println(map.size());
System.out.println(map.containsKey("one"));
if(map.containsKey("two")){
//用了泛型無需再用強制類型轉換
int i = map.get("two");
System.out.println(i);
}
}
}
輸出結果:
四、總結
- 一個圖,java集合之間關係圖
- 一個類 Collections
- 三個知識點:增強for循環,泛型,自動打包解包
- 六個接口:collection、set、list、map、iterator、comparable。其中iterator接口標準的統一的來遍歷一個collection的方式,comparable可以應用泛型,來定義這個類兩個對象之間誰大誰小。