天天看點

Java基礎之雙列集合MapDay18

Day18

18.01_集合架構(Map集合概述和特點)

A:Map接口概述           

檢視API可以知道:

将鍵映射到值的對象

一個映射不能包含重複的鍵

每個鍵最多隻能映射到一個值

B:Map接口和Collection接口的不同

Map是雙列的,Collection是單列的

Map的鍵唯一,Collection的子體系Set是唯一的

Map集合的資料結構值針對鍵有效,跟值無關;Collection集合的資料結構是針對元素有效

C:hashSet底層是雙列的,隐藏掉了值就是一個Object,依靠于hashMap,隐藏掉一個是可以的,但是憑空創造就比較難了,hashMap和

hashSet底層其實都是hash算法,搞了一套算方法.

18.02_集合架構(Map集合的功能概述)

重點:熟練.

A:Map集合的功能概述

a:添加功能

V      put(K key,V value):添加元素。

如果鍵是第一次存儲,就直接存儲元素,傳回null

如果鍵不是第一次存在,就用值把以前的值替換掉,傳回以前的值

b:删除功能

void    clear():移除所有的鍵值對元素

V      remove(Object key):根據鍵删除鍵值對元素,并把值傳回,鍵和值都取消了

c:判斷功能

boolean   containsKey(Object key):判斷集合是否包含指定的鍵

boolean   containsValue(Object value):判斷集合是否包含指定的值

boolean   isEmpty():判斷集合是否為空

d:擷取功能

Set<Map.Entry<K,V>>   entrySet():拿到所有的鍵值集合

V                    get(Object key):根據鍵擷取值

Set<K>               keySet():擷取集合中所有鍵的集合

Collection<V>          values():擷取集合中所有值的集合

e:長度功能

int size():傳回集合中的鍵值對的個數

--------------------------------------------------------------------------------------------------------------------------

HashMap<String, Integer> map = new HashMap<String, Integer>();

map.put("張三", 33);

map.put("李四", 44);

map.put("王五", 55);

boolean a = map.containsKey("張三");

boolean b = map.containsValue(44);

boolean c = map.isEmpty();

System.out.println(a);

System.out.println(b);

System.out.println(c);

int x = map.size();

System.out.println(x);

int y = map.remove("張三");

System.out.println(y);

System.out.println(map);

map.clear();

System.out.println(map.size());

---------------------------------------------------------------------------------------------------------------------------

18.03 Map集合的周遊之鍵找值

重點:掌握第一種周遊方式.

A:鍵找值思路:

擷取所有鍵的集合

周遊鍵的集合,擷取到每一個鍵

根據鍵找值

B:案例示範

Map集合的周遊之鍵找值

---------------------------------------------------------------------------------------------------------------------------

HashMap<String, Integer> map = new HashMap<String, Integer>();

map.put("張三", 33);

map.put("李四", 44);

map.put("王五", 55);

int a = map.get("張三");

System.out.println(a);

for(String key : map.keySet()) {

Integer value = map.get(key);

System.out.println(key + "..." + value);

}

Set<String> keys = map.keySet(); //與單列集合不同,應該是先獲得key的集合

Iterator<String> it = keys.iterator();

while (it.hasNext()) {

String key = it.next(); //兩次用鍵,指針會後移是以就是固定起來

Integer value = map.get(key);

System.out.println(key + "..." + value);

}

---------------------------------------------------------------------------------------------------------------------------

18.04 Map集合的周遊之鍵值對對象找鍵和值

重點:第二種周遊方式.

A:鍵值對對象找鍵和值思路:

擷取所有鍵值對對象的集合

周遊鍵值對對象的集合,擷取到每一個鍵值對對象

根據鍵值對對象找鍵和值

B:案例示範

Map集合的周遊之鍵值對對象找鍵和值

---------------------------------------------------------------------------------------------------------------------------

HashMap<String, Integer> hm = new HashMap<>();

hm.put("張三", 23);

hm.put("李四", 24);

hm.put("王五", 25);

hm.put("趙六", 26);

for(Entry<String,Integer> en : hm.entrySet()) {

System.out.println(en.getKey() + "=" + en.getValue());

}

---------------------------------------------------------------------------------------------------------------------------

C:源碼分析,就是說entry是map下邊的内部類.  導包的話就是将外部類進行了打開裡邊可以直接的進行了建立對象.

interface Inter {

interface Inter2 {

public void show();

}

}

class Demo implements Inter.Inter2 {

@Override

public void show() {

}

}

18.05 HashMap集合鍵是Student值是String的案例

重點:引用類型.

A:案例示範

HashMap集合鍵是Student值是String的案例(因為鍵是hash算法,必須重寫自定義對象的hashCode和equts方法,不然的話

他們就隻不同的對象,也就是說不能進行覆寫的),這個才是運用最多的,但是一般情況下鍵卻是基本資料類型.

---------------------------------------------------------------------------------------------------------------------------

public static void main(String[] args) {

HashMap<Student, String> hm = new HashMap<>();

hm.put(new Student("張三", 23), "北京");

hm.put(new Student("張三", 23), "上海");

hm.put(new Student("李四", 24), "廣州");

hm.put(new Student("王五", 25), "深圳");

System.out.println(hm); //列印出了每一個對象和值,因為其父類重寫了toString方法.

}

---------------------------------------------------------------------------------------------------------------------------

public class Student {

private String name;

private int age;

public Student() {

super();

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [name=" + name + ", age=" + age + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

}

---------------------------------------------------------------------------------------------------------------------------

18.06_集合架構(LinkedHashMap的概述和使用)

A:案例示範

LinkedHashMap的特點

底層是連結清單實作的可以保證怎麼存就怎麼取

---------------------------------------------------------------------------------------------------------------------------

LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();

lhm.put("張三", 23);

lhm.put("李四", 24);

lhm.put("趙六", 26);

lhm.put("王五", 25);

System.out.println(lhm);

---------------------------------------------------------------------------------------------------------------------------

18.07 TreeMap集合鍵是Student值是String的案例

重點:為了保證鍵值是唯一的,當鍵值是自定義對象的時候應該進行重寫hashcode和equals方法.與hashMap是一樣的

A:案例示範

TreeMap集合鍵是Student值是String的案例,除了保證唯一性他還得重寫自定義類的comparable比較器接口,因為

人家是排序的集合,你不傳比較算法人家就不讓你存.

---------------------------------------------------------------------------------------------------------------------------

public static void main(String[] args) {

TreeMap<Student, String> map = new TreeMap<Student, String>(

new Comparator<Student>() {

@Override

public int compare(Student o1, Student o2) {

int num = o1.getName().compareTo(o2.getName());   //按照姓名進行比較

return num == 0 ? o1.getAge() - o2.getAge() : num;

}

});

map.put(new Student("張三", 33), "北京");

map.put(new Student("張三", 33), "北京");

map.put(new Student("李四", 44), "上海");

map.put(new Student("王五", 55), "西安");

System.out.println(map);

---------------------------------------------------------------------------------------------------------------------------

public class Student implements Comparable<Student> {

private String name;

private int age;

public Student() {

super();

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [name=" + name + ", age=" + age + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

@Override

public int compareTo(Student o) {

int num = this.age - o.age; //以年齡為主要條件

return num == 0 ? this.name.compareTo(o.name) : num;

}

}

---------------------------------------------------------------------------------------------------------------------------

案例--18.08_集合架構(統計字元串中每個字元出現的次數)

A:案例示範

需求:統計字元串中每個字元出現的次數

---------------------------------------------------------------------------------------------------------------------------

HashMap<Character, Integer> map = new HashMap<Character, Integer>();

String s = "aaaaaaaaaaaabbbbbbbbbbbbbcccccccccccc";

char[] chs = s.toCharArray();

for (char c : chs) {

map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1);

}

//System.out.println(map);

// 列印到的方式.

for (Entry<Character, Integer> c : map.entrySet()) {

System.out.println(c.getKey() + "..." + c.getValue());

}

for (char  c : map.keySet()) {

System.out.println(c + "..." + map.get(c));

}

---------------------------------------------------------------------------------------------------------------------------

重點:18.09_集合架構(集合嵌套之HashMap嵌套HashMap)

重點:注意取費分層取到鍵值和相應的鍵值的entry.KeySet.

A:案例示範

集合嵌套之HashMap嵌套HashMap

---------------------------------------------------------------------------------------------------------------------------

HashMap<Student,String> map = new HashMap<Student, String>();

map.put(new Student("張三",33), "北京");

map.put(new Student("張三",33), "北京");

map.put(new Student("李四",44), "上海");

map.put(new Student("王五",55), "西安");

HashMap<Student,String> map1 = new HashMap<Student, String>();

map1.put(new Student("張三1",331), "北京1");

map1.put(new Student("張三1",331), "北京1");

map1.put(new Student("李四1",441), "上海1");

map1.put(new Student("王五1",551), "西安1");

HashMap<HashMap<Student, String>, String> hm =

new HashMap<HashMap<Student,String>, String>();

hm.put(map, "88期學員");

hm.put(map1, "99期學員");

for (Entry<HashMap<Student, String>, String> en : hm.entrySet()) {

String value1 = en.getValue();

for (Entry<Student, String>  en1 : en.getKey().entrySet()) {

String value2 = en1.getValue();

System.out.println(en1.getKey() + "..." + value2 + "..." + value1);

}

}

---------------------------------------------------------------------------------------------------------------------------

18.10_集合架構(HashMap和Hashtable的差別)經常面試!

A:面試題

HashMap和Hashtable的差別

Hashtable是JDK1.0版本出現的,是線程安全的,效率低,HashMap是JDK1.2版本出現的,是線程不安全的,效率高

Hashtable不可以存儲null鍵和null值,HashMap可以存儲null鍵和null值

hashTable和vertor的命運一樣,都被替代了.

B:案例示範

HashMap和Hashtable的差別

18.11_集合架構(Collections工具類的概述和常見方法講解)

A:Collections類概述

針對集合操作 的工具類

B:Collections成員方法

public static <T> void sort(List<T> list)

public static <T> int binarySearch(List<?> list,T key)

public static <T> T max(Collection<?> coll)

public static void reverse(List<?> list)

public static void shuffle(List<?> list)

18.12_集合架構(模拟鬥地主洗牌和發牌)

A:案例示範

模拟鬥地主洗牌和發牌,牌沒有排序

//買一副撲克

String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

String[] color = {"方片","梅花","紅桃","黑桃"};

ArrayList<String> poker = new ArrayList<>();

for(String s1 : color) {

for(String s2 : num) {

poker.add(s1.concat(s2));

}

}

poker.add("小王");

poker.add("大王");

//洗牌

Collections.shuffle(poker);

//發牌

ArrayList<String> gaojin = new ArrayList<>();

ArrayList<String> longwu = new ArrayList<>();

ArrayList<String> me = new ArrayList<>();

ArrayList<String> dipai = new ArrayList<>();

for(int i = 0; i < poker.size(); i++) {

if(i >= poker.size() - 3) {

dipai.add(poker.get(i));

}else if(i % 3 == 0) {

gaojin.add(poker.get(i));

}else if(i % 3 == 1) {

longwu.add(poker.get(i));

}else {

me.add(poker.get(i));

}

}

//看牌

System.out.println(gaojin);

System.out.println(longwu);

System.out.println(me);

System.out.println(dipai);

18.13_集合架構(模拟鬥地主洗牌和發牌并對牌進行排序的原理圖解)

A:畫圖示範

畫圖說明排序原理

18.14_集合架構(模拟鬥地主洗牌和發牌并對牌進行排序的代碼實作)

A:案例示範

模拟鬥地主洗牌和發牌并對牌進行排序的代碼實作

//買一副牌

String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

String[] color = {"方片","梅花","紅桃","黑桃"};

HashMap<Integer, String> hm = new HashMap<>(); //存儲索引和撲克牌

ArrayList<Integer> list = new ArrayList<>(); //存儲索引

int index = 0; //索引的開始值

for(String s1 : num) {

for(String s2 : color) {

hm.put(index, s2.concat(s1)); //将索引和撲克牌添加到HashMap中

list.add(index); //将索引添加到ArrayList集合中

index++;

}

}

hm.put(index, "小王");

list.add(index);

index++;

hm.put(index, "大王");

list.add(index);

//洗牌

Collections.shuffle(list);

//發牌

TreeSet<Integer> gaojin = new TreeSet<>();

TreeSet<Integer> longwu = new TreeSet<>();

TreeSet<Integer> me = new TreeSet<>();

TreeSet<Integer> dipai = new TreeSet<>();

for(int i = 0; i < list.size(); i++) {

if(i >= list.size() - 3) {

dipai.add(list.get(i)); //将list集合中的索引添加到TreeSet集合中會自動排序

}else if(i % 3 == 0) {

gaojin.add(list.get(i));

}else if(i % 3 == 1) {

longwu.add(list.get(i));

}else {

me.add(list.get(i));

}

}

//看牌

lookPoker("高進", gaojin, hm);

lookPoker("龍五", longwu, hm);

lookPoker("馮佳", me, hm);

lookPoker("底牌", dipai, hm);

}

public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer, String> hm) {

System.out.print(name + "的牌是:");

for (Integer index : ts) {

System.out.print(hm.get(index) + " ");

}

System.out.println();

}

18.15_集合架構(泛型固定下邊界)

? super E

18.16_day18總結

把今天的知識點總結一遍。

下一篇: 雙列集合