javaSE第十八天
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaSE第十八天相关的知识,希望对你有一定的参考价值。
A:HashMap<String,String>????199
B:HashMap<Integer,String>????200
C:HashMap<String,Student>????201
D:HashMap<Student,String>????203
允许value值为null,不允许key值为null ????205
A:TreeMap<String,String>????205
B:TreeMap<Student,String>????206
(2)面试题:Collection和Collections的区别????216
A:ArrayList集合存储自定义对象的排序????218
2:ConnectionsDemo.java文件????219
C:模拟斗地主洗牌和发牌并对牌进行排序的原理图解????221
?
?
?
第十八天
1:Map(掌握)
????(1)定义:
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
????(2)Map和Collection的区别?
????????A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
????????B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍
????(3)Map接口功能概述(自己补齐)
????????A:添加功能
????????????public V put(K key, V value):添加元素,这个还有另外一个功能
????????????如果键是第一次存储,就直接存储元素,返回null
????????????如果键不是第一次存储,就用值把以前的值替换掉,返回以前的值
????????B:删除功能
????????????public void clear():移除集合中的所有元素
????????????public V remove(Object key):根据键删除键值对元素,并把值返回
????????????????????????????????删除不存在的键,返回null
????????C:判断功能
????????????public boolean containsKey(Object key):判断集合是否包含指定的键
????????????public boolean containsValue(Object value)判断集合是否包含指定的值
????????????public boolean isEmpty():判断集合是否为空
????????D:获取功能
????????????Set<Map.Entry<K, V>> entrySet()
????????????public V get(Object key):根据键获取值
????????????public Set<K> keySet():获取集合中所有键的集合
????????????public Collection<V> values():获取集合中所有值的集合
????????E:长度功能
????????????public int size():返回集合中键值对的对数
????????F:简单方法功能演示:
import java.util.HashMap; import java.util.Map; ? /* * 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名,请问怎么做呢? * 如果采用前面讲解过的集合,我们只能把学号和学生姓名作为一个对象的成员,然后存储整个对象,将来遍历的时候,判断,获取对应的名称。 * 但是呢,如果我都能把学生姓名拿出来了,我还需要根据编号去找吗? * 针对我们目前的这种需求:仅仅知道学号,就想知道学生姓名的情况,Java就提供了一种新的集合 Map。 * 通过查看API,我们知道Map集合的一个最大的特点,就是它可以存储键值对的元素。这个时候存储我们上面的需求,就可以这样做 * ????????学号1????????姓名1 * ????????学号2 ???? 姓名2 * ????????学号3????????姓名3 * ????????学号2(不行) 姓名4 * ????????学号4 姓名4 * Map集合的特点: * ????????将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。 * * Map集合和Collection集合的区别? * ????????Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的。可以把这个理解为:夫妻对 * ????????Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的。可以把这个理解为:光棍(11.11) * * 注意: * ????????Map集合的数据结构值针对键有效,跟值无关???? * ????????????HashMap,TreeMap等会讲。 *????????Collection集合的数据结构是针对元素有效 * * Map集合的功能概述: * 1:添加功能 * ????????V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲 * ????????????如果键是第一次存储,就直接存储元素,返回null * ????????????如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值 * 2:删除功能 * ????????void clear():移除所有的键值对元素 * ????????V remove(Object key):根据键删除键值对元素,并把值返回 * 3:判断功能 * ????????boolean containsKey(Object key):判断集合是否包含指定的键 * ????????boolean containsValue(Object value):判断集合是否包含指定的值 * ????????boolean isEmpty():判断集合是否为空 * 4:获取功能 * ????????Set<Map.Entry<K,V>> entrySet():??? * ????????V get(Object key):根据键获取值 * ????????Set<K> keySet():获取集合中所有键的集合 * ????????Collection<V> values():获取集合中所有值的集合 * 5:长度功能 * ????????int size():返回集合中的键值对的对数 */ class MapDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????Map<String, String> map = new HashMap<String, String>(); ? ????????// 添加元素 ????????// V put(K key,V value):添加元素。这个其实还有另一个功能?先不告诉你,等会讲 ????????// System.out.println("put:" + map.put("文章", "马伊俐"));//map.put(),第一次存储V,V返回null ????????// System.out.println("put:" + map.put("文章", "姚笛"));//map.put(),第二次存储V,V替换,返回原来的V ? ????????map.put("邓超", "孙俪"); ????????map.put("黄晓明", "杨颖"); ????????map.put("周杰伦", "蔡依林"); ????????map.put("刘恺威", "杨幂"); ? ????????// void clear():移除所有的键值对元素 ????????// map.clear(); ? ????????// V remove(Object key):根据键删除键值对元素,并把值返回 ????????// System.out.println("remove:" + map.remove("黄晓明"));//返回杨颖 ????????// System.out.println("remove:" + map.remove("黄晓波"));//删除不存在的键,返回null ? ????????// boolean containsKey(Object key):判断集合是否包含指定的键 ????????// System.out.println("containsKey:" + map.containsKey("黄晓明"));//true ????????// System.out.println("containsKey:" + map.containsKey("黄晓波"));//false ? ????????// boolean isEmpty():判断集合是否为空 ????????// System.out.println("isEmpty:"+map.isEmpty()); ???????? ????????//int size():返回集合中的键值对的对数 ????????System.out.println("size:"+map.size()); ? ????????// 输出集合名称 ????????System.out.println("map:" + map); ????} } |
????????G:获取功能演示:
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; ? /* * 获取功能: * V get(Object key):根据键获取值,如果没有该键,就返回null * Set<K> keySet():获取集合中所有键的集合 * Collection<V> values():获取集合中所有值的集合 */ class MapDemo2 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????Map<String, String> map = new HashMap<String, String>(); ? ????????// 创建元素并添加元素 ????????map.put("邓超", "孙俪"); ????????map.put("黄晓明", "杨颖"); ????????map.put("周杰伦", "蔡依林"); ????????map.put("刘恺威", "杨幂"); ? ????????// V get(Object key):根据键获取值 ????????System.out.println("get:" + map.get("周杰伦"));//蔡依林 ????????System.out.println("get:" + map.get("周杰")); // 返回null ????????System.out.println("----------------------"); ? ????????// Set<K> keySet():获取集合中所有键的集合 ????????Set<String> set = map.keySet();//set是键的集合 ????????for (String key : set) { ????????????System.out.println(key); ????????} ????????System.out.println("----------------------"); ? ????????// Collection<V> values():获取集合中所有值的集合 ????????Collection<String> con = map.values();//con是值的集合 ????????for (String value : con) { ????????????System.out.println(value); ????????} ????} } |
????????H:Map集合的遍历方式一
import java.util.HashMap; import java.util.Map; import java.util.Set; ? /* * Map集合的遍历。 * Map -- 夫妻对 * 思路: * ????????A:把所有的丈夫给集中起来。 * ????????B:遍历丈夫的集合,获取得到每一个丈夫。 * ????????C:让丈夫去找自己的妻子。 * * 转换: * ????????A:获取所有的键 * ????????B:遍历键的集合,获取得到每一个键 * ????????C:根据键去找值 */ class MapDemo3 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????Map<String, String> map = new HashMap<String, String>(); ? ????????// 创建元素并添加到集合 ????????map.put("杨过", "小龙女"); ????????map.put("郭靖", "黄蓉"); ????????map.put("杨康", "穆念慈"); ????????map.put("陈玄风", "梅超风"); ? ????????// 遍历 ????????// 获取所有的键 ????????Set<String> set = map.keySet(); ????????// 遍历键的集合,获取得到每一个键 ????????for (String key : set) { ????????????// 根据键去找值 ????????????String value = map.get(key); ????????????System.out.println(key + "---" + value); ????????} ????} } |
????????J:Map集合的遍历方式二entrySet
import java.util.HashMap; import java.util.Map; import java.util.Set; ? /* * Map集合的遍历。 * Map -- 夫妻对 * * 思路: * ????????A:获取所有结婚证的集合 * ????????B:遍历结婚证的集合,得到每一个结婚证 * ????????C:根据结婚证获取丈夫和妻子 * * 转换: * ????????A:获取所有键值对对象的集合 * ????????B:遍历键值对对象的集合,得到每一个键值对对象 * ????????C:根据键值对对象获取键和值 * * 这里面最麻烦的就是键值对对象如何表示呢? * 看看我们开始的一个方法: * ????????Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合 */ class MapDemo4 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????Map<String, String> map = new HashMap<String, String>(); ? ????????// 创建元素并添加到集合 ????????map.put("杨过", "小龙女"); ????????map.put("郭靖", "黄蓉"); ????????map.put("杨康", "穆念慈"); ????????map.put("陈玄风", "梅超风"); ? ????????// 获取所有键值对对象的集合 ????????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); ????????} ????} } |
?
????(4)Map集合的遍历
A:Map集合遍历两种方式比较图解
????????B:键找值
????????????a:获取所有键的集合
????????????b:遍历键的集合,得到每一个键
????????????c:根据键到集合中去找值
????????
????????C:键值对对象找键和值
????????????a:获取所有的键值对对象的集合
????????????b:遍历键值对对象的集合,获取每一个键值对对象
????????????c:根据键值对对象去获取键和值
????????????
????????代码体现:
????????????
// 创建集合对象 ????????Map<String, String> map = new HashMap<String, String>(); ? ????????// 创建元素并添加到集合 ????????map.put("杨过", "小龙女"); ????????map.put("郭靖", "黄蓉"); ????????map.put("杨康", "穆念慈"); ????????map.put("陈玄风", "梅超风"); |
?
????????????
????????????//方式1 键找值
????????????
//方式一:键找值 ????????Set<String> keys = map.keySet(); ????????for(String key : keys) { ????????????String value = map.get(key); ????????????System.out.println(key + "--" + value); ????????} |
?
????????????
????????????//方式2 键值对对象找键和值
????????????
// 方式二:获取所有键值对对象的集合 ????????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); ????????} |
?
????(5)HashMap集合的练习
????????????允许插入null-null键值对
????????A:HashMap<String,String>
import java.util.HashMap; import java.util.Set; ? /* * HashMap:是基于哈希表的Map接口实现。 * 哈希表的作用是用来保证键的唯一性的。 * * HashMap<String,String> * 键:String * 值:String */ class HashMapDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<String, String> hm = new HashMap<String, String>(); ? ????????// 创建元素并添加元素 ???????? ????????hm.put("it001", "马云"); ????????hm.put("it003", "马化腾"); ????????hm.put("it004", "乔布斯"); ????????hm.put("it005", "张朝阳"); ????????hm.put("it002", "裘伯君"); // wps ????????hm.put("it001", "比尔盖茨"); ? ????????// 遍历 ????????Set<String> set = hm.keySet(); ????????for (String key : set) { ????????????String value = hm.get(key); ????????????System.out.println(key + "---" + value); ????????} ????} } |
?
????????B:HashMap<Integer,String>
import java.util.HashMap; import java.util.Set; ? /* * HashMap<Integer,String> * 键:Integer * 值:String */ class HashMapDemo2 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<Integer, String> hm = new HashMap<Integer, String>(); ? ????????// 创建元素并添加元素 ????????// Integer i = new Integer(27); ????????// Integer i = 27; ????????// String s = "林青霞"; ????????// hm.put(i, s); ? ????????hm.put(27, "林青霞"); ????????hm.put(30, "风清扬"); ????????hm.put(28, "刘意"); ????????hm.put(29, "林青霞"); ? ????????// 0开头的是八进制数据//下面的写法是八进制,但是不能出现8以上的单个数据 ????????// hm.put(003, "hello"); ????????// hm.put(006, "hello"); ????????// hm.put(007, "hello"); ????????// hm.put(008, "hello"); ? ????????// 遍历 ????????Set<Integer> set = hm.keySet(); ????????for (Integer key : set) { ????????????String value = hm.get(key); ????????????System.out.println(key + "---" + value); ????????} ? ????????// 下面这种方式仅仅是集合的元素的字符串表示 ????????// System.out.println("hm:" + hm); ????} } |
?
????????C:HashMap<String,Student>
????????????1:Student.java文件
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?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; ????} ? |
?
????????????2:HashMapDemo3.java
import java.util.HashMap; import java.util.Set; ? /* * HashMap<String,Student> * 键:String????学号 * 值:Student 学生对象 */ class HashMapDemo3 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<String, Student> hm = new HashMap<String, Student>(); ? ????????// 创建学生对象 ????????Student s1 = new Student("周星驰", 58); ????????Student s2 = new Student("刘德华", 55); ????????Student s3 = new Student("梁朝伟", 54); ????????Student s4 = new Student("刘嘉玲", 50); ? ????????// 添加元素 ????????hm.put("9527", s1); ????????hm.put("9522", s2); ????????hm.put("9524", s3); ????????hm.put("9529", s4); ? ????????// 遍历 ????????Set<String> set = hm.keySet(); ????????for (String key : set) { ????????????// 注意了:这次值不是字符串了 ????????????// String value = hm.get(key); ????????????Student value = hm.get(key); ????????????System.out.println(key + "---" + value.getName() + "---" ????????????????????+ value.getAge()); ????????} ????} } |
?
????????D:HashMap<Student,String>
import java.util.HashMap; import java.util.Set; ? /* * HashMap<Student,String> * 键:Student * ????????要求:如果两个对象的成员变量值都相同,则为同一个对象。 * 值:String */ class HashMapDemo4 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<Student, String> hm = new HashMap<Student, String>(); ? ????????// 创建学生对象 ????????Student s1 = new Student("貂蝉", 27); ????????Student s2 = new Student("王昭君", 30); ????????Student s3 = new Student("西施", 33); ????????Student s4 = new Student("杨玉环", 35); ????????Student s5 = new Student("貂蝉", 27); ? ????????// 添加元素 ????????hm.put(s1, "8888"); ????????hm.put(s2, "6666"); ????????hm.put(s3, "5555"); ????????hm.put(s4, "7777"); ????????hm.put(s5, "9999"); ? ????????// 遍历 ????????Set<Student> set = hm.keySet(); ????????for (Student key : set) { ????????????String value = hm.get(key); ????????????System.out.println(key.getName() + "---" + key.getAge() + "---" ????????????????????+ value); ????????} ????} } |
????????E:LinkedHashMap的简介
import java.util.LinkedHashMap; import java.util.Set; ? /* * LinkedHashMap:是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。 * 由哈希表保证键的唯一性 * 由链表保证键盘的有序(存储和取出的顺序一致) */ class LinkedHashMapDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????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); ????????} ????} } |
?
?
????(6)TreeMap集合的练习????
???????? 允许value值为null,不允许key值为null ????
????????A:TreeMap<String,String>
import java.util.Set; import java.util.TreeMap; ? /* * TreeMap:是基于红黑树的Map接口的实现。 * * HashMap<String,String> * 键:String * 值:String */ class TreeMapDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????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); ????????} ????} } |
?
????????B:TreeMap<Student,String>
????????????1:Student.java
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; ????} ? } |
?
????????????2:TreeMapDemo2.java
import java.util.Comparator; import java.util.Set; import java.util.TreeMap; ? /* * TreeMap<Student,String> * 键:Student * 值:String */ class TreeMapDemo2 { ????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; ????????????????????} ????????????????}); ? ????????// 创建学生对象 ????????Student s1 = new Student("潘安", 30); ????????Student s2 = new Student("柳下惠", 35); ????????Student s3 = new Student("唐伯虎", 33); ????????Student s4 = new Student("燕青", 32); ????????Student s5 = new Student("唐伯虎", 33); ? ????????// 存储元素 ????????tm.put(s1, "宋朝"); ????????tm.put(s2, "元朝"); ????????tm.put(s3, "明朝"); ????????tm.put(s4, "清朝"); ????????tm.put(s5, "汉朝"); ? ????????// 遍历 ????????Set<Student> set = tm.keySet(); ????????for (Student key : set) { ????????????String value = tm.get(key); ????????????System.out.println(key.getName() + "---" + key.getAge() + "---" ????????????????????+ value); ????????} ????} } |
?
????(7)案例
????????A:统计一个字符串中每个字符出现的次数
统计一个字符串中每个字符出现次数图解
import java.util.Scanner; import java.util.Set; import java.util.TreeMap; ? /* * 需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1) * * 分析: * ????????A:定义一个字符串(可以改进为键盘录入) * ????????B:定义一个TreeMap集合 * ????????????键:Character * ????????????值:Integer * ????????C:把字符串转换为字符数组 * ????????D:遍历字符数组,得到每一个字符 * ????????E:拿刚才得到的字符作为键到集合中去找值,看返回值 * ????????????是null:说明该键不存在,就把该字符作为键,1作为值存储 * ????????????不是null:说明该键存在,就把值加1,然后重写存储该键和值 * ????????F:定义字符串缓冲区变量 * ????????G:遍历集合,得到键和值,进行按照要求拼接 * ????????H:把字符串缓冲区转换为字符串输出 * * 录入:linqingxia * 结果:result:a(1)g(1)i(3)l(1)n(2)q(1)x(1) */ class TreeMapDemo { ????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:"+result); ????} } |
?
????????B:集合的嵌套遍历
????????????a:HashMap嵌套HashMap
import java.util.HashMap; import java.util.Set; ? /* * HashMap嵌套HashMap * * 传智播客 * ????????jc????基础班 * ????????????????陈玉楼????????20 * ????????????????高跃????????22 * ????????jy????就业班 * ????????????????李杰????????21 * ????????????????曹石磊????????23 * * 先存储元素,然后遍历元素 */ class HashMapDemo2 { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<String, HashMap<String, Integer>> czbkMap = new HashMap<String, HashMap<String, Integer>>(); ? ????????// 创建基础班集合对象 ????????HashMap<String, Integer> jcMap = new HashMap<String, Integer>(); ????????// 添加元素 ????????jcMap.put("陈玉楼", 20); ????????jcMap.put("高跃", 22); ????????// 把基础班添加到大集合 ????????czbkMap.put("jc", jcMap); ? ????????// 创建就业班集合对象 ????????HashMap<String, Integer> jyMap = new HashMap<String, Integer>(); ????????// 添加元素 ????????jyMap.put("李杰", 21); ????????jyMap.put("曹石磊", 23); ????????// 把基础班添加到大集合 ????????czbkMap.put("jy", jyMap); ???????? ????????//遍历集合 ????????Set<String> czbkMapSet = czbkMap.keySet(); ????????for(String czbkMapKey : czbkMapSet){ ????????????System.out.println(czbkMapKey); ????????????HashMap<String, Integer> czbkMapValue = czbkMap.get(czbkMapKey); ????????????Set<String> czbkMapValueSet = czbkMapValue.keySet(); ????????????for(String czbkMapValueKey : czbkMapValueSet){ ????????????????Integer czbkMapValueValue = czbkMapValue.get(czbkMapValueKey); ????????????????System.out.println("\t"+czbkMapValueKey+"---"+czbkMapValueValue); ????????????} ????????} ????} } |
?
????????????b:HashMap嵌套ArrayList
import java.util.ArrayList; import java.util.HashMap; import java.util.Set; ? /* *需求: *假设HashMap集合的元素是ArrayList。有3个。 *每一个ArrayList集合的值是字符串。 *元素我已经完成,请遍历。 *结果: *???????? 三国演义 *?????????????吕布 *?????????????周瑜 *???????? 笑傲江湖 *?????????????令狐冲 *?????????????林平之 *???????? 神雕侠侣 *?????????????郭靖 *?????????????杨过 */ class HashMapIncludeArrayListDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>(); ? ????????// 创建元素集合1 ????????ArrayList<String> array1 = new ArrayList<String>(); ????????array1.add("吕布"); ????????array1.add("周瑜"); ????????hm.put("三国演义", array1); ? ????????// 创建元素集合2 ????????ArrayList<String> array2 = new ArrayList<String>(); ????????array2.add("令狐冲"); ????????array2.add("林平之"); ????????hm.put("笑傲江湖", array2); ? ????????// 创建元素集合3 ????????ArrayList<String> array3 = new ArrayList<String>(); ????????array3.add("郭靖"); ????????array3.add("杨过"); ????????hm.put("神雕侠侣", array3); ???????? ????????//遍历集合 ????????Set<String> set = hm.keySet(); ????????for(String key : set){ ????????????System.out.println(key); ????????????ArrayList<String> value = hm.get(key); ????????????for(String s : value){ ????????????????System.out.println("\t"+s); ????????????} ????????} ????} } |
?
????????????c:ArrayList嵌套HashMap
import java.util.ArrayList; import java.util.HashMap; import java.util.Set; ? /* ArrayList集合嵌套HashMap集合并遍历。 需求: 假设ArrayList集合的元素是HashMap。有3个。 每一个HashMap集合的键和值都是字符串。 元素我已经完成,请遍历。 结果: 周瑜---小乔 吕布---貂蝉 ? 郭靖---黄蓉 杨过---小龙女 ? 令狐冲---任盈盈 林平之---岳灵珊 */ class ArrayListIncludeHashMapDemo { ????public?static?void main(String[] args) { ????????// 创建集合对象 ????????ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>(); ? ????????// 创建元素1 ????????HashMap<String, String> hm1 = new HashMap<String, String>(); ????????hm1.put("周瑜", "小乔"); ????????hm1.put("吕布", "貂蝉"); ????????// 把元素添加到array里面 ????????array.add(hm1); ? ????????// 创建元素1 ????????HashMap<String, String> hm2 = new HashMap<String, String>(); ????????hm2.put("郭靖", "黄蓉"); ????????hm2.put("杨过", "小龙女"); ????????// 把元素添加到array里面 ????????array.add(hm2); ? ????????// 创建元素1 ????????HashMap<String, String> hm3 = new HashMap<String, String>(); ????????hm3.put("令狐冲", "任盈盈"); ????????hm3.put("林平之", "岳灵珊"); ????????// 把元素添加到array里面 ????????array.add(hm3); ? ????????// 遍历 ????????for (HashMap<String, String> hm : array) { ????????????Set<String> set = hm.keySet(); ????????????for (String key : set) { ????????????????String value = hm.get(key); ????????????????System.out.println(key + "---" + value); ????????????} ????????} ????} } |
?
????????????d:多层嵌套
????????????
import java.util.ArrayList; import java.util.HashMap; import java.util.Set; ? /* * 多层嵌套 * ????????:hashMap里面嵌套hashmap,hashMap里面在嵌套ArrayList * 为了更符合要求: * ????????这次的数据就看成是学生对象。 * * 传智播客 * ????????bj????北京校区 * ????????????jc????基础班 * ????????????????????林青霞????????27 * ????????????????????风清扬????????30 * ????????????jy????就业班???? * ????????????????????赵雅芝????????28 * ????????????????????武鑫????????29 * ????????sh????上海校区 * ????????????jc????基础班 * ????????????????????郭美美????????20 * ????????????????????犀利哥????????22 * ????????????jy????就业班???? * ????????????????????罗玉凤????????21 * ????????????????????马征????????23 * ????????gz????广州校区 * ????????????jc????基础班 * ????????????????????王力宏????????30 * ????????????????????李静磊????????32 * ????????????jy????就业班???? * ????????????????????郎朗????????31 * ????????????????????柳岩????????33 * ????????xa????西安校区 * ????????????jc????基础班 * ????????????????????范冰冰????????27 * ????????????????????刘意????????30 * ????????????jy????就业班???? * ????????????????????李冰冰????????28 * ????????????????????张志豪????????29 */ class HashMapDemo { ????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()); ????????????????} ????????????} ????????} ????} } |
?
2:Collections(理解)????
????(1)是针对集合进行操作的工具类
????(2)面试题:Collection和Collections的区别
????????A:Collection 是单列集合的顶层接口,有两个子接口List和Set
????????B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等
????(3)常见的几个小方法:
????????A:public static <T> void sort(List<T> list):排序,默认情况下是自然排序
????????B:public static <T> void sort(List<T> list, Comparator<? super T> c):按照比较器进行排序
???????????? 说明:如果同时有自然排序和比较器排序,则以比较器排序为主
?
????????C:public static <T> int binarySearch(List<?> list,T key):二分查找
????????D:public static <T> T max(Collection<?> coll):最大值
????????E:public static void reverse(List<?> list):反转
????????F:public static void shuffle(List<?> list):随机置换
F:方法功能演示:
????
import java.util.Collections; import java.util.List; import java.util.ArrayList; ? /* * Collections:是针对集合进行操作的工具类,都是静态方法。 * * 面试题: * Collection和Collections的区别? * Collection:是单列集合的顶层接口,有子接口List和Set。 * 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):随机置换 */ class CollectionsDemo { ????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:" + list); ? ????????// public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。 ????????// Collections.sort(list); ????????// System.out.println("list:" + list); ????????// [10, 20, 30, 40, 50] ? ????????// public static <T> int binarySearch(List<?> list,T key):二分查找 ????????// System.out ????????// .println("binarySearch:" + Collections.binarySearch(list, 30)); ????????// System.out.println("binarySearch:" ????????// + Collections.binarySearch(list, 300)); ? ????????// public static <T> T max(Collection<?> coll):最大值 ????????// System.out.println("max:"+Collections.max(list)); ? ????????// public static void reverse(List<?> list):反转 ????????// Collections.reverse(list); ????????// System.out.println("list:" + list); ???????? ????????//public static void shuffle(List<?> list):随机置换 ????????Collections.shuffle(list); ????????System.out.println("list:" + list); ????} } |
?
????(4)案例
????????A:ArrayList集合存储自定义对象的排序
????????????1:Student.java文件
/* * 对象自然排序: * ????????必须实现Comparable接口,重写里面的 compareTo方法 */ 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?int compareTo(Student s) { ????????int?num = this.age - s.age; ????????int?num2 = num == 0 ? this.name.compareTo(s.name) : num; ????????return?num2; ????} } |
?
?
????????????2:ConnectionsDemo.java文件
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; ? /* * Collections可以针对ArrayList存储基本包装类的元素排序,存储自定义对象可不可以排序呢? *如果同时有自然排序和比较器排序,以比较器排序为主 */ class CollectionsDemo { ????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); ????????Student s5 = new Student("林青霞", 27); ? ????????// 添加元素对象 ????????list.add(s1); ????????list.add(s2); ????????list.add(s3); ????????list.add(s4); ????????list.add(s5); ? ????????// 排序 ????????// 自然排序 ????????// 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()); ????????} ????} } |
?
????????B:模拟斗地主洗牌和发牌
????????????PokerDemo.java文件
import java.util.ArrayList; import java.util.Collections; ? /* * 模拟斗地主洗牌和发牌 * * 分析: * ????????A:创建一个牌盒 * ????????B:装牌 * ????????C:洗牌 * ????????D:发牌 * ????????E:看牌 */ class PokerDemo { ????public?static?void main(String[] args) { ????????// 创建一个牌盒 ????????ArrayList<String> array = new ArrayList<String>(); ? ????????// 装牌 ????????// 黑桃A,黑桃2,黑桃3,...黑桃K ????????// 红桃A,... ????????// 梅花A,... ????????// 方块A,... ????????// 定义一个花色数组 ????????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> fengQingYang = new ArrayList<String>(); ????????ArrayList<String> linQingXia = new ArrayList<String>(); ????????ArrayList<String> liuYi = 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) { ????????????????fengQingYang.add(array.get(x)); ????????????} else?if (x % 3 == 1) { ????????????????linQingXia.add(array.get(x)); ????????????} else?if (x % 3 == 2) { ????????????????liuYi.add(array.get(x)); ????????????} ????????} ? ????????// 看牌 ????????lookPoker("风清扬", fengQingYang); ????????lookPoker("林青霞", linQingXia); ????????lookPoker("刘意", liuYi); ? ????????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(); ????} } |
C:模拟斗地主洗牌和发牌并对牌进行排序的原理图解
????????D:模拟斗地主洗牌和发牌并对牌进行排序
?
????????????PokerDemo.java文件
import?java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; ? /* * 思路: * ????????A:创建一个HashMap集合 * ????????B:创建一个ArrayList集合 * ????????C:创建花色数组和点数数组 * ????????D:从0开始往HashMap里面存储编号,并存储对应的牌 * 同时往ArrayList里面存储编号即可。 * E:洗牌(洗的是编号) * F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收) * G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) */ class PokerDemo { ????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> fengQingYang = new TreeSet<Integer>(); ????????TreeSet<Integer> linQingXia = new TreeSet<Integer>(); ????????TreeSet<Integer> liuYi = 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) { ????????????????fengQingYang.add(array.get(x)); ????????????} else?if (x % 3 == 1) { ????????????????linQingXia.add(array.get(x)); ????????????} else?if (x % 3 == 2) { ????????????????liuYi.add(array.get(x)); ????????????} ????????} ? ????????// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) ????????lookPoker("风清扬", fengQingYang, hm); ????????lookPoker("林青霞", linQingXia, hm); ????????lookPoker("刘意", liuYi, 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(); ????} } |
?
3:集合大总结
1:集合(自己补齐) Collection(单列集合) ????List(有序,可重复) ????????ArrayList ????????????底层数据结构是数组,查询快,增删慢 ????????????线程不安全,效率高 ????????Vector ????????????底层数据结构是数组,查询快,增删慢 ????????????线程安全,效率低 ????????LinkedList ????????????底层数据结构是链表,查询慢,增删快 ????????????线程不安全,效率高 ????Set(无序,唯一) ????????HashSet ????????????底层数据结构是哈希表。 ????????????哈希表依赖两个方法:hashCode()和equals() ????????????执行顺序: ????????????????首先判断hashCode()值是否相同 ????????????????????是:继续执行equals(),看其返回值 ????????????????????????是true:说明元素重复,不添加 ????????????????????????是false:就直接添加到集合 ????????????????????否:就直接添加到集合 ????????????最终: ????????????????自动生成hashCode()和equals()即可 ???????????????? ????????????LinkedHashSet ????????????????底层数据结构由链表和哈希表组成。 ????????????????由链表保证元素有序。 ????????????????由哈希表保证元素唯一。 ????????TreeSet ????????????底层数据结构是红黑树。(是一种自平衡的二叉树) ????????????如何保证元素唯一性呢? ????????????????根据比较的返回值是否是0来决定 ????????????如何保证元素的排序呢? ????????????????两种方式 ????????????????????自然排序(元素具备比较性) ????????????????????????让元素所属的类实现Comparable接口 ????????????????????比较器排序(集合具备比较性) ????????????????????????让集合接收一个Comparator的实现类对象 Map(双列集合) ????A:Map集合的数据结构仅仅针对键有效,与值无关。 ????B:存储的是键值对形式的元素,键唯一,值可重复。 ???? ????HashMap ????????底层数据结构是哈希表。线程不安全,效率高 ????????????哈希表依赖两个方法:hashCode()和equals() ????????????执行顺序: ????????????????首先判断hashCode()值是否相同 ????????????????????是:继续执行equals(),看其返回值 ????????????????????????是true:说明元素重复,不添加 ????????????????????????是false:就直接添加到集合 ????????????????????否:就直接添加到集合 ????????????最终: ????????????????自动生成hashCode()和equals()即可 ????????LinkedHashMap ????????????底层数据结构由链表和哈希表组成。 ????????????????由链表保证元素有序。 ????????????????由哈希表保证元素唯一。 ????Hashtable ????????底层数据结构是哈希表。线程安全,效率低 ????????????哈希表依赖两个方法:hashCode()和equals() ????????????执行顺序: ????????????????首先判断hashCode()值是否相同 ????????????????????是:继续执行equals(),看其返回值 ????????????????????????是true:说明元素重复,不添加 ????????????????????????是false:就直接添加到集合 ????????????????????否:就直接添加到集合 ????????????最终: ????????????????自动生成hashCode()和equals()即可 ????TreeMap ????????底层数据结构是红黑树。(是一种自平衡的二叉树) ????????????如何保证元素唯一性呢? ????????????????根据比较的返回值是否是0来决定 ????????????如何保证元素的排序呢? ????????????????两种方式 ????????????????????自然排序(元素具备比较性) ????????????????????????让元素所属的类实现Comparable接口 ????????????????????比较器排序(集合具备比较性) ????????????????????????让集合接收一个Comparator的实现类对象 ? 2:到底使用那种集合(自己补齐) 看需求。 ? 是否是键值对象形式: ????是:Map ????????键是否需要排序: ????????????是:TreeMap ????????????否:HashMap ????????不知道,就使用HashMap。 ???????? ????否:Collection ????????元素是否唯一: ????????????是:Set ????????????????元素是否需要排序: ????????????????????是:TreeSet ????????????????????否:HashSet ????????????????不知道,就使用HashSet ???????????????? ????????????否:List ????????????????要安全吗: ????????????????????是:Vector(其实我们也不用它,后面我们讲解了多线程以后,我在给你回顾用谁) ????????????????????否:ArrayList或者LinkedList ????????????????????????增删多:LinkedList ????????????????????????查询多:ArrayList ????????????????????不知道,就使用ArrayList ????????不知道,就使用ArrayList ???????? 3:集合的常见方法及遍历方式 Collection: ????add() ????remove() ????contains() ????iterator() ????size() ???? ????遍历: ????????增强for ????????迭代器 ???????? ????|--List ????????get() ???????? ????????遍历: ????????????普通for ????|--Set ? Map: ????put() ????remove() ????containskey(),containsValue() ????keySet() ????get() ????value() ????entrySet() ????size() ???? ????遍历: ????????根据键找值 ????????根据键值对对象分别找键和值 ???????? 作业: ????我讲解过的任意一个集合,我要求你存储什么,你就能够存储什么。 ????并且,还要能够遍历出来。 ? 4:ArrayList,LinkedList,HashSet,HashMap(掌握) 存储字符串和自定义对象数据并遍历 ? 5:集合的嵌套遍历(理解) |
?
?
以上是关于javaSE第十八天的主要内容,如果未能解决你的问题,请参考以下文章