JavaSE学习总结第18天_集合框架4

时间:2023-03-09 03:33:05
JavaSE学习总结第18天_集合框架4

 

18.01 Map集合概述和特点

Map接口概述:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值

Map接口和Collection接口的不同

1.Map是双列的,Collection是单列的

2.Map的键唯一,Collection的子体系Set是唯一的

3.Map集合的数据结构值针对键有效,跟值无关,Collection集合的数据结构是针对元素有效

18.02 Map集合的功能概述

成员方法:

1.V put(K key,V value):

将指定的值与此映射中的指定键关联(可选操作)。

2.V remove(Object key):

如果存在一个键的映射关系,则将其从此映射中移除(可选操作)。

3.void clear():

从此映射中移除所有映射关系(可选操作)。

4.boolean containsKey(Object key):

如果此映射包含指定键的映射关系,则返回 true。

5.boolean containsValue(Object value):

如果此映射将一个或多个键映射到指定值,则返回 true。

6.boolean isEmpty():

如果此映射未包含键-值映射关系,则返回 true。

7.int size():

返回此映射中的键-值映射关系数。

8.V get(Object key)

返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。

9.Set<K> keySet()

返回此映射中包含的键的 Set 视图。

10.Collection<V> values()

返回此映射中包含的值的 Collection 视图。

11.Set<Map.Entry<K,V>> entrySet()

返回此映射中包含的映射关系的 Set 视图。

18.03 Map集合的基本功能测试

map.put("001", "旺财");    :第一次存储直接存储元素,返回null

map.put("001", "小强");    :不是第一次存储,用新值将以前的值替换掉,返回以前的值

map.remove("001");     :根据键删除键值对元素,并返回键所对应的值,没有则返回空

18.04 Map集合的获取功能测试

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

 map.put("001", "小明");
map.put("002", "旺财");
map.put("003", "小强");
//根据键获取值
System.out.println(map.get("002"));//旺财,没有该键返回null
System.out.println("-----");
//获取集合中所有键的集合
Set<String> set = map.keySet();
for(String key : set)
{
System.out.println(key);
}
System.out.println("-----");
//获取集合中所有值的集合
Collection<String> coll = map.values();
for(String value : coll)
{
System.out.println(value);
}

18.05 Map集合的遍历之键找值

根据键找值:

获取所有键的集合,遍历键的集合,获取到每一个键,根据键找值

 Map<String, String> map = new HashMap<String, String>();
map.put("001", "小明");
map.put("002", "旺财");
map.put("003", "小强"); //获取所有的键
Set<String> set = map.keySet();
for(String key : set)
{
//根据键找值
String value = map.get(key);
System.out.println(key+":"+value);
}

18.06 Map集合的遍历之键值对对象找键和值

根据键值对对象找键和值:

获取所有键值对对象的集合,遍历键值对对象的集合,获取到每一个键值对对象,根据键值对对象找键和值

 Map<String, String> map = new HashMap<String, String>();
map.put("001", "小明");
map.put("002", "旺财");
map.put("003", "小强");
//获取所有键值对对象的集合
Set<Map.Entry<String, String>> set = map.entrySet();
//遍历键值对对象的集合,获取每一个键值对对象
for(Map.Entry<String, String> me : set)
{
//根据键值对对象获取键和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key+":"+value);
}

18.07 Map集合遍历的两种方式比较图解

JavaSE学习总结第18天_集合框架4

JavaSE学习总结第18天_集合框架4

18.08 HashMap集合键是Stirng值是String的案例

HashMap类概述:键是哈希表结构,可以保证键的唯一性

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

 map.put("001", "小明");
map.put("002", "旺财");
map.put("003", "小强");
map.put("004", "小红");
Set<String> set = map.keySet();
for(String key : set)
{
String value = map.get(key);
System.out.println(key+"--"+value);
}

18.09 HashMap集合键是Student值是String的案例

 // 创建集合对象
HashMap<Student, String> hm = new HashMap<Student, String>(); hm.put(new Student("小明",23), "001");
hm.put(new Student("小强",15), "002");
hm.put(new Student("旺财",13), "003");
hm.put(new Student("张三",17), "004");
hm.put(new Student("小强",15), "005"); // 遍历
Set<Student> set = hm.keySet();
for (Student key : set)
{
String value = hm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---"
+ value);
}

当键是自定义对象时,需重写hashCode()和equals()方法

18.10 LinkedHashMap的概述和使用

Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。

由哈希表保证键的唯一性

由链表保证键的有序

 LinkedHashMap<String, String> hm = new LinkedHashMap<String,String>();

 // 创建并添加元素
hm.put("2345", "hello");
hm.put("1234", "world");
hm.put("3456", "java");
hm.put("1234", "javaee");
hm.put("3456", "android"); // 遍历
Set<String> set = hm.keySet();
for (String key : set)
{
String value = hm.get(key);
System.out.println(key + "---" + value);
}

运行结果:

2345---hello
1234---javaee
3456---android

18.11 TreeMap集合键是String值是String的案例

TreeMap类概述:键是红黑树结构,可以保证键的排序和唯一性

 // 创建集合对象
TreeMap<String, String> tm = new TreeMap<String, String>(); // 创建元素并添加元素
tm.put("hello", "你好");
tm.put("world", "世界");
tm.put("java", "爪哇");
tm.put("world", "世界2");
tm.put("javaee", "爪哇EE"); // 遍历集合
Set<String> set = tm.keySet();
for (String key : set)
{
String value = tm.get(key);
System.out.println(key + "---" + value);
}

运行结果:

hello---你好
java---爪哇
javaee---爪哇EE
world---世界2

18.12 TreeMap集合键是Student值是String的案例

 public class Practice
{
public static void main(String[] args)
{
// 创建集合对象
TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>()
{
@Override
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;
}
}); tm.put(new Student("小明",23), "001");
tm.put(new Student("小强",15), "002");
tm.put(new Student("旺财",13), "003");
tm.put(new Student("张三",17), "004");
tm.put(new Student("小强",15), "005"); Set<Map.Entry<Student, String>> set = tm.entrySet();
for(Map.Entry<Student, String> me : set)
{
Student key = me.getKey();
String value = me.getValue();
System.out.println(key.getName()+":"+key.getAge()+":"+value);
}
}
}

运行结果:

旺财:13:003
小强:15:005
张三:17:004
小明:23:001

18.13 统计字符串中每个字符出现的次数案例图解

"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)

JavaSE学习总结第18天_集合框架4

18.14 统计字符串中每个字符出现的次数案例代码实现

 public class Practice
{
public static void main(String[] args)
{
// 定义一个字符串(改进为键盘录入)
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
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); //是null:说明该键不存在,就把该字符作为键,1作为值存储
if(i == null)
{
tm.put(ch, 1);
}
else
{
//不是null:说明该键存在,就把值加1,然后重写存储该键和值
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);
}
}

运行结果:

请输入一个字符串:
aasdgrfedcsdf
结果:a(2)c(1)d(3)e(1)f(2)g(1)r(1)s(2)

18.15 HashMap集合嵌套HashMap集合的案例

 public class Practice
{
public static void main(String[] args)
{
// 大集合对象
HashMap<String, HashMap<String, Integer>> bigMap = new HashMap<String, HashMap<String, Integer>>(); // 集合对象1
HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
// 添加元素
hm1.put("小明", 20);
hm1.put("小强", 22);
// 把集合添加到大集合
bigMap.put("集合1", hm1); // 集合对象2
HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
// 添加元素
hm2.put("旺财", 5);
hm2.put("张三", 23);
// 把集合添加到大集合
bigMap.put("集合2", hm2); //遍历集合
Set<String> bigMapset = bigMap.keySet();
for(String bigMapKey : bigMapset)
{
System.out.println(bigMapKey);
HashMap<String, Integer> value = bigMap.get(bigMapKey);
Set<String> set = value.keySet();
for(String s : set)
{
Integer i = value.get(s);
System.out.println("\t"+s+":"+i);
}
}
}
}

运行结果:

集合1
小强:22
小明:20
集合2
旺财:5
张三:23

18.16 集合多层嵌套的代码体现

 public class Practice
{
public static void main(String[] args)
{
// 创建大集合
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("赵雅芝", 28);
Student s4 = new Student("武鑫", 29);
array2.add(s3);
array2.add(s4);
bjCzbkMap.put("基础班", array1);
bjCzbkMap.put("就业班", array2);
czbkMap.put("北京校区", bjCzbkMap); // 西安校区数据
HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array3 = new ArrayList<Student>();
Student s5 = new Student("范冰冰", 27);
Student s6 = new Student("刘意", 30);
array3.add(s5);
array3.add(s6);
ArrayList<Student> array4 = new ArrayList<Student>();
Student s7 = new Student("李冰冰", 28);
Student s8 = new Student("张志豪", 29);
array4.add(s7);
array4.add(s8);
xaCzbkMap.put("基础班", array3);
xaCzbkMap.put("就业班", array4);
czbkMap.put("西安校区", xaCzbkMap); // 遍历集合
Set<String> czbkMapSet = czbkMap.keySet();
for (String czbkMapKey : czbkMapSet)
{
System.out.println(czbkMapKey);
HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(czbkMapKey);
Set<String> czbkMapValueSet = czbkMapValue.keySet();
for (String czbkMapValueKey : czbkMapValueSet)
{
System.out.println("\t" + czbkMapValueKey);
ArrayList<Student> czbkMapValueValue = czbkMapValue.get(czbkMapValueKey);
for (Student s : czbkMapValueValue)
{
System.out.println("\t\t" + s.getName() + "---"+ s.getAge());
}
}
}
}
}

运行结果:

北京校区
就业班
赵雅芝---28
武鑫---29
基础班
林青霞---27
风清扬---30
西安校区
就业班
李冰冰---28
张志豪---29
基础班
范冰冰---27
刘意---30

18.17 HashMap和Hashtable的区别

1:Hashtable和HashMap的区别

Hashtable:线程安全,效率低。不允许null键和null值

HashMap:线程不安全,效率高。允许null键和null值

2:List,Set,Map等接口是否都继承子Map接口?

List,Set不是继承自Map接口,它们继承自Collection接口

Map接口本身就是一个顶层接口

18.18 Collections工具类的概述

Collections类概述:针对集合操作的工具类,都是静态方法

Collection和Collections的区别:

Collection:是单列集合的顶层接口,有子接口List和Set。

Collections:是针对集合操作的工具类,有对集合进行排序和二分查找的方法

18.19 Collections工具类的常见方法讲解

Collections成员方法

1.public static <T extends Comparable<? super T>> void sort(List<T> list):

根据元素的自然顺序对指定列表按升序进行排序。

2.public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key):

使用二分搜索法搜索指定列表,以获得指定对象。

3.public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll):

根据元素的自然顺序,返回给定 collection 的最大元素。

4.public static void reverse(List<?> list):

反转指定列表中元素的顺序。

5.public static void shuffle(List<?> list):

使用默认随机源对指定列表进行置换。所有置换发生的可能性都是大致相等的。

例:

 public class Practice
{
public static void main(String[] args)
{
// 创建集合对象
List<Integer> list = new ArrayList<Integer>(); // 添加元素
list.add(30);
list.add(20);
list.add(50);
list.add(10);
list.add(40); System.out.println("原始:" + list);
//排序 默认情况下是自然顺序
Collections.sort(list);
System.out.println("排序:" + list);
// 二分查找
System.out.println("查找:"+Collections.binarySearch(list, 30));
// 最大值
System.out.println("最大值:"+Collections.max(list));
// 反转
Collections.reverse(list);
System.out.println("反转:" + list);
//随机置换
Collections.shuffle(list);
System.out.println("随机:" + list);
}
}

运行结果:

原始:[30, 20, 50, 10, 40]
排序:[10, 20, 30, 40, 50]
查找:2
最大值:50
反转:[50, 40, 30, 20, 10]
随机:[20, 10, 30, 50, 40]

18.20 ArrayList存储自定义对象并排序案例

 public class Practice
{
public static void main(String[] args)
{
// 创建集合对象
List<Student> list = new ArrayList<Student>(); // 创建学生对象
Student s1 = new Student("小明", 27);
Student s2 = new Student("旺财", 30);
Student s3 = new Student("小强", 28);
Student s4 = new Student("张三", 29); // 添加元素对象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4); // 排序
// 自然排序必须实现 Comparable 接口
//Collections.sort(list);
// 比较器排序
// 如果同时有自然排序和比较器排序,以比较器排序为主
Collections.sort(list, new Comparator<Student>()
{
@Override
public int compare(Student s1, Student s2)
{
int num = s2.getAge() - s1.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;
return num2;
}
});
// 遍历集合
for (Student s : list)
{
System.out.println(s.getName() + "---" + s.getAge());
}
}
}

运行结果:

旺财---30
张三---29
小强---28
小明---27

18.21 模拟斗地主洗牌和发牌

模拟斗地主洗牌和发牌

分析:

A:创建一个牌盒、B:装牌、C:洗牌、D:发牌、E:看牌

 public class Practice
{
public static void main(String[] args)
{
// 创建一个牌盒
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); // System.out.println("array:" + array); // 发牌
ArrayList<String> player1 = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> 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)
{
player1.add(array.get(x));
}
else if (x % 3 == 1)
{
player2.add(array.get(x));
}
else if (x % 3 == 2)
{
player3.add(array.get(x));
}
} // 看牌
lookPoker("玩家1", player1);
lookPoker("玩家2", player2);
lookPoker("玩家3", player3); lookPoker("底牌", diPai);
} public static void lookPoker(String name, ArrayList<String> array)
{
System.out.print(name + "的牌是:");
for (String s : array)
{
System.out.print(s + " ");
}
System.out.println();
}
}

运行结果:

玩家1的牌是:♦10 ♥4 ♠K ♠5 ♦6 ♥K ♦8 ♥2 ♥3 ♣8 ♣3 ♣4 ♦7 ♠Q ♣10 ♠3 ♠6
玩家2的牌是:♦9 ♦5 ♦A ♣5 ♦3 ♥8 ♣2 ♥J ♦J ♥5 ♦4 ♠9 ♠10 小王♣A ♥Q ♥7
玩家3的牌是:♠A ♣6 ♦2 ♦K ♣9 ♣K ♣7 ♦Q ♠4 ♥9 ♠7 大王♣Q ♥10 ♠8 ♣J ♠2
底牌的牌是:♥A ♥6 ♠J

18.22 模拟斗地主洗牌和发牌并对牌进行排序的代码实现

思路:

A:创建一个HashMap集合

B:创建一个ArrayList集合

C:创建花色数组和点数数组

D:从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。

E:洗牌(洗的是编号)

F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)

G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)

 public class Practice
{
public static void main(String[] args)
{
// 创建一个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> player1 = new TreeSet<Integer>();
TreeSet<Integer> player2 = new TreeSet<Integer>();
TreeSet<Integer> player3 = 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)
{
player1.add(array.get(x));
} else if (x % 3 == 1)
{
player2.add(array.get(x));
} else if (x % 3 == 2)
{
player3.add(array.get(x));
}
} // 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("玩家1", player1, hm);
lookPoker("玩家2", player2, hm);
lookPoker("玩家3", player3, 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();
}
}

运行结果:

玩家1的牌是:♠3 ♦3 ♥4 ♣4 ♥5 ♣5 ♠6 ♣8 ♥9 ♦10 ♥J ♣J ♦Q ♣A ♠2 ♣2 大王
玩家2的牌是:♠5 ♦5 ♣7 ♦7 ♠9 ♥10 ♦J ♠Q ♣Q ♠K ♣K ♠A ♥A ♦A ♥2 ♦2 小王
玩家3的牌是:♥3 ♣3 ♠4 ♦4 ♥6 ♣6 ♠7 ♥7 ♥8 ♦8 ♣9 ♠10 ♣10 ♠J ♥Q ♥K ♦K
底牌的牌是:♦6 ♠8 ♦9