Map集合概述特點(diǎn)
一個(gè)映射不能有重復(fù)的鍵耕赘,每個(gè)鍵最多映射到一個(gè)值
Map和Collection接口的區(qū)別
Map是雙列的骄蝇,而Collection是單列的
Map的鍵是唯一的,而Collection的子體系Set(TreeSet HashSet LinkedHashSet)是唯一的
Map集合的數(shù)據(jù)結(jié)構(gòu)針對鍵有效操骡,與值無關(guān)九火,Collection集合的數(shù)據(jù)結(jié)構(gòu)是針對元素有效
注意:在HashSet底層執(zhí)行的是Map的操作
v = put(key,value)指定的值與映射關(guān)聯(lián)(v返回的是被覆蓋的值)
相同的鍵不存儲(chǔ),僅僅值覆蓋
v = remove(key);根據(jù)鍵刪除元素册招,返回鍵對應(yīng)的值
boolean flg = map.containsKey(key);
boolean flg = map.containsValue(value);
map.size();
-----------------------------------------------------
Map集合的遍歷(沒有迭代器岔激,不能直接迭代)
Set<E> keySet = map.keySet();
Iterator<E> it = keSet.iterator();
while(it.hasNext()){
E e = it.next;
T t = map.get(e);? ?
}
-----------------------------------------------------
利用增強(qiáng)for循環(huán)
for(E keyE:map.keySet()){
? T t = map.get(keyE);
}
HashMap<String,Integer> hm = new HashMap();
hm.put("",23);
Set<Map.Entry<String,Integer>> entrySet = hm.entrySet();
Iterator it = entrySet.iterator();
for(it.hasNext){
Entry<String,Integer> en = hm.entrySet();
String key = en.getKey();
String value =en.getValue();
}
或者使用增強(qiáng)for
for(Entry<String,Integer> en:hm.entry.Set()){}
-----------------------------------------------------
集合框架(如何保證鍵的唯一)
HashMap<Student,String> hm = new HashMap<>();
hm.put(new Student("",""),str);
必須重寫HashCode方法和equals方法
LinkedHashMap的概述和使用
LinkedHashMap<String,Integer> lhm = new LinkedHashMap<>();
lhm.put("xx",23);
-------------------------------------------------------
HashMap嵌套
HashMap<Student,String> hm = new HashMap<>();
hm.put(new Student("zs"23,),"北京");
HashMap<HashMap<Student,String>, String> hm2 = new HashSet();
for(HashMap<Student,String> h:hm2.keySet()){
String value = h.get(h);
//遍歷鍵的雙列集合對象
for(Student key:h.keySet()){
String value2 = h.get(key);
}
}
---------------------------------------------------------
TreeMap
TreeMap<Student,String> tm = new TreeMap<>();
tm.put(new Student("",xxx),"");
需要實(shí)現(xiàn)Comparable接口
return num==0?this.name.equals(another):num;
或者
TreeMap<Student,String> tm = new TreeMap<>(new Comparator(Student){
public int compare(Student s1,Student s2){
return num == 0?s1.xxx.equals(s2..x):num;
? ? }
});
根據(jù)鍵值獲取每個(gè)字符出現(xiàn)的次數(shù)
思路 如果出現(xiàn)的鍵相同,則使用HashMap 如果出現(xiàn)重復(fù)的鍵是掰,則使用
關(guān)鍵語句:hm.containsKey(c );
----------------------------------------
Hashtable (Vector類似)被HashTable去除了
HashMap 是線程不安全的鹦倚,效率高JDK1.2
HashTable是線程安全的,效率低 JDK1.0
HashMap可以存Null鍵和Null值
Hashtable 不能存儲(chǔ)null值和null鍵
----------------------------------------------
Collections工具類常用概述
Public static<T> void sort(List<T> list);
public static<T> int BinarySearch(List<T> list,T? key);
如果搜索鍵在list中冀惭,則返回索引震叙,如果沒有則返回(-插入點(diǎn)-1)
Collections.max(list); 根據(jù)默認(rèn)結(jié)果獲取集合最大值
Collections.reverse(list); 反轉(zhuǎn)集合
Collection.shuffle(list);隨機(jī)置換?
-----------------------------------------------
斗地主:
利用HashMap 并且根據(jù)索引獲取值,并將索引放入Arraylist洗牌散休,最后發(fā)牌后放入到TreeSet集合中
------------------------------------------------
泛型固定下邊界
<媒楼?super E>
泛型固定上邊界
<? extends E>