18-java学习-Map集合、HashMap和Hashtable的区别、Collections(集合工具类)、集合练习、模拟斗地主(洗牌,发牌,看牌)&案例代码
目录:
Map集合
HashMap和Hashtable的区别
Collections(集合工具类)
集合练习
模拟斗地主(洗牌,发牌,看牌)
1.集合框架(Map集合概述和特点)
B:Map接口概述
查看API可以知道:
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值
C:Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
2.集合框架(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(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
e:长度功能
int size():返回集合中的键值对的对数
3.集合框架(Map集合的基本功能测试)
Map集合的基本功能
V put(K key,V value)//键相同 值覆盖
V remove(Object key)
void clear()
boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
int size()
5.集合框架(Map集合的遍历之键找值)
A:键找值思路:
获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值
B:案例演示
Map集合的遍历之键找值
public class MyTest4 {
public static void main(String[] args) {
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "刘亦菲");
hashMap.put(2, "王菲");
hashMap.put(3, "张柏芝");
hashMap.put(4, "张曼玉");
//遍历双列集合
//方式1:根据 键找值
//获取所有的键的集合
Set<Integer> integers = hashMap.keySet();
// System.out.println(integers);
//遍历键的集合来根据键找值
for (Integer key : integers) {
String value = hashMap.get(key);
System.out.println(key+"==="+value);
}
System.out.println("================================");
//遍历方式2:把真个键值对 当作一个整体获取出来。 这个整体的键值对象 是一个对象 Entry<Integer, String>
Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"==="+value);
}
}
}
6.集合框架(Map集合的遍历之键值对对象找键和值)
A:键值对对象找键和值思路:
获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值
B:案例演示
Map集合的遍历之键值对对象找键和值
7.集合框架(HashMap集合键是String值是Student的案例)
//案例演示: HashMap集合键是String值是Student的案例
public class MyTest {
public static void main(String[] args) {
//存储这个键是String类型 ,值是 Student类型的
HashMap<String, Student> hashMap = new HashMap<>();
hashMap.put("s001",new Student("张三1",231));
hashMap.put("s001", new Student("张三11111", 2311111));
hashMap.put("s002", new Student("张三2", 232));
hashMap.put("s003", new Student("张三3", 233));
hashMap.put("s004", new Student("张三4", 234));
hashMap.put("s005", new Student("张三5", 235));
hashMap.put("s006", new Student("张三6", 236));
hashMap.put("s007", new Student("张三7", 237));
//遍历集合中的元素,并打印学生的信息
//方式1
Set<String> strings = hashMap.keySet();
for (String key : strings) {
Student student = hashMap.get(key);
System.out.println(key+"=="+student.getName()+"---"+student.getAge());
}
System.out.println("======================");
//遍历方式2
Set<Map.Entry<String, Student>> entries = hashMap.entrySet();
for (Map.Entry<String, Student> entry : entries) {
String key = entry.getKey();
Student student = entry.getValue();
System.out.println(key + "==" + student.getName() + "---" + student.getAge());
}
}
}
8.集合框架(HashMap集合键是Student值是String的案例)
键唯一 注意重写hashCode方法 和 equals 方法
//HashMap集合键是Student值是String的案例
public class MyTest2 {
public static void main(String[] args) {
//存储键是Student 值是 String类型
//所有的双列集合的数据结构只跟键有关,跟值没关系。
//HashMap 你的键的数据结构是哈希表,哈希表唯一,且无序。要保证键的唯一性,要靠键重写hashCode和equals方法才能保证
HashMap<Student, String> hashMap = new HashMap<>();
hashMap.put(new Student("张三1",231),"s001");
hashMap.put(new Student("张三1", 231), "s00120");
hashMap.put(new Student("张三1", 231), "s00120202");
hashMap.put(new Student("张三1", 231), "s001302020");
hashMap.put(new Student("张三1", 231), "s001");
hashMap.put(new Student("张三1", 231), "s001");
hashMap.put(new Student("张三2", 232), "s002");
hashMap.put(new Student("张三3", 233), "s003");
hashMap.put(new Student("张三4", 234), "s004");
hashMap.put(new Student("张三5", 235), "s005");
hashMap.put(new Student("张三6", 236), "s006");
hashMap.put(new Student("张三7", 237), "s007");
//遍历
Set<Student> students = hashMap.keySet();
for (Student student : students) {
String s = hashMap.get(student);
System.out.println(student.getName()+"="+student.getAge()+"=="+s);
}
System.out.println("=====================");
Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName()+":"+key.getAge()+"==="+value);
}
}
}
9.集合框架(LinkedHashMap的概述和使用)
A:LinkedHashMap的概述: Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
B:LinkedHashMap的特点: 底层的数据结构是链表和哈希表 元素有序 并且唯一
元素的有序性由链表数据结构保证 唯一性由 哈希表数据结构保证
Map集合的数据结构只和键有关
C:案例演示: LinkedHashMap的特点
public class MyTest {
public static void main(String[] args) {
//LinkedHashMap 键的数据结构是链表和哈希表,键的特点 有序且唯一 哈希表保证唯一,链表保证有序
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("s001","张三");
linkedHashMap.put("s001", "张三2");
linkedHashMap.put("s002", "张三3");
linkedHashMap.put("s003", "张三4");
linkedHashMap.put("s004", "张三5");
linkedHashMap.put("s005", "张三6");
Set<String> strings = linkedHashMap.keySet();
for (String string : strings) {
String s = linkedHashMap.get(string);
System.out.println(string+"==="+s);
}
}
}
10.集合框架(TreeMap集合键是String值是String的案例)
TreeMap 键不允许插入null
A: TreeMap: 键的数据结构是红黑树,可保证键的排序和唯一性
排序分为自然排序和比较器排序
线程是不安全的效率比较高
B:案例演示: TreeMap集合键是Integer值是String的案例
public class MyTest2 {
public static void main(String[] args) {
// TreeMap 键的数据结构是二叉树,能够对键进行排序
//排序:自然排序,使用空参构造,对键有要求,要求键必须实现Comparable
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(10,"aaa");
treeMap.put(90, "aaa");
treeMap.put(11, "aaa");
treeMap.put(20,"bbbb");
System.out.println(treeMap);
}
}
11.集合框架(TreeMap集合键是Student值是String的案例)
//案例演示: TreeMap集合键是Student值是String的案例,按照年龄大小进行排序 注意键要实现Comparable 接口
public class MyTest {
public static void main(String[] args) {
//用TreeMap来存 键是Student类型,值时String类型
//双列集合的数据结构,只跟键有关,跟值没关系。
//对键进行自然排序,对键有要求,要求键实现Comparable接口 重写compareTo方法,根据此方法返回值的正负0来决定元素的排列顺序。 String Integer 默认实现了 Comparable接口
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("张三1",231),"s001");
treeMap.put(new Student("张三2", 239), "s002");
treeMap.put(new Student("张三3", 234), "s003");
treeMap.put(new Student("张三4", 230), "s004");
treeMap.put(new Student("张三5", 23), "s005");
treeMap.put(new Student("张三6", 213), "s006");
//System.out.println(treeMap);
Set<Map.Entry<Student, String>> entries = treeMap.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName()+":"+key.getAge()+"=="+value);
}
}
}
12.集合框架(统计字符串中每个字符出现的次数)
//案例演示: 需求:统计字符串中每个字符出现的次数
//"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
public class MyTest3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一串字符");
String s = scanner.nextLine();
//"aababcabcdabcde"
//思路:
/*
* 我们已经拿到这段字符串
* 统计完之后 输出的结果是这样a(5) b(4) c(3) d(2) e(1)
* a----5
* b----4
* c----3
* d----2
* e----1
*/
//下来就是遍历集合 拼接字符串
//遍历字符串,统计每个字符的出现的次数,把他存到双列集合中
HashMap<Character, Integer> hm = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
// "aababcabcdabcde"
char ch = s.charAt(i);
//判断集合中是否包含这个字符(键)
if(!hm.containsKey(ch)){
hm.put(ch,1);
}else{
Integer value = hm.get(ch); //获取之前的值
value++;
hm.put(ch,value);//键相同 值覆盖
}
}
//System.out.println(hm);
//遍历上面的集合进行拼串 拼接成 a(5) b(4) c(3) d(2) e(1)
StringBuilder sb = new StringBuilder();
Set<Character> characters = hm.keySet();
for (Character character : characters) {
Integer value = hm.get(character);
sb.append(character).append("(").append(value).append(")");
}
String string = sb.toString();
System.out.println(string);
}
}
13.集合框架(集合嵌套之HashMap嵌套HashMap)
//案例演示:集合嵌套之HashMap嵌套HashMap
public class MyTest4 {
public static void main(String[] args) {
/*
基础班
张三 20
李四 22
就业班
王五 21
赵六 23
*/
HashMap<String, Integer> jcMap = new HashMap<>();
jcMap.put("张三",23);
jcMap.put("李四", 22);
HashMap<String, Integer> jyMap = new HashMap<>();
jyMap.put("王五", 21);
jyMap.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> maxMap = new HashMap<>();
maxMap.put("基础班",jcMap);
maxMap.put("就业班",jyMap);
//遍历
Set<String> keySet = maxMap.keySet();
for (String key : keySet) {
System.out.println(key);
HashMap<String, Integer> minMap = maxMap.get(key);
//遍历小集合
Set<String> keySet1 = minMap.keySet();
for (String s : keySet1) {
Integer value = minMap.get(s);
System.out.println("\t"+s+"\t"+value);
}
System.out.println();
}
System.out.println("========================");
Set<Map.Entry<String, HashMap<String, Integer>>> entries = maxMap.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
HashMap<String, Integer> minMap = entry.getValue();
Set<Map.Entry<String, Integer>> entries1 = minMap.entrySet();
for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {
String key1 = stringIntegerEntry.getKey();
Integer value = stringIntegerEntry.getValue();
System.out.println("\t"+key1+"\t"+value);
}
System.out.println();
}
}
}
14.集合框架(集合嵌套之HashMap嵌套ArrayList)
//案例演示:集合嵌套之HashMap嵌套ArrayList
假设HashMap集合的元素是ArrayList。有3个。
每一个ArrayList集合的值是字符串。
public class MyTest5 {
public static void main(String[] args) {
/*三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
神雕侠侣
郭靖
杨过*/
ArrayList<String> sgList = new ArrayList<>();
sgList.add("吕布");
sgList.add("周瑜");
ArrayList<String> xaList = new ArrayList<>();
xaList.add("令狐冲");
xaList.add("林平之");
ArrayList<String> sdList = new ArrayList<>();
sdList.add("郭靖");
sdList.add("杨过");
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
hashMap.put("三国演义",sgList);
hashMap.put("笑傲江湖",xaList);
hashMap.put("神雕侠侣",sdList);
//遍历
Set<Map.Entry<String, ArrayList<String>>> entries = hashMap.entrySet();
for (Map.Entry<String, ArrayList<String>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
ArrayList<String> list = entry.getValue();
for (String s : list) {
System.out.println("\t"+s);
}
System.out.println();
}
}
}
15.集合框架(集合嵌套之ArrayList嵌套HashMap)(理解)
//案例演示:集合嵌套之ArrayList嵌套HashMap
//假设ArrayList集合的元素是HashMap。有3个。
//每一个HashMap集合的键和值都是字符串。
public class MyTest6 {
public static void main(String[] args) {
/*
周瑜-- - 小乔
吕布-- - 貂蝉
郭靖-- - 黄蓉
杨过-- - 小龙女
令狐冲-- - 任盈盈
林平之-- - 岳灵珊
*/
HashMap<String, String> sgMap = new HashMap<>();
sgMap.put("周瑜","小乔");
sgMap.put("吕布","貂蝉");
HashMap<String, String> sdMap = new HashMap<>();
sdMap.put("郭靖","黄蓉");
sdMap.put("杨过","小龙女");
HashMap<String, String> xaMap = new HashMap<>();
xaMap.put("令狐冲","任盈盈");
xaMap.put("林平之","岳灵珊");
ArrayList<HashMap<String, String>> maxList = new ArrayList<>();
maxList.add(sgMap);
maxList.add(sdMap);
maxList.add(xaMap);
for (HashMap<String, String> hashMap : maxList) {
Set<Map.Entry<String, String>> entries = hashMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"------"+value);
}
System.out.println();
}
}
}
16.集合框架(HashMap和Hashtable的区别)
A:面试题
HashMap和Hashtable的区别: 查看API可以知道
HashMap: 线程不安全,效率高.允许null值和null键
Hashtable: 线程安全 , 效率低.不允许null值和null键
17.集合框架(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.集合框架(模拟斗地主洗牌和发牌)
A:案例演示: 模拟斗地主洗牌和发牌,牌没有排序
public class MyTest {
public static void main(String[] args) {
//针对 Collection 集合 Java给我们提供了一个工具类Collections
ArrayList<Integer> list = new ArrayList<>();
list.add(20);
list.add(2);
list.add(250);
list.add(140);
/* list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a-b;
}
});*/
Collections.sort(list);
System.out.println(list);
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return b-a;
}
});
System.out.println(list);
System.out.println("=======================");
//获取集合中的最值
Integer max = Collections.max(list);
Integer min = Collections.min(list);
System.out.println(max);
System.out.println(min);
//二分查找,前提是元素有序
Collections.sort(list);
System.out.println(list);
int i = Collections.binarySearch(list, 140);
System.out.println(i);
//反转集合中的元素
Collections.reverse(list);
System.out.println(list);
//随机打乱集合中的元素
Collections.shuffle(list);
System.out.println(list);
}
}
public class 斗地主 {
public static void main(String[] args) {
//模拟斗地主 实现 发牌 洗牌 看牌
//1.生成54张牌
//2.生成的牌放到容器中,找个集合
//3.发牌 三个人 留三张底牌
//4.洗牌
//5.看牌
ArrayList<String> pokerBox = new ArrayList<>();
//生成54张牌
String[] colors={"♥","♠","♦","♣"};
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (String color : colors) {
for (String num : nums) {
pokerBox.add(color.concat(num));
}
}
//System.out.println(pokerBox.size());
//手动生成大小王
//
pokerBox.add("☀");
pokerBox.add("🌙");
//洗牌
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
//发牌:传统发牌 一人一张轮换着发
// 一人给一摞牌 subList(0, 12)
// 三个人 相当于三个集合。底牌是一个集合。
//List<String> strings = pokerBox.subList(0, 12);
//List<String> strings = pokerBox.subList(12, 12+13);
//一人一张轮换着发
ArrayList<String> 星仔 = new ArrayList<>();
ArrayList<String> 刀仔 = new ArrayList<>();
ArrayList<String> 高进 = new ArrayList<>();
ArrayList<String> 底牌 = new ArrayList<>();
//发牌:
/*按照索引来发
* 星仔 0 3 6 9 12 索引%3==0
* 刀仔 1 4 7 10 13 索引%3==1
* 高进 2 5 8 11 14 索引%3==2
* */
for (int i = 0; i < pokerBox.size(); i++) {
//留三张底牌
if(i>=pokerBox.size()-3){
底牌.add(pokerBox.get(i));
}else if(i%3==0){
星仔.add(pokerBox.get(i));
}else if(i % 3 == 1){
刀仔.add(pokerBox.get(i));
}else{
高进.add(pokerBox.get(i));
}
}
//看牌
lookPoker("星仔",星仔);
lookPoker("刀仔", 刀仔);
lookPoker("高进", 高进);
lookPoker("底牌", 底牌);
}
private static void lookPoker(String name, ArrayList<String> list) {
System.out.println(name);
for (String s : list) {
System.out.print(s+"\t");
}
System.out.println();
}
}