2020.5.20 課堂筆記
Map接口
- 雙列集合的接口,存放一個鍵映射到值的對象,鍵不能重複,每個鍵可以最多映射到一個值。
- Map接口和Collection接口的不同
- Map接口:是雙列的,鍵映射值,鍵唯一。資料結構針對鍵有效。
- Collection接口:單列的,隻有Set接口的子集合元素唯一,資料結構針對元素有效。
- Map接口和其子集合

- HashMap:鍵的唯一是靠重寫hash方法和equals方法來保證。
- LinkedHashMap:底層資料結構是連結清單和哈希表,連結清單保證有序,哈希表保證鍵唯一。
- TreeMap:底層資料結構是二叉樹,保證了鍵值的排序和唯一
- HashMap和Hashtable的差別
- HashMap:允許null值和null鍵,線程不安全,效率高。
- Hashtable:不允許null值和null鍵,線程安全,效率底。
- 其他都是相同的
代碼
package org.westos.demo0519.HashMap;
/*Author:LH
CreatTime:2020.05.20.15:32*/
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
public class Test {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(13, "張三");
map.put(14,"李四");
map.put(15,"張三");
map.put(16,"李四");
map.put(18,"張三");
map.put(19,"李四");
// 添加元素,傳回此鍵映射的舊元素,如果有就傳回,沒有就傳回null.
String s = map.put(13, "張三2");
System.out.println(s); //此處傳回第一個鍵映射的“張三”。
map.put(14,"李四2");// 鍵相同,值覆寫。覆寫掉第一個鍵映射的值李四。
System.out.println(map);
String s1 = map.remove(14);
System.out.println(s1);
System.out.println(map);
// 删除的鍵不存在,就傳回null.
String s2 = map.remove(19);
// 将HashMap轉換成 Set集合。
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry);
}
// 将map集合的鍵存到set集合中。
Set<Integer> integers = map.keySet();
for (Integer integer : integers) {
// 周遊鍵,拿出映射的值
System.out.println(map.get(integer));
}
System.out.println("+++++++++++++");
// 判斷是否存在指定的鍵或者值
System.out.println(map.containsKey(17));
System.out.println(map.containsKey(18));;
System.out.println(map.containsValue("張三"));
Collection<String> values = map.values();
System.out.println("===========");
// 擷取值的集合,并周遊
values.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}
2.package org.westos.demo0519.HashMap;
/*Author:LH
CreatTime:2020.05.21.9:12*/
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
//如果Student不重寫hash方法和equals方法,就會将所有的資料存進去,因為每次每次new出來的
//對象位址都不相同,是以hash值也不相同。
//重寫hash方法和equals方法,就會把名字和年齡相同的去掉。
public class TestStudent {
public static void main(String[] args) {
HashMap<Student, String> hashMap = new HashMap<>();
hashMap.put(new Student("劉興",18),"s001");
hashMap.put(new Student("劉興",19),"s002");
hashMap.put(new Student("張三",18),"s001");
hashMap.put(new Student("趙四",20),"s003");
hashMap.put(new Student("劉興",19),"s002");
System.out.println(hashMap);
System.out.println("==========");
// 周遊方式一,擷取出鍵值對對象,周遊并擷取鍵和值
Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
for (Map.Entry<Student, String> node : entries) {
Student key = node.getKey();
String value = node.getValue();
System.out.println(key.toString()+"=="+value);
}
System.out.println("=========");
// 周遊方式二,擷取鍵的集合,通過周遊鍵擷取值
Set<Student> keySet = hashMap.keySet();
keySet.forEach(new Consumer<Student>() {
@Override
public void accept(Student student) {
String s = hashMap.get(student);
System.out.println(student.toString()+"=="+s);
}
});
System.out.println("==============");
// 周遊方式三,通過JDK1.8提供的方法foreach擷取鍵和值
hashMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.toString()+"=="+s);
}
});
}
}
package org.westos.demo0519.HashMap;
/*Author:LH
CreatTime:2020.05.18.15:16*/
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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 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);
}
}
3.package org.westos.demo0519.HashMap;
/*Author:LH
CreatTime:2020.05.20.16:42*/
//存取順序一緻,自定義對象也要重寫hash方法和equals方法
import java.util.LinkedHashMap;
public class LinkeHashMap {
public static void main(String[] args) {
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>(17, 0.8f, false);
map.put(12,12);
map.put(14,15);
map.put(15,13);
map.put(12,17);
map.put(8,15);
map.put(1,13);
Integer remove = map.remove(15);
System.out.println(map);
}
}
4.package org.westos.demo0519.TreeMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*Author:LH
CreatTime:2020.05.21.10:40*/
//hashmap集合嵌套,并按照此方式列印出來。
/*基礎班
張三 20
李四 22
就業班
王五 21
趙六 23*/
public class Test2 {
public static void main(String[] args) {
// 先将基本資料存在小的雙列集合中
HashMap<String, Integer> minhashMap1 = new HashMap<>();
minhashMap1.put("張三",20);
minhashMap1.put("李四",22);
HashMap<String, Integer> minhashMap2 = new HashMap<>();
minhashMap2.put("王五",21);
minhashMap2.put("趙六",23);
// 再将班級資訊存到大的集合中
HashMap<String, HashMap<String, Integer>> hashMap = new HashMap<>();
hashMap.put("基礎班",minhashMap1);
hashMap.put("就業班",minhashMap2);
// 周遊
// 先擷取班級資訊
Set<Map.Entry<String, HashMap<String, Integer>>> entries = hashMap.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
HashMap<String, Integer> value = entry.getValue();
System.out.println(key);
// 通過擷取的班級資訊,在擷取班級中成員的資訊
Set<Map.Entry<String, Integer>> entries1 = value.entrySet();
for (Map.Entry<String, Integer> entry1 : entries1) {
String key1 = entry1.getKey();
Integer value1 = entry1.getValue();
System.out.println('\t'+key1+"=="+value1);
}
System.out.println();
}
}
}
5.package org.westos.demo0519.TreeMap;
/*Author:LH
CreatTime:2020.05.21.10:16*/
import java.util.TreeMap;
import java.util.function.BiConsumer;
//使用自然排序鍵存儲自定義對象,對象要實作 Comparable<Student>接口,重寫 compareTo方法
//才能保證唯一和排序。
public class TestStudent {
public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("張三",18),"s001");
treeMap.put(new Student("張三",19),"s002");
treeMap.put(new Student("趙四",18),"s003");
treeMap.put(new Student("王五",20),"s004");
treeMap.put(new Student("張三",19),"s005");
treeMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.toString()+"==="+s);
}
});
}
}
package org.westos.demo0519.TreeMap;
/*Author:LH
CreatTime:2020.05.18.15:16*/
import java.util.Objects;
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
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 compareTo(Student o) {
int num =this.age-o.age;
int num2=num==0?this.name.compareTo(o.name):num;
return num2;
}
}
6. package org.westos.demo0519.TreeMap;
/*Author:LH
CreatTime:2020.05.21.10:28*/
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
//使用比較器排序
public class TestStudent2 {
public static void main(String[] args) {
TreeMap<Student2, String> treeMap = new TreeMap<>(new Comparator<Student2>() {
@Override
public int compare(Student2 o1, Student2 o2) {
// 此處的邏輯根據需要重寫
int i = o1.getAge() - o2.getAge();
int i2=i==0?o1.getName().compareTo(o2.getName()):i;
return i2;
}
});
treeMap.put(new Student2("張三",18),"s001");
treeMap.put(new Student2("張三",19),"s002");
treeMap.put(new Student2("趙四",18),"s003");
treeMap.put(new Student2("王五",20),"s004");
treeMap.put(new Student2("張三",19),"s005");
Set<Map.Entry<Student2, String>> entries = treeMap.entrySet();
for (Map.Entry<Student2, String> entry : entries) {
Student2 key = entry.getKey();
String value = entry.getValue();
System.out.println(key.toString()+"=="+value);
}
}
}
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):随機打亂集合。
鬥地主發牌
package org.westos.demo0519.playgame;
/*Author:LH
CreatTime:2020.05.19.22:49*/
//鬥地主發牌遊戲
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeMap;
import java.util.function.BiConsumer;
public class Game {
public static void main(String[] args) {
// 先将牌組拼好放在集合中
ArrayList<String> list = new ArrayList<>();
String []shape={"♠","♥","♦","♣"};
String [] num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (String s1 : num) {
for (String s2 : shape) {
String s = s1.concat(s2);
list.add(s);
}
}
list.add("☀");
list.add("☼");
// 将單列集合轉換成雙列集合,存放map集合
TreeMap<Integer, String> map = new TreeMap<>();
for (int i = 0; i < list.size(); i++) {
map.put(i,list.get(i));
}
// 取出雙列集合的鍵存放在新list集合,便于發牌和洗牌,隻針對鍵進行,再通過鍵找到value。
ArrayList<Integer> list1 = new ArrayList<>();
map.forEach(new BiConsumer<Integer, String>() {
@Override
public void accept(Integer key, String value) {
list1.add(key);
}
});
Collections.shuffle(list1);
ArrayList<Integer> person1 = new ArrayList<>();
ArrayList<Integer> person2 = new ArrayList<>();
ArrayList<Integer> person3 = new ArrayList<>();
ArrayList<Integer> publicCard = new ArrayList<>();
for (int i = 0; i < list1.size(); ) {
if (i<list1.size()-3){
person1.add(list1.get(i));
person2.add(list1.get(i+1));
person3.add(list1.get(i+2));
i+=3;
}else {
publicCard.add(list1.get(i));
i++;
}
}
Collections.sort(person1);
Collections.sort(person2);
Collections.sort(person3);
for (Integer integer : person1) {
System.out.print(map.get(integer)+'\t');
}
System.out.println();
for (Integer integer : person2) {
System.out.print(map.get(integer)+'\t');
}
System.out.println();
for (Integer integer : person3) {
System.out.print(map.get(integer)+'\t');
}
System.out.println();
for (Integer integer : publicCard) {
System.out.print(map.get(integer)+'\t');
}
}
}
查找重複元素
package org.westos.demo0519.playgame;
/*Author:LH
CreatTime:2020.05.21.10:56*/
import java.util.HashMap;
/*需求:統計字元串中每個字元出現的次數
"aababcabcdabcde",擷取字元串中每一個字母出現的次數要求結果:a(5)b(4)c(3)d(2)e(1)*/
public class Test {
public static void main(String[] args) {
String s="aababcabcdabcde";
// 建立雙列集合,把已知的元素當作鍵和值
HashMap<Character,Character> hashMap = new HashMap<>();
hashMap.put('a','a');
hashMap.put('b','b');
hashMap.put('c','c');
hashMap.put('d','d');
hashMap.put('e','e');
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
for (int i = 0; i < s.length(); i++) {
// 向已知的集合中存字元串,有相同鍵,值就會值覆寫,傳回舊值,然後做累加就知道數量
Character put = hashMap.put(s.charAt(i), s.charAt(i));
if (put.equals('a')){
a++;
}else if (put.equals('b')){
b++;
}else if (put.equals('c')){
c++;
}else if (put.equals('d')){
d++;
}else{
e++;
}
}
System.out.println("a("+a+")");
System.out.println("b("+b+")");
System.out.println("c("+c+")");
System.out.println("d("+d+")");
System.out.println("e("+e+")");
}
}
(put.equals('a')){
a++;
}else if (put.equals('b')){
b++;
}else if (put.equals('c')){
c++;
}else if (put.equals('d')){
d++;
}else{
e++;
}
}
System.out.println("a("+a+")");
System.out.println("b("+b+")");
System.out.println("c("+c+")");
System.out.println("d("+d+")");
System.out.println("e("+e+")");
}
}