Map集合
-------------
1、Map與Collection不同
2、Map集合存儲於取出元素的方式
3、Map集合的特點
4、Map集合中常用類
Map與Collection
1、Map與Collection在集合框架中屬並列存在
2、Map存儲的是鍵值對
3、Map存儲元素使用put方法,Colletion使用add方法
4、Map集合沒有直接取出元素的方法,而是先轉成Set集合,在通過迭代獲取元素
5、Map集合中鍵要保證唯一性
hashMap的每個元素叫條目
所有的kev位於set當中
1、Hashtable:線程安全,速度慢,不允許存放null鍵,null值,已被HashMap替代
2、HashMap:線程不安全,速度快,允許存放null鍵,null值
3、TreeMap: 對鍵進行排序,排序原理與TreeSet相同
----------------------
拿出一個Map得到所有的kev:
Set set=map.keySet();是Map集合的方法
map.get(kev) //通過kev獲得value
//Map代碼案例參考:
public static void main(String[] args) {
Map<String,Dog> map = new HashMap<String, Dog>();
Dog dog = new Dog ("" , 12, "");
map.put(null, null);
map.put("dog-1000-1981", dog);
map.put("dog-1000-1982", dog);
map.put("dog-1000-1983", new Dog("",12,""));
map.put("dog-1000-1984",new Dog("",12,""));
map.put("dog-1000-1985",new Dog("",12,""));
System.out.println(map.size());
Set<Entry<String,Dog>> entries = map.entrySet();
//循環map的所有的entry
for(Entry<String,Dog> entry : entries){
String key = entry.getKey();
Dog d = entry.getValue();
System.out.println(key + ":" + d.toString());
}
//map刪除
Dog d2 = map.remove("dog-1000-1984");
System.out.println("刪除了" + d2);
d2 = map.remove("dog -1000-1984");
System.out.println("刪除了" + d2);
//通過key遍歷map
Set<String> keySet = map.keySet();
for(String str : keySet){
Dog value =map.get(str);
}
}
}
------------------------
TreeSet
-----------------------------
1、使用比較方法判斷對象是否重複
2、比較方法實現有兩種
a、自定義Comparator比較器,和TreeeSet關聯
b、讓javaBean實現Comparable接口,實現CompareTo()方法
3、TreeSet可以容納null元素
4、TreeSet可以使用降序排序,通過descendingIterator()方法得到降序迭代器實現
5、TreeSet默認升序排列
6、TreeSet是依靠TreeMap來實現的。
TreeSet是一個有序集合,TreeSet中的元素將按照升序排列,缺省是按照自然排序進行排列,意味着TreeSet中的元素要實現Comparable接口。或者有一個自定義的比較器。
我們可以在構造TreeSet對象時,傳遞實現Comparator接口的比較器對象。
-------------------
public class TeerSet {
private static final Comparator<? super Person> comp = null;
public static void main(String[] args) {
/*
* 自定義比較器,實現的是對象大小比較
* 按照年齡判斷大小情況
*/
Comparable<Person> comp =new Comparable<Person>() {
public int compareTo(Person o1,Person o2) {
//o1==null
if(o1==null){
if(o2==null){
return 0;
}
else{
return -1;
}
}
//o1!=null
else{
if(o2==null){
return 1;
}
else{
return o1.getAge()-o2.getAge();
}
}
}
@Override
public int compareTo(Person o) {
// TODO Auto-generated method stub
return 0;
};
};
}
//通過比較器構造TreeSet集合
TreeSet<Person> tr=new TreeSet<Person> (comp);
tr.add(null);
tr.add(new Person("p1",10));
tr.add(new Person("p2",20));
tr.add(new Person("p3",30));
tr.add(new Person("p4",40));
System.out.println(tr.size());
for(Iterator<Person> it=tr.descendingIterator();it.hadsNext();){
Person p=it.next();
System.out.println(p!=null ?p.getName():"Nobody");
}
/*
* 使用comprable接口實現對象大小的比較
*
*/
TreeSet<Dog> trr=new TreeSet<Dog>();
trr.add(new Dog("black",20,"金巴"));
trr.add(new Dog("black",12,"金巴"));
trr.add(new Dog("white",20,"金巴"));
trr.add(new Dog("black",20,"藏獒"));
trr.add(new Dog("yellow",20,"藏獒"));
System.out.println(trr.szie());
//進行拍排序(升序輸出),用遍歷迭代器的方法(降序的話把trr.iterator改成trr.descendingIterator就ok)
for(Iterator<Dog> it = trr.iterator();it.hasNext(){
System.out.println(it.next());//調用了toString方法,it.next返回的是對象
}
}
}