一、Map接口:
1)功能:
2)javascript
Map<String,String>m=new HashMap<String,String>();
//添加元素,元素無序
System.out.println("map.put("cc","coco"));//null
//替換鍵值,返回該鍵的上一個值,若鍵不存在,返回null
System.out.println(map);//cc=coco,左邊是鍵,右邊是值
//map.clear();//清除全部元素
//System.out.printn(map.remove("cc"));//coco。若鍵存在,則刪除該鍵,返回該鍵的鍵值。若不存在,返回null
System.out.println(map.containsKey("cc"));true.//集合存在該鍵,則返回true。若不在,則返回false
System.out.println(map.containsKey(map.isEmpty()));//集合爲空,則返回false
System.out.println(map.containsKey(map.size);//返回集合中鍵的數目
//獲取功能
System.out.println((map.get(cc));//coco。將獲取該鍵的鍵值,若鍵不存在,將返回null
Set<String> set=map.keyset();
for(String key:set){
System.out.println(key);
}//將返回全部的鍵
Collection<String>con=map.values();
for(String value:value){
System.out.println(value);
}//將返回全部的鍵值
//Map集合的遍歷-方式1依鍵尋值
//獲取全部的鍵-遍歷鍵的集合,獲取獲得每個鍵-根據鍵去找值
Map<String,String>m=new HashMap<String,String>();
map.put("cc","coco");
map.put("jc","jack");
Set<String> set=map.keyset();
for(String key:set){
Strng value=map.get(key);
System.out.println(value);
}
//Map集合的遍歷-方式2
//獲取全部的鍵值段的集合
//遍歷鍵值對對象的集合,獲得每個鍵值對對象
//根據鍵值對對象獲取鍵和鍵值
//鍵值對對象的表示:Set<Map.Entry<K,V>>entrySet()l;返回鍵值對對象的集合
Set<Map.Entry<String,String>>set=map.entrySet();
for(Map.Entry<String,String>me:set){
String key=me.getKey();
String value=me.getValue();
}
3)兩種遍歷方式的圖解
php
二、HashMap
1)鍵是哈希表結構,能夠保證鍵的惟一性,是Map的接口實現
2)HashMapjava
//建立集合對象
HashMap<String,String>hm=new HashMap<String,String>();
hm.put("cc","coco");
hm.put("jc","jack");
Set<String>set=hm.keySet();
for(String key:Set){
String value=hm.get(key);
System.out.println(value);
}
3)HashMapweb
HashMap<Integer,String>=new HashMap<Integer,String>();
Integer i=new Integer(55);
String s="jack";
hm.put(i,s);
hm.put(14,"coco");
Set<Integer>set=hm,keySet();
for(Integer key:set){
String value=hm.get(key);
}
4)HashMap數組
//Student類略
HashMap<String,Student>hm=new HashMap<String,Student>();
Student s1=new Student("coco",22);
Student s1=new Student("kiki",23);
//添加元素
hm.put("5414",s1);
hm.put("5415",s2);
//遍歷
Set<String>set=hm.keySet();
for(String key:set){
Student value=hm.get(key);
}
5)HashMapapp
//建立集合對象
HashMap<Student,String>hm=new HashMap<Student,String>();
//建立學生對象
Student s1=new Student("coco",22);
Student s1=new Student("kiki",23);
//添加元素
hm.put(s1,"3f4d");
hm.put(s2,"4f4h");
//遍歷
Set<Student>set=hm.keySet();
for(Student key:set){
String value=hm.get(key);
//若出現某幾個對象成員變量相同但出現了屢次該鍵時,在Student類中生成HashCode()和equal()便可
}
三、LinkedHashMap
1)Map接口的哈希表和連接列表實現,具備可預知的迭代順序。即鍵是有序的
2)svg
LinkedHashMap<String,String>hm=new LinkedHashMap<String,String>();
hm.put("1223","dd");
hm.put("1223","wd");
hm.put("1233","qd");
for(String key:set){
String value=hm.get(key);//輸出語句中重複的將覆蓋
}
四、TreeMap類
1)鍵爲紅黑樹結構,能夠保證鍵的排序和惟一性
2)TreeMap集合鍵是String值是String的案例工具
TreeMap<String,String>tm=new TreeMap<String,String>;
tm.put("coco","cc");
tm.put("jack","jc");
set<String>set=tm.eySet();
for(String key:set){
String value=tm.get(key);
//使用的是天然排序
}
3)TreeMap集合鍵是Student值是String的案例ui
TreeMap<String,String>tm=new TreeMap<String,String>(new Comparator<Student>(){
public int compare(Student s1,Student s2){
int num=s1.getAge()-s2.getAge();
//如下是次要條件
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});//比較器排序,按年齡從低到高排序
Student s1=new Student ("kiki",23);
Student s2=new Student ("jiji",24);
tm.put(s1,"5821");
tm.put(s1,"5851");
set<String>set=tm.keySet();
for(String key:set){
String value=tm.get(key);
//要重寫compareTo方法實現Comparable接口才能實現天然排序
@例題1:統計字符串中每一個字符出現的次數
圖解
this
Scannner sc=new Scanner(System.in);
//鍵盤錄入一個字符串
String line=sc.nextLine();
//定義TreeMap集合
TreeMap<Character,Integer>tm=new TreeMap<Character,Integer>();
//把字符串轉換爲字符數組
char[] chs=line,toCharArray();
//遍歷數組
for(char ch:chs){
Integer i=tm.get(ch,1);
}else{
i++;
tm.put(ch,i);
}}
StringBuilder sb=new StringBuilder();
Set<Character>set=tm.keySet();
for(Character key:set){
Integer value=tm.get(key);
sb.append(key).append)("(").append(value).append(")");
String result=sb.toString;
System.out,println(result);
}
@例題3:HashMap嵌套HashMap
//建立集合對象
HashMap<String,HashMap><String,Integer>>czbkMap=new HashMap<String,HashMap><String,Integer>>();
//建立基礎班集合對象
HashMap<String,Integer>jcMap=new HashMap<String,Integer>();
jcMap.put("cc",12);
jcMap.put("bb",14);
//添加到大集合中
czbkMap.put("jc",jcMap);
//建立就業班集合對象
HashMap<String,Integer>jyMap=new HashMap<String,Integer>();
jcMap.put("aa",13);
jcMap.put("dd",15);
//添加到大集合中
czbkMap.put("jy",jyMap);
//遍歷集合
Set<String>czbkMapSet=czbkMap.keySet();
for(String czbkMapKey:czbkMapSet){
HashMap<String,Integer>czbkMapValue=czbkMap.get(czbkMapSet);
Set<String > czbkMapValueSet=czbkMapValueSet.keySet();
for(String czbkMapValueKey:czbkMapValueSet){
Integer czbkMapValueValue=czbkMapValue.get(czbkMapValueKey);
System.out,println(czbkMapValueKey+","czbkValueValue);
}
}
@例題4:HashMap集合嵌套ArrayList
//建立集合對象
HashMap<String,ArrayList<String>>hm=new HashMap<String,ArrayList<String>>();
//建立元素集合1
ArrayList<String>array1=new ArrayList<String>();
array1.add("黃蓋");
array1.add("孫權");
bjczbkMap.put("jc",array1)
bjczbkMap.put("jc",array2)
hm.put("三國","array1");//添加鍵
//建立元素集合2
ArrayList<String>array2=new ArrayList<String>();
array3.add("孫悟空");
array3.add("觀音");
hm.put("西遊記","array2");//添加鍵
//建立元素集合1
ArrayList<String>array3=new ArrayList<String>();
array4.add("賈寶玉");
array4.add("薛寶釵");
bjczbkMap.put("jc",array3)
bjczbkMap.put("jc",array4)
hm.put("紅樓","array3");//添加鍵
//遍歷集合
Set<String>set=hm.keySet();
for(String key:set){
System.out.println(key);
ArrayList<String> array=hmget(key);
for(String s:value){
System.out.println("\t"+s);
}
}
@例題6:ArrayListq嵌套HashMap
//建立集合對象
ArrayList<HashMap><String.String>>array=new ArrayList<HashMap<String,String>>();
//建立元素1
HashMap<String,String>hm1=new HashMap<String,String>();
hm1.put("周瑜","小喬");
hm1.put("呂布","貂蟬");
//把元素添加到array裏
array.add(hm1);
//建立元素2
HashMap<String,String>hm1=new HashMap<String,String>();
hm1.put("牛魔王","鐵扇");
hm1.put("金角","銀角");
//把元素添加到array裏
array.add(hm2);
//建立元素1
HashMap<String,String>hm1=new HashMap<String,String>();
hm1.put("牛肉","牛肉麪");
hm1.put("陳靖仇","於小雪");
//把元素添加到array裏
array.add(hm1);
//遍歷
for(HashMap<String,String>hm:array){
Set<String>set=hm.keySet();
for(String key:set){
String value=hm.get(key);
System.out.println(key+" "+value)
}
}
@例題7:HashMap嵌套HashMap嵌套HashMap
//建立大集合
HashMap<String,HashMap<String,ArrayList<Student>>>czbkMap=new HashMap<String,HashMap<String,ArrayList<Student>>>();
//北京校區
HashMap<String,ArrayList<Student>> bjczbkMap=new HashMap<String,ArrayList<Student>>();
ArrayList<Student>array1=new ArrayList<Student>();
Student s1=new Student("林青霞","27");
Student s2=new Student("風清揚","30");
array1.add(s1);
array1.add(s2);
ArrayList<Student>array2=new ArrayList<Student>();
Student s3=new Student("董存瑞","17");
Student s4=new Student("李雲龍","60");
array2.add(s3);
array2.add(s4);
//其他類比再也不給出
//遍歷集合
Set<String>czbkMapSet=czbkMap.keySet();
for(String czbkMapKey:czbkMapSet){
for(String czbkMapKey:czbkMapSet){
System,out.println(czbkMapKey);
HashMap<String,ArrayList<Student>>czbkMapValue=cabkMap.get(czbkMapKey);
set<String>czbkMapValueSet=czbkMapValue.keySet();
for(String czbkMapValueKey:czbkMapValueSet){
ArrayList<Student>czbkMapValueValue=czbkMapValue.get(czbkMapValueKey);
for(Student s:czbkMapValueValue){
System.out.println(s.getName);
}
}
}
}
@例題8:HashMap與Hashtable的區別
Hashtable:此類實現一個哈希表,與HashMap用法幾乎同樣,但它不容許null鍵和null值 。它是同步的,HashMap是不一樣步的。
@例題9:Ltst,Set,Map接口是否都繼承自Map接口?
List,Set不繼承自Map接口,它們繼承自Collection接口,Map自己就是一個頂層接口
2、Collections類:
1)針對集合操做的工具類
2 )Collection與Collections的區別:前者是單列集合的頂層接口。後者是對集合操做的工具類
3)成員方法
①public static <T> void sort(List<T>List)
排序。默認進行天然排序
public static <T> int binarySearch(List<?>list,T key)
進行二分查找
public static <T> max(collection<?> coll)
求最大值
public static void reverse(List<?>list)
反轉
public static void shuffle (List<?>list)
隨機置換
List<Integer>list=new ArrayList<Integer>()'
list.add(20);
list.add(30);
list.add(10);
list.add(60);
list.add(5);
list.add(200);
Collections.sort(list);
int index=Collections(list,10);
System.out.println(Collections.max(list));
Collections.reverse(list);
Collections.shuffle(list);
@例題10:ArrayList存儲自定義對象並排序
//學生類略
Public class Student implements Comparable<Student>{
public int compareTo(Student s){
int num=this.age-s.age;
int num2=num==0?this.name.comparaTo(s.name):num;
return num2;
}
List<Student>list=new ArrayList<Student>();
Student s1=new Student("huh",34);
Student s2=new Student("hh",44);
list.add(s1);
list.add(s2);
//天然排序
//collections.sort(list);
//比較器排序,以比較器爲主
Collections.sort(list,new Comparator)<Student>(){
public int compare(Student s1,Student s2){
int num=s2.getAge()-s1.getAge();
return num;
}
})
for(Student s:List){
System.out.println(s.getName())
}
@例題11:模擬鬥地主洗牌和發牌
ArrayList<String> array=new ArrayList<String>();
String[] colors={"方塊","梅花","紅桃","黑桃"}
String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for(String color:colors){
for(String number:numbers){
array.add(color.concat(number));
}
}
array.add(大王);
array.add(小王);
//洗牌
Collections.shuffle(array);
//發牌
ArrayList<String>p1=new ArrayList<String>();
ArrayList<String>p2=new ArrayList<String>();
ArrayList<String>p3=new ArrayList<String>();
ArrayList dipai=new ArrayList<String>();
for(int x=0;x<array.size();x++){
if(x>array.size()-3){
diPai.add(array.get(x));
else if(x%3==0){p1.add(array.get(x))
}
else if(x%3==1){p2.add(array.get(x))
}
else if(x%3==2){p3.add(array.get(x))
}
}
}
//看牌
lookPoker("p1",p1);
lookPoker("p2",p2);
lookPoker("p3",p3);
public static void lookPoker(String name,ArrayList<String>array){
System.out.println(name+"的牌是");
for(String s:array){
System.out.println(s+" ")
}
System.out.println(" ")
}
圖解:
//完整代碼
//建立一個HashMap集合
//建立一個ArrayList集合
//建立花色和點數數組
//從0開始往HashMap裏面存儲編號,並存儲對應的牌。同時往ArrayList裏面存儲編號便可
//洗牌(洗的是編號)
//發牌(發的是編號,爲了使得在看牌時編號是排序的,就建立TreeSet集合接收)
//看牌(遍歷TreeSet集合,獲取編號,到HashMap裏找對應的牌)
//建立一個HashMap集合
// 建立一個HashMap集合
HashMap<Integer, String> hm = new HashMap<Integer, String>();
// 建立一個ArrayList集合
ArrayList<Integer> array = new ArrayList<Integer>();
// 建立花色數組和點數數組
// 定義一個花色數組
String[] colors = { "♠", "♥", "♣", "♦" };
// 定義一個點數數組
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 從0開始往HashMap裏面存儲編號,並存儲對應的牌,同時往ArrayList裏面存儲編號便可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是編號)
Collections.shuffle(array);
// 發牌(發的也是編號,爲了保證編號是排序的,就建立TreeSet集合接收)
TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
TreeSet<Integer> linQingXia = new TreeSet<Integer>();
TreeSet<Integer> liuYi = new TreeSet<Integer>();
TreeSet<Integer> diPai = new TreeSet<Integer>();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍歷TreeSet集合,獲取編號,到HashMap集合找對應的牌)
lookPoker("風清揚", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("劉意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 寫看牌的功能
public static void lookPoker(String name, TreeSet<Integer> ts,
HashMap<Integer, String> hm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}