=============================================================================
=============================================================================
涉及到的知识点有:
1:Map接口(掌握)
(1)Map接口的概述
(2)Map接口和Collection接口的区别?
(3)Map接口的功能概述
A:添加功能
B:删除功能
C:判断功能
D:获取功能
E:长度功能
(4)Map集合的遍历
A:键找值
B:键值对对象找键和值
(5)HashMap集合(唯一)
A:HashMap集合的概述
B:HashMap集合的案例
C:LinkedHashMap集合的概述(唯一和有序)
(6)TreeMap集合(排序和唯一)
A:TreeMap类概述
B:TreeMap集合的案例
(7)Map集合案例
A:统计一个字符串中每个字符出现的次数(要对着思路会写)
B:集合的嵌套遍历
(8)面试题
a:HashMap类和Hashtable类的区别
b:List、Set、Map等接口是否都继承自Map接口?
2:Collections工具类(理解)
(1)Collections类的概述
(2)面试题:Collection和Collections的区别
(3)Collections工具类的常见成员方法
(4)Collections工具类的案例
A:ArrayList集合存储自定义对象的排序
B:模拟斗地主洗牌和发牌
C:模拟斗地主洗牌和发牌并对牌进行排序
=============================================================================
=============================================================================
1:Map接口(掌握)
(1)Map接口的概述
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
(2)Map接口和Collection接口的区别?
A:Map集合存储的元素是键值对形式出现的,Map集合的键是唯一,值是可以重复的。可以把这个理解为:夫妻对。
B:Collection集合存储的元素是单独出现的,子接口Set元素唯一,子接口List元素可重复。可以把这个理解为:光棍(11.11)。
C:Map集合的数据结构只针对键有效,跟值无关。
D:Collection集合的数据结构针对元素有效。
---------------------------------------
(3)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() 返回集合中的键值对的对数


1 package cn.itcast_01;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 /*
7 * 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名,请问怎么做呢?
8 * 如果采用前面讲解过的集合,我们只能把学生学号和学生姓名作为一个对象的成员变量,然后存储整个对象,将来遍历的时候,判断,获取对应的名称。
9 * 但是呢,如果我都能把学生姓名拿出来了,我还需要根据编号去找吗?
10 *
11 * 针对我们目前的这种需求:仅仅知道学号,就想知道学生姓名的情况,Java就提供了一种新的集合 Map(接口)。
12 * 通过查看API,我们知道Map集合的一个最大的特点,就是它可以存储键值对的元素。这个时候存储我们上面的需求,就可以这样做:
13 * Set(不可重复) List(可重复)
14 * 键 映射 值
15 * 学号1 对应 姓名1
16 * 学号2 对应 姓名2
17 * 学号3 对应 姓名3
18 * 学号2(不行) 不能对应 姓名4
19 * 学号4 对应 姓名4
20 *
21 * Map集合的特点:
22 * 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
23 *
24 * Map集合和Collection集合的区别?
25 * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的。可以把这个理解为:夫妻对
26 * Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的。可以把这个理解为:光棍(11.11)
27 *
28 * 注意:
29 * Map集合的数据结构值针对键有效,跟值无关。
30 * HashMap,TreeMap等会讲。
31 * Collection集合的数据结构是针对元素有效。
32 *
33 * Map集合的功能概述:
34 * 1:添加功能
35 * V put(K key, V value) 添加元素/替换元素
36 * 如果键是第一次存储,就直接存储元素,而返回的值是null。
37 * 如果键不是第一次存储,就用现在的值把以前的值替换掉,而返回的值是以前的值。
38 * 2:删除功能
39 * void clear() 移除所有的键值对元素
40 * V remove(Object key) 根据键删除键值对元素,并把值返回
41 * 3:判断功能
42 * boolean containsKey(Object key) 判断集合是否包含指定的键
43 * boolean containsValue(Object value) 判断集合是否包含指定的值
44 * boolean isEmpty() 判断集合是否为空
45 * 4:获取功能
46 * Set<Map.Entry<K, V>> entrySet() 返回的是键值对对象的集合
47 * V get(Object key) 根据键获取值
48 * Set<K> keySet() 获取集合中所有键的集合
49 * Collection<V> values() 获取集合中所有值的集合
50 * 5:长度功能
51 * int size() 返回集合中的键值对的对数
52 */
53 public class MapDemo {
54 public static void main(String[] args) {
55 // 创建集合对象,且键和值都是字符串类型
56 Map<String, String> map = new HashMap<String, String>();
57
58 // 添加元素
59 // V put(K key, V value) 添加元素
60 // 如果键是第一次存储,就直接存储元素,而返回的值是null。
61 // 如果键不是第一次存储,就用现在的值把以前的值替换掉,而返回的值是以前的值。
62 // System.out.println("put:" + map.put("文章", "马伊俐"));
63 // System.out.println("put:" + map.put("文章", "姚笛"));
64
65 map.put("邓超", "孙俪");
66 map.put("黄晓明", "杨颖");
67 map.put("周杰伦", "蔡依林");
68 map.put("刘恺威", "杨幂");
69
70 // void clear() 移除所有的键值对元素
71 // map.clear();
72
73 // V remove(Object key) 根据键删除键值对元素,并把值返回
74 // System.out.println("remove:" + map.remove("黄晓明")); // 返回的是杨颖
75 // System.out.println("remove:" + map.remove("黄晓波")); // 键不存在的时候返回null
76
77 // boolean containsKey(Object key) 判断集合是否包含指定的键
78 // System.out.println("containsKey:" + map.containsKey("黄晓明")); // 包含指定的键时返回true
79 // System.out.println("containsKey:" + map.containsKey("黄晓波")); // 不包含指定的键时返回false
80
81 // boolean isEmpty() 判断集合是否为空
82 // System.out.println("isEmpty:"+map.isEmpty());
83
84 // int size() 返回集合中的键值对的对数
85 System.out.println("size:"+map.size()); // 4
86
87 // 输出集合名称
88 System.out.println("map:" + map); // 重写了toString()方法
89 }
90 }
MapDemo.java


1 package cn.itcast_01;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7
8 /*
9 * Map集合的功能概述:
10 * 4:获取功能
11 * V get(Object key) 根据键获取值,返回的是值
12 * Set<K> keySet() 获取集合中所有键的集合
13 * Collection<V> values() 获取集合中所有值的集合
14 */
15 public class MapDemo2 {
16 public static void main(String[] args) {
17 // 创建集合对象
18 Map<String, String> map = new HashMap<String, String>();
19
20 // 创建元素并添加元素
21 map.put("邓超", "孙俪");
22 map.put("黄晓明", "杨颖");
23 map.put("周杰伦", "蔡依林");
24 map.put("刘恺威", "杨幂");
25
26 // V get(Object key) 根据键获取值,返回的是值
27 System.out.println("get:" + map.get("周杰伦"));
28 System.out.println("get:" + map.get("周杰")); // 返回null
29 System.out.println("----------------------");
30
31 // Set<K> keySet() 获取集合中所有键的集合
32 Set<String> set = map.keySet();
33 for (String key : set) {
34 System.out.println(key);
35 }
36 System.out.println("----------------------");
37
38 // Collection<V> values() 获取集合中所有值的集合
39 Collection<String> con = map.values();
40 for (String value : con) {
41 System.out.println(value);
42 }
43 }
44 }
MapDemo2.java
---------------------------------------
(4)Map集合的遍历
A:键找值
a:获取所有键的集合
b:遍历键的集合,得到每一个键
c:根据键到集合中去找值
B:键值对对象找键和值
a:获取所有的键值对对象的集合
b:遍历键值对对象的集合,获取每一个键值对对象
c:根据键值对对象找键和值
代码体现:
Map<String,String> hm = new HashMap<String, String>();
hm.put("it002", "hello");
hm.put("it003", "world");
hm.put("it001", "java");
// 方式1:键找值
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
// 方式2:键值对对象找键和值
Set<Map.Entry<String,String>> set2 = hm.entrySet();
for (Map.Entry<String,String> me : set2) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "---" + value);
}

---------------------------------------
(5)HashMap集合(唯一)
A:HashMap集合的概述
是基于哈希表的Map接口实现类。键是哈希表结构,可以保证键的唯一性。依赖于hashCode()和equals()方法来保证唯一性的。
B:HashMap集合的案例(注意:蓝色字体为键)
a:HashMap<String, String>


1 package cn.itcast_02;
2
3 import java.util.HashMap;
4 import java.util.Set;
5
6 /*
7 * HashMap:是基于哈希表的Map接口实现类。
8 * 哈希表的作用是用来保证键的唯一性的。
9 * 依赖于hashCode()和equals()方法来保证唯一性的。
10 *
11 * 而String类默认重写了hashCode()和equals()方法。
12 *
13 * HashMap<String, String>
14 * 键:String
15 * 值:String
16 */
17 public class HashMapDemo {
18 public static void main(String[] args) {
19 // 创建集合对象
20 HashMap<String, String> hm = new HashMap<String, String>();
21
22 // 创建元素并添加元素
23 // String key1 = "it001";
24 // String value1 = "马云";
25 // hm.put(key1, value1);
26
27 // 添加元素到集合
28 hm.put("it001", "马云");
29 hm.put("it003", "马化腾");
30 hm.put("it004", "乔布斯");
31 hm.put("it005", "张朝阳");
32 hm.put("it002", "裘伯君");
33 hm.put("it001", "比尔盖茨"); // 键相同,则值覆盖。
34
35 // 遍历集合
36 Set<String> set = hm.keySet();
37 for (String key : set) {
38 String value = hm.get(key);
39 System.out.println(key + "---" + value);
40 }
41 }
42 }
HashMapDemo.java
程序运行的结果为:
it004---乔布斯
it003---马化腾
it005---张朝阳
it002---裘伯君
it001---比尔盖茨
b:HashMap<Integer, String>


1 package cn.itcast_02;
2
3 import java.util.HashMap;
4 import java.util.Set;
5
6 /*
7 * HashMap<Integer, String>
8 * 键:Integer
9 * 值:String
10 *
11 * 而Integer类默认也重写了hashCode()和equals()方法。
12 *
13 */
14 public class HashMapDemo2 {
15 public static void main(String[] args) {
16 // 创建集合对象
17 HashMap<Integer, String> hm = new HashMap<Integer, String>();
18
19 // 创建元素并添加元素
20 // Integer i = new Integer(27);
21 // Integer i = 27; // 自动装箱
22 // String s = "林青霞";
23 // hm.put(i, s);
24
25 // 添加元素到集合
26 hm.put(35, "风清扬");
27 hm.put(29, "林青霞");
28 hm.put(27, "林青霞");
29 hm.put(28, "刘意");
30
31
32 // 0开头的表示的是八进制数据,下面的写法是八进制的写法,但是不能出现8以上的单个数据
33 // hm.put(003, "hello");
34 // hm.put(006, "hello");
35 // hm.put(007, "hello");
36 // hm.put(008, "hello"); // 错误
37
38 // 遍历集合
39 Set<Integer> set = hm.keySet();
40 for (Integer key : set) {
41 String value = hm.get(key);
42 System.out.println(key + "---" + value);
43 }
44
45 // 下面这种方式仅仅是集合元素的字符串表示,不是遍历
46 // System.out.println("hm:" + hm);
47 }
48 }
HashMapDemo2.java
程序运行的结果为:
35---风清扬
27---林青霞
28---刘意
29---林青霞
c:HashMap<String, Student>


1 package cn.itcast_02;
2
3 public class Student {
4 private String name;
5 private int age;
6
7 public Student() {
8 super();
9 }
10
11 public Student(String name, int age) {
12 super();
13 this.name = name;
14 this.age = age;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32 }
Student.java


1 package cn.itcast_02;
2
3 import java.util.HashMap;
4 import java.util.Set;
5
6 /*
7 * HashMap<String, Student>
8 * 键:String 学号
9 * 值:Student 学生对象
10 */
11 public class HashMapDemo3 {
12 public static void main(String[] args) {
13 // 创建集合对象
14 HashMap<String, Student> hm = new HashMap<String, Student>();
15
16 // 创建学生对象
17 Student s1 = new Student("周星驰", 58);
18 Student s2 = new Student("刘德华", 55);
19 Student s3 = new Student("梁朝伟", 54);
20 Student s4 = new Student("郭富城", 50);
21 Student s5 = new Student("周星驰", 58); // 此时的学生对象是值,键值对的值是不具备唯一性的。
22
23 // 添加元素到集合
24 hm.put("9527", s1);
25 hm.put("9522", s2);
26 hm.put("9524", s3);
27 hm.put("9529", s4);
28 hm.put("9521", s5);
29
30 // 遍历集合
31 Set<String> set = hm.keySet();
32 for (String key : set) {
33 Student value = hm.get(key);
34 System.out.println(key + "---" + value.getName() + "---" + value.getAge());
35 }
36 }
37 }
HashMapDemo3.java
程序运行的结果为:
9521---周星驰---58
9524---梁朝伟---54
9522---刘德华---55
9529---郭富城---50
9527---周星驰---58
d:HashMap<Student, String>


1 package cn.itcast_02;
2
3 public class Student {
4 private String name;
5 private int age;
6
7 public Student() {
8 super();
9 }
10
11 public Student(String name, int age) {
12 super();
13 this.name = name;
14 this.age = age;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32
33 @Override
34 public int hashCode() {
35 final int prime = 31;
36 int result = 1;
37 result = prime * result + age;
38 result = prime * result + ((name == null) ? 0 : name.hashCode());
39 return result;
40 }
41
42 @Override
43 public boolean equals(Object obj) {
44 if (this == obj)
45 return true;
46 if (obj == null)
47 return false;
48 if (getClass() != obj.getClass())
49 return false;
50 Student other = (Student) obj;
51 if (age != other.age)
52 return false;
53 if (name == null) {
54 if (other.name != null)
55 return false;
56 } else if (!name.equals(other.name))
57 return false;
58 return true;
59 }
60
61 }
Student.java


1 package cn.itcast_02;
2
3 import java.util.HashMap;
4 import java.util.Set;
5
6 /*
7 * HashMap<Student, String>
8 * 键:Student
9 * 要求:如果键的成员变量值都相同,则为同一个对象。即:键相同,则值覆盖。
10 * 即:要手动重写键Student类的hashCode()和equals()方法。
11 * 值:String
12 */
13 public class HashMapDemo4 {
14 public static void main(String[] args) {
15 // 创建集合对象
16 HashMap<Student, String> hm = new HashMap<Student, String>();
17
18 // 创建学生对象
19 Student s1 = new Student("貂蝉", 27);
20 Student s2 = new Student("王昭君", 30);
21 Student s3 = new Student("西施", 33);
22 Student s4 = new Student("杨玉环", 35);
23 Student s5 = new Student("貂蝉", 27); // 此时的学生对象是键,而键值对的键具备唯一性。即:键相同,则值覆盖。
24
25 // 添加元素到集合
26 hm.put(s1, "8888");
27 hm.put(s2, "6666");
28 hm.put(s3, "5555");
29 hm.put(s4, "7777");
30 hm.put(s5, "9999");
31
32 // 遍历集合
33 Set<Student> set = hm.keySet();
34 for (Student key : set) {
35 String value = hm.get(key);
36 System.out.println(key.getName() + "---" + key.getAge() + "---" + value);
37 }
38 }
39 }
HashMapDemo4.java
程序运行的结果为:
王昭君---30---6666
貂蝉---27---9999
杨玉环---35---7777
西施---33---5555
C:LinkedHashMap集合的概述(唯一和有序)
是Map接口的哈希表和链表的实现类,具有可预知的迭代顺序。
由哈希表保证键的唯一性。
由链表保证键盘的有序(存储和取出的顺序一致)。


1 package cn.itcast_03;
2
3 import java.util.LinkedHashMap;
4 import java.util.Set;
5
6 /*
7 * LinkedHashMap:是Map接口的哈希表和链接列表实现类,具有可预知的迭代顺序。
8 * 由哈希表保证键的唯一性。
9 * 由链表保证键盘的有序(存储和取出的顺序一致)。
10 */
11 public class LinkedHashMapDemo {
12 public static void main(String[] args) {
13 // 创建集合对象
14 LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
15
16 // 创建并添加元素到集合
17 hm.put("2345", "hello");
18 hm.put("1234", "world");
19 hm.put("3456", "java");
20 hm.put("1234", "javaee"); // 键相同,则值覆盖。
21 hm.put("3456", "android"); // 键相同,则值覆盖。
22
23 // 遍历结合
24 Set<String> set = hm.keySet();
25 for (String key : set) {
26 String value = hm.get(key);
27 System.out.println(key + "---" + value);
28 }
29 }
30 }
LinkedHashMapDemo.java
程序运行的结果为:
2345---hello
1234---javaee
3456---android
---------------------------------------
(6)TreeMap集合(排序和唯一)
A:TreeMap类概述
是基于红黑树的Map接口的实现类。键是红黑树结构,可以保证键的排序和唯一性。
B:TreeMap集合的案例(注意:蓝色字体为键)
a:TreeMap<String, String>
自然排序:
String类默认实现了Comparable接口,重写了compareTo()方法,故而是自然排序。


1 package cn.itcast_04;
2
3 public class Student {
4 private String name;
5 private int age;
6
7 public Student() {
8 super();
9 }
10
11 public Student(String name, int age) {
12 super();
13 this.name = name;
14 this.age = age;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32 }
Student.java


1 package cn.itcast_04;
2
3 import java.util.Set;
4 import java.util.TreeMap;
5
6 /*
7 * TreeMap:是基于红黑树的Map接口的实现类。
8 * 键是红黑树结构,可以保证键的排序和唯一性
9 *
10 * TreeMap<String, String>
11 * 键:String
12 * 值:String
13 *
14 * 自然排序:
15 * String类默认实现了Comparable接口,重写了compareTo()方法,故而是自然排序。
16 */
17 public class TreeMapDemo {
18 public static void main(String[] args) {
19 // 创建集合对象
20 TreeMap<String, String> tm = new TreeMap<String, String>();
21
22 // 创建元素并添加元素到集合
23 tm.put("hello", "你好");
24 tm.put("world", "世界");
25 tm.put("java", "爪哇");
26 tm.put("world", "世界2"); // 键相同,则值覆盖。
27 tm.put("javaee", "爪哇EE");
28
29 // 遍历集合
30 Set<String> set = tm.keySet();
31 for (String key : set) {
32 String value = tm.get(key);
33 System.out.println(key + "---" + value);
34 }
35 }
36 }
TreeMapDemo.java
程序运行的结果为:
hello---你好
java---爪哇
javaee---爪哇EE
world---世界2
b:TreeMap<Student, String>
比较器排序:
让集合的构造方法接收一个比较器接口 Comparator的实现类对象,一般用匿名内部类实现。


1 package cn.itcast_04;
2
3 public class Student {
4 private String name;
5 private int age;
6
7 public Student() {
8 super();
9 }
10
11 public Student(String name, int age) {
12 super();
13 this.name = name;
14 this.age = age;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this.name = name;
23 }
24
25 public int getAge() {
26 return age;
27 }
28
29 public void setAge(int age) {
30 this.age = age;
31 }
32 }
Student.java


1 package cn.itcast_04;
2
3 import java.util.Comparator;
4 import java.util.Set;
5 import java.util.TreeMap;
6
7 /*
8 * TreeMap<Student, String>
9 * 键:Student
10 * 值:String
11 *
12 * 比较器排序:
13 * 让集合的构造方法接收一个比较器接口 Comparator的实现类对象,一般用匿名内部类实现。
14 *
15 * 如果一个方法的参数是接口,那么真正需要的是接口的实现类的对象,而且该方法只调用一次。
16 * 所以使用匿名内部类就可以实现这个东西,这样就不用自己去重新写一个具体的实现类了。其实这种方式是很常见的。
17 */
18 public class TreeMapDemo2 {
19 public static void main(String[] args) {
20 // 创建集合对象
21 TreeMap<Student, String> tm = new TreeMap<Student, String>(
22 new Comparator<Student>() {
23 @Override
24 public int compare(Student s1, Student s2) {
25 // 主要条件
26 int num = s1.getAge() - s2.getAge();
27 // 次要条件
28 int num2 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num);
29 return num2;
30 }
31 });
32
33 // 创建学生对象
34 Student s1 = new Student("潘安", 30);
35 Student s2 = new Student("柳下惠", 35);
36 Student s3 = new Student("唐伯虎", 33);
37 Student s4 = new Student("燕青", 32);
38 Student s5 = new Student("唐伯虎", 33); // 键相同,则值覆盖。即最终唐伯虎是汉朝的。
39
40 // 添加元素到集合
41 tm.put(s1, "宋朝");
42 tm.put(s2, "元朝");
43 tm.put(s3, "明朝");
44 tm.put(s4, "清朝");
45 tm.put(s5, "汉朝");
46
47 // 遍历集合
48 Set<Student> set = tm.keySet();
49 for (Student key : set) {
50 String value = tm.get(key);
51 System.out.println(key.getName() + "---" + key.getAge() + "---" + value);
52 }
53 }
54 }
TreeMapDemo2.java
程序运行的结果为:
潘安---30---宋朝
燕青---32---清朝
唐伯虎---33---汉朝
柳下惠---35---元朝
---------------------------------------
(7)Map集合案例
A:统计一个字符串中每个字符出现的次数(要对着思路会写)
示例代码如下:


1 package cn.itcast_05;
2
3 import java.util.Scanner;
4 import java.util.Set;
5 import java.util.TreeMap;
6
7 /*
8 * 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数。
9 * 要求输出结果的格式为 a(5)b(4)c(3)d(2)e(1)
10 *
11 * 分析:
12 * A:定义一个字符串(可以改进为键盘录入);
13 * B:定义一个TreeMap集合(为了使用其自带的排序功能);
14 * 键:Character
15 * 值:Integer
16 * Character类默认实现了Comparable接口,重写了compareTo()方法,故而是自然排序。
17 * C:把字符串转换为字符数组;
18 * D:遍历字符数组,得到每一个字符;
19 * E:拿得到的字符作为键到集合中去找值,看返回的值值:
20 * 如果是null:说明该键不存在,就把该字符作为键,把次数1作为值进行存储到集合中;
21 * 如果不是null:说明该键存在,就把值加1,然后重写存储该键和值到集合中。
22 * F:定义字符串缓冲区变量(对象);
23 * G:遍历集合,获取到每一个键和值,按照输出格式要求进行拼接;
24 * H:把字符串缓冲区内容转换为字符串输出。
25 *
26 * 测试:
27 * 录入:linqingxia
28 * 结果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1)
29 */
30 public class TreeMapDemo {
31 public static void main(String[] args) {
32 // 定义一个字符串(可以改进为键盘录入)
33 Scanner sc = new Scanner(System.in);
34 System.out.println("请输入一个字符串:");
35 String line = sc.nextLine();
36
37 // 定义一个TreeMap集合
38 TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
39
40 // 把字符串转换为字符数组
41 char[] chs = line.toCharArray();
42
43 // 遍历字符数组,得到每一个字符
44 for(char ch : chs){
45 // 拿刚才得到的字符作为键到集合中去找值,看返回值
46 Integer i = tm.get(ch); // 自动装箱
47
48 // 如果是null:说明该键不存在,就把该字符作为键,把次数1作为值进行存储到集合中
49 if(i == null){
50 tm.put(ch, 1);
51 }else {
52 // 如果不是null:说明该键存在,就把值加1,然后重写存储该键和值到集合中
53 i++; // 自动拆箱,再自动装箱
54 tm.put(ch,i);
55 }
56 }
57
58 // 定义字符串缓冲区(对象)
59 StringBuilder sb= new StringBuilder();
60
61 // 遍历集合,得到键和值,进行按照要求拼接
62 Set<Character> set = tm.keySet();
63 for(Character key : set){
64 Integer value = tm.get(key); // 得到键
65 sb.append(key).append("(").append(value).append(")"); // a(5)b(4)c(3)d(2)e(1)
66 }
67
68 // 把字符串缓冲区内容转换为字符串输出
69 String result = sb.toString();
70 System.out.println("result:"+result);
71 }
72 }
TreeMapDemo.java
程序运行的结果为:
请输入一个字符串:
aababcabcdabcde
result:a(5)b(4)c(3)d(2)e(1)

B:集合的嵌套遍历(注意:蓝色字体为键)
a:HashMap嵌套HashMap,即 HashMap<String, HashMap<String, Integer>>


1 package cn.itcast_05;
2
3 import java.util.HashMap;
4 import java.util.Set;
5
6 /*
7 * HashMap嵌套HashMap
8 *
9 * 存储的格式为:
10 * 传智播客
11 * 键 值
12 * jc---基础班
13 * 键 值
14 * 陈玉楼 ---20
15 * 高跃---22
16 *
17 * 键 值
18 * jy---就业班
19 * 键 值
20 * 李杰---21
21 * 曹石磊---23
22 * 输出形式为:
23 * jc
24 * 陈玉楼---20
25 * 高跃---22
26 * jy
27 * 李杰---21
28 * 曹石磊---23
29 *
30 * 先存储元素,然后遍历元素
31 */
32 public class HashMapIncludeHashMapDemo {
33 public static void main(String[] args) {
34 // 创建传智播客集合对象
35 HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>();
36
37 // 创建基础班集合对象
38 HashMap<String, Integer> jcMap = new HashMap<String, Integer>();
39 // 添加元素到基础班集合
40 jcMap.put("陈玉楼", 20);
41 jcMap.put("高跃", 22);
42 // 把基础班添加到传智播客大集合
43 czbkMap.put("jc", jcMap);
44
45 // 创建就业班集合对象
46 HashMap<String, Integer> jyMap = new HashMap<String, Integer>();
47 // 添加元素到就业班集合
48 jyMap.put("李杰", 21);
49 jyMap.put("曹石磊", 23);
50 // 把基础班添加到传智播客大集合
51 czbkMap.put("jy", jyMap);
52
53 // 遍历传智播客大集合
54 Set<String> czbkMapSet = czbkMap.keySet(); // 得到键
55 for(String czbkMapKey : czbkMapSet){ // 遍历键
56 System.out.println(czbkMapKey); // 输出键
57 HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey); // 根据键获取值,返回的是值,该值是hashMap集合
58 // 遍历班级集合
59 Set<String> czbkMapValueSet = czbkMapValue.keySet(); // 得到键
60 for(String czbkMapValueKey : czbkMapValueSet){ // 遍历键
61 Integer czbkMapValueValue = czbkMapValue.get(czbkMapValueKey); // 根据键获取值,返回的是值
62 System.out.println("\t"+czbkMapValueKey+"---"+czbkMapValueValue);
63 }
64 }
65 }
66 }
HashMapIncludeHashMapDemo.java
程序运行结果为:
jc
高跃---22
陈玉楼---20
jy
曹石磊---23
李杰---21
b:HashMap嵌套ArrayList,即 HashMap<String, ArrayList<String>>


1 package cn.itcast_05;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Set;
6
7 /*
8 * HashMap结合嵌套ArrayList集合并遍历。
9 *
10 * 需求:
11 * 假设HashMap集合的元素是ArrayList。每个HashMap集合有3个ArrayList集合元素。
12 * 每一个ArrayList集合的值是字符串。每个ArrayList集合有2个元素。
13 * 元素我已经完成,请遍历。
14 *
15 * 存储的格式为:
16 * 键 值
17 * 三国演义---ArrayList
18 * 吕布
19 * 周瑜
20 * 笑傲江湖---ArrayList
21 * 令狐冲
22 * 林平之
23 * 神雕侠侣---ArrayList
24 * 郭靖
25 * 杨过
26 *
27 * 输出形式为:
28 * 三国演义
29 * 吕布
30 * 周瑜
31 * 笑傲江湖
32 * 令狐冲
33 * 林平之
34 * 神雕侠侣
35 * 郭靖
36 * 杨过
37 */
38 public class HashMapIncludeArrayListDemo {
39 public static void main(String[] args) {
40 // 创建HashMap集合对象
41 HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
42
43 // 创建元素集合1
44 ArrayList<String> array1 = new ArrayList<String>();
45 array1.add("吕布");
46 array1.add("周瑜");
47 // 把元素添加到hm里面
48 hm.put("三国演义", array1);
49
50 // 创建元素集合2
51 ArrayList<String> array2 = new ArrayList<String>();
52 array2.add("令狐冲");
53 array2.add("林平之");
54 // 把元素添加到hm里面
55 hm.put("笑傲江湖", array2);
56
57 // 创建元素集合3
58 ArrayList<String> array3 = new ArrayList<String>();
59 array3.add("郭靖");
60 array3.add("杨过");
61 // 把元素添加到hm里面
62 hm.put("神雕侠侣", array3);
63
64 // 遍历HashMap集合
65 Set<String> set = hm.keySet(); // 得到键
66 for(String key : set){ // 遍历键
67 System.out.println(key); // 输出键
68 ArrayList<String> value = hm.get(key); // 根据键获取值,返回的是值,该值是ArrayList集合
69 // 遍历ArrayList集合
70 for(String s : value){
71 System.out.println("\t" + s);
72 }
73 }
74 }
75 }
HashMapIncludeArrayListDemo.java
程序的运行结果为:
神雕侠侣
郭靖
杨过
三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
c:ArrayList嵌套HashMap,即 ArrayList<HashMap<String, String>>


1 package cn.itcast_05;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Set;
6
7 /*
8 * ArrayList集合嵌套HashMap集合并遍历。
9 *
10 * 需求:
11 * 假设ArrayList集合的元素是HashMap。每个ArrayList集合有3个HashMap集合元素。
12 * 每一个HashMap集合的键和值都是字符串。每个HashMap集合有2个元素。
13 * 元素我已经完成,请遍历。
14 *
15 *输出形式为:
16 * ArrayList
17 * 键 值
18 * 周瑜---小乔
19 * 吕布---貂蝉
20 *
21 * 郭靖---黄蓉
22 * 杨过---小龙女
23 *
24 * 令狐冲---任盈盈
25 * 林平之---岳灵珊
26 */
27 public class ArrayListIncludeHashMapDemo {
28 public static void main(String[] args) {
29 // 创建ArrayList集合对象
30 ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
31
32 // 创建元素集合1
33 HashMap<String, String> hm1 = new HashMap<String, String>();
34 hm1.put("周瑜", "小乔");
35 hm1.put("吕布", "貂蝉");
36 // 把元素添加到array里面
37 array.add(hm1);
38
39 // 创建元素集合2
40 HashMap<String, String> hm2 = new HashMap<String, String>();
41 hm2.put("郭靖", "黄蓉");
42 hm2.put("杨过", "小龙女");
43 // 把元素添加到array里面
44 array.add(hm2);
45
46 // 创建元素集合3
47 HashMap<String, String> hm3 = new HashMap<String, String>();
48 hm3.put("令狐冲", "任盈盈");
49 hm3.put("林平之", "岳灵珊");
50 // 把元素添加到array里面
51 array.add(hm3);
52
53 // 遍历ArrayList集合
54 for (HashMap<String, String> hm : array) {
55 // 遍历HashMap集合
56 Set<String> set = hm.keySet(); // 得到键
57 for (String key : set) {
58 String value = hm.get(key); // 根据键获取值,返回的是值
59 System.out.println(key + "---" + value);
60 }
61 System.out.println();
62 }
63 }
64 }
ArrayListIncludeHashMapDemo.java
程序运行结果为:
吕布---貂蝉
周瑜---小乔
杨过---小龙女
郭靖---黄蓉
令狐冲---任盈盈
林平之---岳灵珊
d:HashMap嵌套HashMap嵌套ArrayList(多层嵌套),即 HashMap<String, HashMap<String, ArrayList<Student>>>


1 package cn.itcast_06;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Set;
6
7 /*
8 * HashMap嵌套HashMap嵌套ArrayList(多层嵌套)
9 *
10 * 为了更符合要求:
11 * 这次的数据是学生对象。
12 *
13 * 存储的格式为:
14 * 传智播客
15 * 键 值
16 * bj---北京校区
17 * 键 值
18 * jc---基础班
19 * ArrayList集合
20 * 林青霞--- 27
21 * 风清扬---30
22 * jy---就业班
23 * 赵雅芝---28
24 * 武鑫---29
25 * sh---上海校区
26 * jc---基础班
27 * 郭美美---20
28 * 犀利哥---22
29 * jy---就业班
30 * 罗玉凤---21
31 * 马征---23
32 * gz---广州校区
33 * jc---基础班
34 * 王力宏---30
35 * 李静磊---32
36 * jy---就业班
37 * 郎朗---31
38 * 柳岩---33
39 * xa---西安校区
40 * jc---基础班
41 * 范冰冰---27
42 * 刘意---30
43 * jy---就业班
44 * 李冰冰---28
45 * 张志豪---29
46 * 输出形式为:
47 * bj
48 * jc
49 * 林青霞---27
50 * 风清扬---30
51 * jy
52 * 赵雅芝---28
53 * 武鑫---29
54 * xa
55 * jc
56 * 范冰冰---27
57 * 刘意---30
58 * jy
59 * 李冰冰---28
60 * 张志豪---29
61 */
62 public class HashMapIncludeHashMapIncludeArrayListDemo {
63 public static void main(String[] args) {
64 // 创建传智播客大集合对象
65 HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
66
67 // 创建北京校区集合对象
68 HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>();
69
70 // 创建基础班集合对象
71 ArrayList<Student> array1 = new ArrayList<Student>();
72 // 创建学生对象
73 Student s1 = new Student("林青霞", 27);
74 Student s2 = new Student("风清扬", 30);
75 // 把学生对象添加进基础班集合
76 array1.add(s1);
77 array1.add(s2);
78
79 // 创建就业班集合对象
80 ArrayList<Student> array2 = new ArrayList<Student>();
81 // 创建学生对象
82 Student s3 = new Student("赵雅芝", 28);
83 Student s4 = new Student("武鑫", 29);
84 // 把学生对象添加进就业班集合
85 array2.add(s3);
86 array2.add(s4);
87
88 // 把基础班添加到北京校区集合
89 bjCzbkMap.put("jc", array1);
90 // 把就业班添加到北京校区集合
91 bjCzbkMap.put("jy", array2);
92 // 把北京校区添加到传智播客大集合
93 czbkMap.put("bj", bjCzbkMap);
94
95 // 上海校区数据自己做
96 // 广州校区数据自己做
97
98 // 创建西安校区集合对象(同理)
99 HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();
100 ArrayList<Student> array3 = new ArrayList<Student>();
101 Student s5 = new Student("范冰冰", 27);
102 Student s6 = new Student("刘意", 30);
103 array3.add(s5);
104 array3.add(s6);
105 ArrayList<Student> array4 = new ArrayList<Student>();
106 Student s7 = new Student("李冰冰", 28);
107 Student s8 = new Student("张志豪", 29);
108 array4.add(s7);
109 array4.add(s8);
110 xaCzbkMap.put("jc", array3);
111 xaCzbkMap.put("jy", array4);
112 czbkMap.put("xa", xaCzbkMap);
113
114 // 遍历传智播客大集合
115 Set<String> czbkMapSet = czbkMap.keySet(); // 得到键
116 for (String czbkMapKey : czbkMapSet) { // 遍历键
117 System.out.println(czbkMapKey); // 输出键
118 HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(czbkMapKey); // 根据键获取值,返回的是值,该值是hashMap集合
119 // 遍历北京校区集合
120 Set<String> czbkMapValueSet = czbkMapValue.keySet(); // 得到键
121 for (String czbkMapValueKey : czbkMapValueSet) { // 遍历键
122 System.out.println("\t" + czbkMapValueKey); // 输出键
123 ArrayList<Student> czbkMapValueValue = czbkMapValue.get(czbkMapValueKey); // 根据键获取值,返回的是值,该值是ArrayList集合
124 // 遍历ArrayList集合
125 for (Student s : czbkMapValueValue) {
126 System.out.println("\t\t" + s.getName() + "---" + s.getAge());
127 }
128 }
129 }
130 }
131 }
HashMapIncludeHashMapIncludeArrayListDemo.java
程序运行结果为:
bj
jc
林青霞---27
风清扬---30
jy
赵雅芝---28
武鑫---29
xa
jc
范冰冰---27
刘意---30
jy
李冰冰---28
张志豪---29
---------------------------------------
(8)面试题
a:HashMap类和Hashtable类的区别(注意:Hashtable的第二个单词是小写,继承了老版本jdk1.0的命名错误,如同System类下的静态方法Arraycopy()一样)
HashMap类:不同步,线程不安全,效率高。允许null键和null值。
Hashtable类:同步,线程安全,效率低。不允许null键和null值。
其实HashMap类就是用来替代Hashtable类的。如同ArrayList类用来替代Vector类一样。
b:List、Set、Map等接口是否都继承自Map接口?
List、Set不是继承自Map接口,它们继承自Collection接口。
Map接口本身就是一个顶层接口。
1 package cn.itcast_07;
2
3 import java.util.Hashtable;
4
5 /*
6 * 1:HashMap类和Hashtable类的区别?
7 * HashMap类:不同步,线程不安全,效率高。允许null键和null值。
8 * Hashtable类:同步,线程安全,效率低。不允许null键和null值。
9 * 其实HashMap类就是用来替代Hashtable类的。如同ArrayList类用来替代Vector类一样。
10 *
11 * 2:List、Set、Map等接口是否都继承自Map接口?
12 * List、Set不是继承自Map接口,它们继承自Collection接口。
13 * Map接口本身就是一个顶层接口。
14 */
15 public class HashtableDemo {
16 public static void main(String[] args) {
17 // HashMap<String, String> hm = new HashMap<String, String>();
18 // hm.put("it001", "hello");
19 // hm.put(null, "world");
20 // hm.put("java", null);
21
22
23 Hashtable<String, String> hm = new Hashtable<String, String>();
24 hm.put("it001", "hello");
25 hm.put(null, "world"); // NullPointerException 空指针异常
26 hm.put("java", null); // NullPointerException 空指针异常
27
28 System.out.println(hm);
29 }
30 }
-----------------------------------------------------------------------------
2:Collections工具类(理解)
(1)Collections类的概述
是针对集合进行操作的工具类,都是静态方法。在java.util包下,父类是Object。
此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。
(2)面试题:Collection和Collections的区别
a:Collection接口:是单列集合的顶层接口,有两个子接口List和Set。(Map是双列集合的顶层接口)
b:Collections工具类:是针对集合进行操作的工具类,有对集合进行排序和二分查找的方法等。
(3)Collections工具类的常见成员方法
A:public static <T> void sort(List<T> list) 排序(默认情况下是自然顺序)
B:public static <T> int binarySearch(List<?> list,T key) 二分查找(前提集合有序),若找到,则返回的是索引值;若找不到,则返回的是 -(元素个数+1)
C:public static <T> T max(Collection<?> coll) 最大值
D:public static void reverse(List<?> list) 反转
E:public static void shuffle(List<?> list) 随机置换


1 package cn.itcast_01;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.ArrayList;
6
7 /*
8 * Collections:是针对集合进行操作的工具类,都是静态方法。
9 * 是针对集合进行操作的工具类,都是静态方法。在java.util包下,父类是Object。
10 * 此类完全由在 collection 上进行操作或返回 collection 的静态方法组成。
11 *
12 * 面试题:
13 * Collection和Collections的区别?
14 * Collection接口:是单列集合的顶层接口,有子接口List和Set。(Map是双列集合的顶层接口)
15 * Collections工具类:是针对集合操作的工具类,有对集合进行排序和二分查找的方法等。
16 *
17 * Collections类的常见成员方法:
18 * public static <T> void sort(List<T> list) 排序(默认情况下是自然顺序)
19 * public static <T> int binarySearch(List<?> list,T key) 二分查找(前提集合有序),若找到,则返回的是索引值;若找不到,则返回的是 -(元素个数+1)
20 * public static <T> T max(Collection<?> coll) 最大值
21 * public static void reverse(List<?> list) 反转
22 * public static void shuffle(List<?> list) 随机置换
23 *
24 * Integer类和String类都默认实现了Comparable接口,重写了compareTo()方法,故而是自然排序。
25 * 但是自定义对象默认没有实现Comparable接口。
26 */
27 public class CollectionsDemo {
28 public static void main(String[] args) {
29 // 创建集合对象,List接口是集合,以List进行举例
30 List<Integer> list = new ArrayList<Integer>();
31
32 // 添加元素
33 list.add(30); // 自动装箱
34 list.add(20);
35 list.add(50);
36 list.add(10);
37 list.add(40);
38
39 System.out.println("list:" + list); // list:[30, 20, 50, 10, 40]
40
41 // public static <T> void sort(List<T> list) 排序(默认情况下是自然顺序)
42 // Collections.sort(list);
43 // System.out.println("list:" + list); // list:[10, 20, 30, 40, 50]
44
45 // public static <T> int binarySearch(List<?> list,T key) 二分查找(前提集合有序),若找到,则返回的是索引值;若找不到,则返回的是 -(元素个数+1)
46 // System.out.println("binarySearch:" + Collections.binarySearch(list, 30)); // binarySearch:2
47 // System.out.println("binarySearch:" + Collections.binarySearch(list, 300));// binarySearch:-6
48
49 // public static <T> T max(Collection<?> coll) 最大值
50 // System.out.println("max:" + Collections.max(list)); // max:50
51
52 // public static void reverse(List<?> list) 反转
53 // Collections.reverse(list); // list:[40, 10, 50, 20, 30]
54 // System.out.println("list:" + list);
55
56 // public static void shuffle(List<?> list) 随机置换
57 Collections.shuffle(list);
58 System.out.println("list:" + list);
59 }
60 }
CollectionsDemo.java
(4)Collections工具类的案例
A:ArrayList集合存储自定义对象的排序
Collections可以针对ArrayList存储基本包装类的元素可以排序,存储自定义对象可不可以排序呢?
因为Integer类和String类都默认实现了Comparable接口,重写了compareTo()方法,故而自然排序。
但是自定义对象默认没有实现Comparable接口,所以不能进行自然排序。
示例代码如下:(注意:示例代码中既有自然排序也有比较器排序)
1 package cn.itcast_02;
2
3 /**
4 * @author Administrator
5 *
6 */
7 public class Student implements Comparable<Student> {
8 private String name;
9 private int age;
10
11 public Student() {
12 super();
13 }
14
15 public Student(String name, int age) {
16 super();
17 this.name = name;
18 this.age = age;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public void setName(String name) {
26 this.name = name;
27 }
28
29 public int getAge() {
30 return age;
31 }
32
33 public void setAge(int age) {
34 this.age = age;
35 }
36
37 @Override
38 public int compareTo(Student s) {
39 int num = this.age - s.age; // 从上到下,从小到大,顺序,升序
40 int num2 = (num == 0 ? this.name.compareTo(s.name) : num);
41 return num2;
42 }
43
44 }
1 package cn.itcast_02;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 /*
9 * Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢?
10 */
11 public class CollectionsDemo {
12 public static void main(String[] args) {
13 // 创建集合对象,List接口是集合,以List进行举例
14 List<Student> list = new ArrayList<Student>();
15
16 // 创建学生对象
17 Student s1 = new Student("林青霞", 27);
18 Student s2 = new Student("风清扬", 30);
19 Student s3 = new Student("刘晓曲", 28);
20 Student s4 = new Student("武鑫", 29);
21 Student s5 = new Student("林青霞", 27);
22
23 // 添加元素对象到集合
24 list.add(s1);
25 list.add(s2);
26 list.add(s3);
27 list.add(s4);
28 list.add(s5);
29
30 // 调用Collections工具类中的方法使得ArrayList集合进行排序
31 // 方式一:自然排序
32 // Collections.sort(list); // 从上到下,从小到大,顺序,升序
33 // 如果学生类中的元素要想能够进行自然排序,学生类就必须实现自然排序接口,然后在学生类中重写compareTo()方法。否则编译就通不过。
34
35 // 如何去除ArrayList集合中的重复元素呢?
36 // 步骤一:我们需要重写自定义对象学生类中的equals()方法,自动生成即可。
37 // 步骤二:创建新集合;
38 // 步骤三:遍历旧集合,获取得到每一个元素;拿这个元素到新集合去找,看有没有,新集合中没有该元素就添加,有就不搭理它;
39 // 步骤四:遍历新集合。
40 // 综上:我们可以写一个去除ArrayList集合中的重复元素的功能。
41
42 // 这样我们通过Collectins工具类中的排序方法和去除ArrayList集合中的重复元素的功能,
43 // 就能实现ArrayList集合的排序和去重复元素了!!!
44 // 这个时候我还要Set集合、Map结合干嘛呢!!!
45 // 因为我的ArrayList集合就能干所有的事情了!!!
46
47 // 方式二:比较器排序,一般用匿名内部类实现,在匿名内部类中重写compare()方法。
48 // 如果同时有自然排序和比较器排序,则以比较器排序为主。
49 Collections.sort(list, new Comparator<Student>() {
50 // @Override
51 // public int compare(Student s1, Student s2) {
52 // return 0;
53 // }
54
55 @Override
56 public int compare(Student s1, Student s2) {
57 int num = s2.getAge() - s1.getAge(); // 从上到下,从大到小,倒序,降序
58 int num2 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num);
59 return num2;
60 }
61 });
62
63 // 遍历集合
64 for (Student s : list) {
65 System.out.println(s.getName() + "---" + s.getAge());
66 }
67 }
68 }
B:模拟斗地主洗牌和发牌


1 package cn.itcast_03;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 /*
7 * 模拟斗地主洗牌和发牌
8 *
9 * 分析:
10 * A:创建一个牌盒(集合)
11 * B:装牌
12 * C:洗牌
13 * D:发牌
14 * E:看牌
15 */
16 public class PokerDemo {
17 public static void main(String[] args) {
18 // 创建一个牌盒(集合)
19 ArrayList<String> array = new ArrayList<String>();
20
21 // 装牌
22 // 牌的組成:
23 // 黑桃A,黑桃2,黑桃3,...,黑桃K
24 // 红桃A,...
25 // 梅花A,...
26 // 方块A,...
27 // 定义一个花色字符串数组
28 String[] colors = { "", "", "", "" };
29 // 定义一个点数字符串数组
30 String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
31 // 装牌
32 // 增强for遍历花色(外循环) 4组X每组13个 A 2 3 ... Q K A 2 3 ... Q K A 2 3 ... Q K A 2 3 ... Q K
33 for (String color : colors) {
34 // 增强for遍历色数字(内循环)
35 for (String number : numbers) {
36 array.add(color.concat(number));
37 }
38 }
39 array.add("小王");
40 array.add("大王");
41
42 System.out.println("array:" + array); // array:[A, 2, ... Q, K, A, 2, ... Q, K, A, 2, ... Q, K, A, 2, ... Q, K, 小王, 大王]
43
44 // 洗牌
45 Collections.shuffle(array);
46
47 System.out.println("array:" + array);
48
49 // 发牌
50 ArrayList<String> fengQingYang = new ArrayList<String>();
51 ArrayList<String> linQingXia = new ArrayList<String>();
52 ArrayList<String> liuYi = new ArrayList<String>();
53 ArrayList<String> diPai = new ArrayList<String>();
54
55 // int size() 元素的个数(即集合的长度) array.size() 集合的长度为54
56 for (int x = 0; x < array.size(); x++) {
57 if (x >= array.size() - 3) { // 51 >= 54 - 3 = 51
58 diPai.add(array.get(x)); // array.get(x) x是索引,当x = 51时,表示的是第52张牌
59 } else if (x % 3 == 0) {
60 fengQingYang.add(array.get(x));
61 } else if (x % 3 == 1) {
62 linQingXia.add(array.get(x));
63 } else if (x % 3 == 2) {
64 liuYi.add(array.get(x));
65 }
66 }
67
68 // 看牌
69 lookPoker("风清扬", fengQingYang);
70 lookPoker("林青霞", linQingXia);
71 lookPoker("刘意", liuYi);
72 lookPoker("底牌", diPai);
73 }
74
75 public static void lookPoker(String name, ArrayList<String> array) {
76 System.out.print(name + "的牌是:");
77 for (String s : array) {
78 System.out.print(s + " ");
79 }
80 System.out.println();
81 }
82 }
PokerDemo.java
C:模拟斗地主洗牌和发牌并对牌进行排序



1 package cn.itcast_04;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.TreeSet;
7
8 /*
9 * 模拟斗地主洗牌和发牌并对牌进行排序
10 *
11 * 思路:
12 * A:创建一个HashMap集合(键值对:存储制定的规则)
13 * B:创建一个ArrayList集合(创建一个牌盒)
14 * C:创建点数字符串数组和花字符串色数组
15 * D:从0开始往HashMap里面存储编号(键),并存储对应的牌(值),同时往ArrayList里面存储编号即可。
16 * E:洗牌(洗的是编号)
17 * F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收,因为TreeSet集合保证元素排序和唯一性)
18 * G:看牌(遍历TreeSet集合,获取编号后,到HashMap集合找对应的牌)
19 */
20 public class PokerDemo {
21 public static void main(String[] args) {
22 // 创建一个HashMap集合(键值对:存储制定的规则)
23 HashMap<Integer, String> hm = new HashMap<Integer, String>();
24
25 // 创建一个ArrayList集合(创建一个牌盒)
26 ArrayList<Integer> array = new ArrayList<Integer>();
27
28 // 创建点数字符串数组和花色字符串数组
29 // 定义一个点数字符串数组
30 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2", };
31 // 定义一个花色字符串数组
32 String[] colors = { "", "", "", "" };
33
34 // 从0开始往HashMap里面存储编号(键),并存储对应的牌(值),同时往ArrayList里面存储编号即可。
35 int index = 0;
36 // 增强for遍历色数字(外循环) 13组X每组4个 3 3 3 3 4 4 4 4 ...... A A A A 2 2 2 2
37 for (String number : numbers) {
38 // 增强for遍历花色(内循环)
39 for (String color : colors) {
40 String poker = color.concat(number);
41 hm.put(index, poker);
42 array.add(index);
43 index++;
44 }
45 }
46 hm.put(index, "小王");
47 array.add(index);
48 index++;
49 hm.put(index, "大王");
50 array.add(index);
51
52 // 洗牌(洗的是编号)
53 Collections.shuffle(array);
54
55 // 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收,因为TreeSet集合保证元素排序和唯一性)
56 TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
57 TreeSet<Integer> linQingXia = new TreeSet<Integer>();
58 TreeSet<Integer> liuYi = new TreeSet<Integer>();
59 TreeSet<Integer> diPai = new TreeSet<Integer>();
60
61 // int size() 元素的个数(即集合的长度) array.size() 集合的长度为54
62 for (int x = 0; x < array.size(); x++) {
63 if (x >= array.size() - 3) { // 51 >= 54 - 3 = 51
64 diPai.add(array.get(x)); // array.get(x) x是索引,当x = 51时,表示的是第52张牌
65 } else if (x % 3 == 0) {
66 fengQingYang.add(array.get(x));
67 } else if (x % 3 == 1) {
68 linQingXia.add(array.get(x));
69 } else if (x % 3 == 2) {
70 liuYi.add(array.get(x));
71 }
72 }
73
74 // 看牌(遍历TreeSet集合,获取编号后,到HashMap集合找对应的牌)
75 lookPoker("风清扬", fengQingYang, hm);
76 lookPoker("林青霞", linQingXia, hm);
77 lookPoker("刘意", liuYi, hm);
78 lookPoker("底牌", diPai, hm);
79 }
80
81 // 写看牌的功能
82 public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
83 System.out.print(name + "的牌是:");
84 for (Integer key : ts) { // 遍历键
85 String value = hm.get(key); // 根据键,获取对应的值
86 System.out.print(value + " ");
87 }
88 System.out.println();
89 }
90 }
PokerDemo.java
=============================================================================
我的GitHub地址:
https://github.com/heizemingjun
我的博客园地址:
http://www.cnblogs.com/chenmingjun
我的蚂蚁笔记博客地址:
http://blog.leanote.com/chenmingjun
Copyright ©2018 黑泽明军
【转载文章务必保留出处和署名,谢谢!】