一:Map(掌握)
(1)Map
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
(2)Map和Collection的区别?
A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍
(3)Map接口功能概述
A:添加功能: V put(K key,V value)
B:删除功能:
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并返回值
C:判断功能:
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
D:获取功能:
V get(Object key):根据键获取值
Set<K> KeySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值得集合
Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合
E:长度功能:int size():返回集合中键值对的对数
(4)Map集合的遍历
方法一:键找值
a:获取所有键的集合
b:遍历键的集合,得到每一个键
c:根据键到集合中找值
代码体现
public class MapDemo {
public static void main(String[] args) {
Map<String,String> hm = new HashMap<String,String>();
hm.put("001","hello");
hm.put("002","world");
hm.put("003","java");
// 获取所有键的集合
Set<String> set = hm.keySet();
// 遍历键的集合
for(String key : set) {
// 根据键到集合中找值
String value = hm.get(key);
System.out.println(key+"-----"+value);
}
}
}
方法二:键值对对象找键和值
a:获取所有键值对对象的集合
b:遍历键值对对象的集合,获取每一个键值对对象
c:根据键到集合中找值
代码体现
public class MapDemo {
public static void main(String[] args) {
Map<String,String> hm = new HashMap<String,String>();
hm.put("001","hello");
hm.put("002","world");
hm.put("003","java");
// 获取所有键值对对象的集合
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<String,String>
import java.util.HashMap;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, String> hm = new HashMap<String,String>();
// 添加元素
hm.put("001", "西施");
hm.put("002", "貂蝉");
hm.put("003", "王昭君");
hm.put("004", "杨玉环");
hm.put("001", "后裔");
// 遍历
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
*/
public class HashMapTest2 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer,String>();
hm.put(30, "马云");
hm.put(50, "马化腾");
hm.put(60, "王健林");
hm.put(40, "周星驰");
hm.put(20, "吴亦凡");
Set<Integer> set = hm.keySet();
for(Integer key : set) {
String value = hm.get(key);
System.out.println(key+"-----"+value);
}
}
}
C:HashMap<String,Student>
import java.util.HashMap;
import java.util.Set;
/*
* HashMap<String,Student>
* 键:String 学号
* 值:Student 学生对象
*/
public class HashMapTest3 {
public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<String,Student>();
Student s1 = new Student("百里守约",27);
Student s2 = new Student("百里玄策",29);
Student s3 = new Student("后裔",30);
Student s4 = new Student("嫦娥",18);
hm.put("T001", s1);
hm.put("T002", s2);
hm.put("T003", s3);
hm.put("T004", s4);
Set<String> set = hm.keySet();
for(String key : set) {
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
*/
public class HashMapTest4 {
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<Student, String>();
Student s1 = new Student("刘备",45);
Student s2 = new Student("关羽",40);
Student s3 = new Student("张飞",38);
Student s4 = new Student("赵云",25);
Student s5 = new Student("刘备",45);
hm.put(s1,"6666");
hm.put(s2,"7777");
hm.put(s3,"8888");
hm.put(s4,"9999");
hm.put(s5,"5555");
Set<Student> set = hm.keySet();
for(Student key : set) {
String value = hm.get(key);
System.out.println(key.getName()+"---"+key.getAge()+"---"+value);
}
}
}
(6)TreeMap集合的练习
A:TreeMap<String,String>
import java.util.Set;
import java.util.TreeMap;
/*
* TreeMap:是基于红黑树的Map接口的实现。
*
* HashMap<String,String>
* 键:String
* 值:String
*/
public 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>
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
/*
* TreeMap<Student,String>
* 键:Student
* 值:String
*/
public class TreeMapTest {
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);
Student s6 = new Student("啊三", 30);
tm.put(s1, "宋朝");
tm.put(s2, "元朝");
tm.put(s3, "明朝");
tm.put(s4, "清朝");
tm.put(s5, "汉朝");
tm.put(s6, "秦国");
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)
*/
public 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
*
* 先存储元素,然后遍历元素
*/
public 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集合的值是字符串。
*元素我已经完成,请遍历。
*结果:
* 三国演义
* 吕布
* 周瑜
* 笑傲江湖
* 令狐冲
* 林平之
* 神雕侠侣
* 郭靖
* 杨过
*/
public 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);
}
}
}
}
二: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> int binarySearch(List<?> list,T key)
C:public static <T> T max(Collection<?> coll)
D:public static void reverse(List<?> list)
E:public static void shuffle(List<?> list)
(4)练习:模拟斗地主洗牌和发牌并对牌进行排序
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集合找对应的牌)
*/
public 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();
}
}