day09_面向對象Map
一.Map集合的概述
1.什麼是Map集合
Collection集合稱爲單列集合
Map集合稱爲雙列集合
2.Map集合的特點
a.Collection每個元素單獨存在,Map每個元素是成對的
b.Map集合中鍵是唯一的,值是可以重複的,通過鍵只能找到唯一的值
二.Map集合中常用的實現類
1.HashMap集合介紹
a.無序的
b.鍵是唯一的,值不唯一
爲了保證鍵的唯一性,如果鍵是自定義類型,我們需要重寫鍵的hashCode和equals方法
2.LinkedHashMap集合介紹
a.有序的
b.鍵是唯一的,值不唯一
爲了保證鍵的唯一性,如果鍵是自定義類型,我們需要重寫鍵的hashCode和equals方法
3.Map接口中通用的方法*************************
增: V put(K key, V value );添加一個鍵值對,返回null(之前鍵不存在)
刪: V remove(K key);根據鍵刪除鍵值對,返回被刪除鍵值對的值
改: V put(K key, V value );如果鍵已經存在,那麼put方法相當於修改值
查:V get(K key); 根據鍵查找出對應的值
其他:
public boolean containsKey(Object key); 是否包含某個鍵
public boolean containsValue(Object value); 是否包含某個值
public class TestMap {
public static void main(String[] args) {
//1.創建一個Map集合
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
//2.put
Integer v1 = map.put("小米", 1234);
Integer v2 = map.put("華爲", 6888);
Integer v3 = map.put("愛瘋", 8888);
Integer v4 = map.put("三星", 2500);
Integer v5 = map.put("小米", 5500);
//{愛瘋=8888, 華爲=6888, 小米=1234, 三星=2500}
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
System.out.println(v4);
System.out.println(v5);
System.out.println(map);
//3.remove
Integer v6 = map.remove("愛瘋");
System.out.println(v6);
System.out.println(map);
//4.get
Integer v7 = map.get("華爲");
System.out.println(v7);
System.out.println(map);
//5.containsKey ContainsValue
System.out.println(map.containsKey("華爲"));
System.out.println(map.containsValue(4321));
}
}
4.Map的遍歷方式一:以鍵找值方式************************
public class MapKeySetDemo {
public static void main(String[] args) {
//1.創建一個Map集合
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
//2.put
map.put("小米", 1234);
map.put("華爲", 6888);
map.put("愛瘋", 8888);
map.put("三星", 2500);
//3.以鍵找值 keySet
//a.獲取所有的鍵的集合
Set<String> keys = map.keySet();
//b.遍歷所有的鍵
//迭代器,foreach
for (String key : keys) {
//c.以鍵找值
Integer value = map.get(key);
//打印出來
System.out.println(key + "=" + value);
}
}
}
5.Map的遍歷方式二:鍵值對方式**************************
public class MapEntrySetDemo {
public static void main(String[] args) {
//1.創建一個Map集合
LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
//2.put
map.put("小米", 1234);
map.put("華爲", 6888);
map.put("愛瘋", 8888);
map.put("三星", 2500);
//3.Map集合的第二種遍歷方式:鍵值對方式
//a.獲取所有鍵值對的集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
//b.遍歷這個鍵值對的集合
//迭代器,foreach
for (Map.Entry<String, Integer> entry : entries) {
//c.獲取鍵值
String key = entry.getKey();
Integer value = entry.getValue();
//打印
System.out.println(key + "=" + value);
}
}
}
6.練習:使用HashMap/LinkedHashMap保存自定義對象
練習:每位學生(姓名,年齡)都有自己的家庭住址
那麼,既然有對應關係,則將學生對象和家庭住址存儲到map集合中。學生作爲鍵,家庭住址作爲值。
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class TestMapDemo01 {
public static void main(String[] args) {
LinkedHashMap<Student,String> map = new LinkedHashMap<Student, String>();
//保存幾個鍵值對
map.put(new Student("柳巖", 20), "上海");
map.put(new Student("古力娜扎", 30), "北京");
map.put(new Student("馬爾扎哈", 25), "廣州");
map.put(new Student("迪麗熱巴", 18), "我家樓上");
map.put(new Student("鳳姐", 30), "你家隔壁");
map.put(new Student("柳巖", 20), "深圳");
// {
// Student{name='古力娜扎', age=30}=北京,
// Student{name='迪麗熱巴', age=18}=我家樓上,
// Student{name='柳巖', age=20}=上海,
// Student{name='馬爾扎哈', age=25}=廣州,
// Student{name='鳳姐', age=30}=你家隔壁
// }
//打印
System.out.println(map);
}
}
7.Map集合練習*****************
需求:
輸入一個字符串,統計其中每個字符出現次數。
public class TestMapDemo02 {
public static void main(String[] args) {
//需求:
//輸入一個字符串,統計其中每個字符出現次數。
//1.輸入
System.out.println("請輸入一個字符串:");
String str = new Scanner(System.in).nextLine();
//2.統計str中出現的每種字符次數
//a.定義保存結果的map集合
LinkedHashMap<Character, Integer> result =
new LinkedHashMap<Character, Integer>();
//b.遍歷字符串
for (int i = 0; i < str.length(); i++) {
//取出字符串中的字符
char ch = str.charAt(i);
if (result.containsKey(ch)) {
//如果ch這個字符,在map中有了,說明不是第一次
Integer count = result.get(ch);
result.put(ch, count + 1);
} else {
//如果ch這個字符,在map中沒有對應的鍵值對,說明第一次出現
result.put(ch, 1);
}
}
//c.打印結果
System.out.println(result);
}
}
三.補充知識點
1.可變參數(回顧)
格式:
public void 方法名(數據類型... 變量名){
}
本質:
可變參數其實就是一個數組
注意:
a.一個方法最最最多隻能有一個可變參數
b.如果有正常參數和可變參數,那麼可變參數必須寫在最後面
應用:
public class VariableArgumentsDemo {
public static void main(String[] args) {
//1.創建集合,添加多個元素
ArrayList<String> arr = new ArrayList<String>();
//添加
// arr.add("jack");
// arr.add("rose");
// arr.add("tom");
// arr.add("marry");
//工具類 Collections
Collections.addAll(arr,"jack","rose","tom","marry");
System.out.println(arr);
}
}
2.數組冒泡排序
一共需要n-1輪的比較
第一輪 需要比較 n-1 次
第二輪 需要比較 n-1-1次
第三輪 需要比較 n-1-2次
...
public class BubbleSortDemo {
public static void main(String[] args) {
//數組
int[] arr = {7, 6, 5, 4, 3};
//冒泡排序
for(int i = 0; i < arr.length-1;i++){
for(int j = 0;j < arr.length-1-i;j++){
//比較 arr[j] arr[j+1]
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
//打印
for (int i : arr) {
System.out.println(i);
}
}
}
冒泡
選擇
插入
希爾
快速
歸併
堆排序
3.Arrays類介紹********************
Arrays數組的工具類
Collections集合的工具類
Arrays工具類中的靜態方法
public static String toString(int[] arr);把數組的元素拼接成指定格式的字符串返回
public static void sort(int[] arr);升序排序數組
public static void sort(Integer[] arr, Compare<T> com);
比較器排序(但是數組必須是引用類型數組,如果是基本類型,請寫成包裝類)
public class ArraysDemo01 {
public static void main(String[] args) {
//1.定義數組
Integer[] arr = {7,8,2,6,1,5,4,9,3};
//2.打印
String str = Arrays.toString(arr);
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(str);
//3.排序數組(升序)
Arrays.sort(arr);
//4.打印
System.out.println(Arrays.toString(arr));
//5.使用比較器排序
Arrays.sort(arr,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
//6.打印
System.out.println(Arrays.toString(arr));
}
}
四.模擬排序鬥地主案例*************************
1.鬥地主規則介紹
2.鬥地主步驟分析
3.代碼實現(代碼演示)
public class SortDouDiZhuDemo {
public static void main(String[] args) {
// 排序鬥地主代碼步驟:
// 1.準備Map集合(鍵是編號,值是撲克牌)
LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
//花色
String[] colors = {"♠", "♥", "♣", "♦"};
//數值
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//編號變量
int id = 1;
//嵌套循環
for (String num : nums) {
for (String color : colors) {
//拼接
String card = color + num;
//添加到Map集合
map.put(id++, card);
}
}
//大小王單獨添加
map.put(53, "小S");
map.put(54, "大S");
// 2.準備一副牌(54個編號)
ArrayList<Integer> cardBox = new ArrayList<Integer>();
for (int i = 1; i < 55; i++) {
cardBox.add(i);
}
// 3.洗牌(打亂編號)
Collections.shuffle(cardBox);
// 4.發牌(發的編號)
ArrayList<Integer> p1 = new ArrayList<Integer>();
ArrayList<Integer> p2 = new ArrayList<Integer>();
ArrayList<Integer> p3 = new ArrayList<Integer>();
ArrayList<Integer> dp = new ArrayList<Integer>();
//發牌
for (int i = 0; i < cardBox.size() - 3; i++) {
//取出牌
Integer card = cardBox.get(i);
//發給誰??
if (i % 3 == 0) {
p1.add(card);
} else if (i % 3 == 1) {
p2.add(card);
} else {
p3.add(card);
}
}
//最後剩下三張給底牌
dp.add(cardBox.get(51));
dp.add(cardBox.get(52));
dp.add(cardBox.get(53));
// 5.排序(編號排序,也是對牌排序)
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
Collections.sort(dp);
// 6.根據編號從Map中獲取真正的牌,打印出來
lookCards(p1, map);
lookCards(p2, map);
lookCards(p3, map);
lookCards(dp, map);
}
public static void lookCards(ArrayList<Integer> pp, LinkedHashMap<Integer, String> map) {
//把集合中編號,通過map的get方法換成牌
for (Integer id : pp) {
String card = map.get(id);
System.out.print(card + " ");
}
System.out.println();
}
}
總結:
泛型只能用引用類型
Comparator接口使用:
Integer[] arr ={};
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2; // 升序 前減後
}
});
-[] 能夠說出Map集合特點
a.雙列集合
b.鍵唯一(如果鍵是自定義類型,必須重寫hashCode和equals)
-[] 使用Map集合添加方法保存數據**********************
增:put(鍵, 值);
刪:remove(鍵);
改:put(重複的鍵,值);
查:get(鍵);
其他:
public boolean containsKey(鍵);
public boolean containsValue(值);
-[] 使用“鍵找值”的方式遍歷Map集合**************
Set<鍵的泛型> keys = map.keySet();
-[] 使用“鍵值對”的方式遍歷Map集合**************
Set<Map.Entry<鍵的泛型,值的泛型>> entries = map.entrySet();
-[] 能夠使用HashMap/LinkedHashMap存儲自定義鍵值對的數據
-[] 能夠使用可變參數
Collections.add方法
框架,反射,服務器源碼
-[] 能夠理解冒泡排序/選擇排序/插入排序的原理
-[] 能夠使用Arrays數組工具類的常用方法
Arrays.toString(數組);把數組元素拼接成字符串
Arrays.sort(數組);升序排列字符串
Arrays.sort(數組,new Comparator(){});
-[] 能夠使用HashMap編寫排序鬥地主洗牌發牌案例