本帖最后由 小石姐姐 于 2019-5-9 18:31 编辑
个人笔记一、Map集合概述和特点
1.键值对映射关系
2.一个键对应一个值
3.键不能重复
4.元素存取无序
二、Map集合的基本功能
Map集合的常用方法V put(K k,V v) 向集合中添加一对元素(键值对) V remove(K k) 根据键删除键值对元素 void clear() 清空集合 boolean containsKey(K k) 判断集合中是否包含指定的键 boolean containsValue(V v) 判断集合中是否包含指定的值 boolean isEmpty() 判断集合是否为空 int size() 获取集合的长度 三、Map集合的获取功能
Map集合常用的获取方法V get(K k) 根据键获取值 Set<K> keyset() 获取所有的键,保存到Set集合中(不可重复) Collection<V> values() 获取所有的值,保存到Collection集合中(可重复) Set<Map.Entry<K,V>> entrySet() 获取所有键值对,封装成Entry对象,entry {key,value} 四、Map集合的遍历(方式1)
[Java] 纯文本查看 复制代码 package com.itheima01;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapTest02 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("张无忌", "赵敏");
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");
//遍历map集合:第一种方式:keySet()和get()方法结合
Set<String> keys = map.keySet();
for (String key : keys) {
String value = map.get(key);
System.out.println(key + ", " + value);
}
}
}
五、Map集合的遍历(方式2)
[HTML] 纯文本查看 复制代码 package com.itheima01;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapTest02 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("张无忌", "赵敏");
map.put("郭靖", "黄蓉");
map.put("杨过", "小龙女");
//遍历map集合:第二种方式:entrySet();
Set<Map.Entry<String, String>> entrys = map.entrySet();
for (Map.Entry<String, String> entry : entrys) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ", " + value);
}
//System.out.println(map);
}
}
六、HashMap集合练习之键是String值是Student
[Java] 纯文本查看 复制代码 package com.itheima02;
//学生类
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
//HashMap集合练习之键是String值是Student
package com.itheima02;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
HashMap<String,Student> hm = new HashMap<String,Student>();
Student s1 = new Student("张三",21);
Student s2 = new Student("李四",22);
Student s3 = new Student("王五",23);
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
Set<String> keys = hm.keySet();
for (String key : keys){
Student value = hm.get(key);
System.out.println(key+": "+value);
}
Set<Map.Entry<String, Student>> entrys = hm.entrySet();
for (Map.Entry<String,Student> entry : entrys){
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key + ": " + value);
}
}
}
七、HashMap集合练习之键是Student值是String
[Java] 纯文本查看 复制代码 package com.itheima02;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashSetTest1 {
public static void main(String[] args) {
HashMap<Student,String> hs = new HashMap<>();
Student s1 = new Student("张三",21);
Student s2 = new Student("李四",22);
Student s3 = new Student("王五",23);
Student s4 = new Student("王五",23);
hs.put(s1,"西安");
hs.put(s2,"北京");
hs.put(s3,"上海");
hs.put(s4,"上海");
Set<Student> keys = hs.keySet();
for (Student key : keys){
String value = hs.get(key);
System.out.println(key.getName()+","+key.getAge()+","+value);
}
System.out.println("========");
Set<Map.Entry<Student, String>> entrys = hs.entrySet();
for (Map.Entry<Student,String> entry : entrys){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName()+","+key.getAge()+","+value);
}
}
}
八、集合嵌套之ArrayList嵌套HashMap
[Java] 纯文本查看 复制代码 package com.itheima03;
/*
ArrayList集合嵌套HashMap集合
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class ListMapTest {
public static void main(String[] args) {
//创建ArrayList集合,元素类型是:HashMap<String,String>
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
//创建HashMap集合
HashMap<String, String> hm1 = new HashMap<String, String>();
//添加HashMap集合元素
hm1.put("张三", "李四");
hm1.put("王五", "赵六");
//添加ArrayList集合元素
array.add(hm1);
//创建HashMap集合
HashMap<String, String> hm2 = new HashMap<String, String>();
//添加HashMap集合元素
hm2.put("张三1", "李四1");
hm2.put("王五1", "赵六1");
//添加ArrayList集合元素
array.add(hm2);
//创建HashMap集合
HashMap<String, String> hm3 = new HashMap<String, String>();
//添加HashMap集合元素
hm3.put("张三2", "李四2");
hm3.put("王五2", "赵六2");
//添加ArrayList集合元素
array.add(hm3);
//遍历ArrayList集合
for (HashMap<String, String> hm : array) {
//遍历HashMap集合
Set<String> keys = hm.keySet();
for (String key : keys) {
String value = hm.get(key);
System.out.println(key + ", " + value);
}
}
}
}
九、集合嵌套之HashMap嵌套ArrayList
[Java] 纯文本查看 复制代码 package com.itheima03;
/*
HashMap集合嵌套ArrayList集合
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class MapListTest {
public static void main(String[] args) {
//创建HashMap集合,元素类型:String,ArrayList<String>
HashMap<String,ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
//创建ArrayList<String>集合
ArrayList<String> sgyy = new ArrayList<String>();
//添加ArrayList集合元素
sgyy.add("诸葛亮");
sgyy.add("周瑜");
//添加HashMap集合元素
hm.put("三国演义",sgyy);
//创建ArrayList<String>集合
ArrayList<String> xyj = new ArrayList<String>();
//添加ArrayList集合元素
xyj.add("唐三藏");
xyj.add("孙悟空");
//添加HashMap集合元素
hm.put("西游记",xyj);
//创建ArrayList<String>集合
ArrayList<String> shz = new ArrayList<String>();
//添加ArrayList集合元素
shz.add("鲁智深");
shz.add("武松");
//添加HashMap集合元素
hm.put("水浒传",shz);
//遍历HashMap集合
Set<String> keys = hm.keySet();
for (String key : keys){
//遍历ArrayList集合
System.out.println(key+":");
ArrayList<String> value = hm.get(key);
for (String s : value){
System.out.print("\t"+s);
}
System.out.println();
}
}
}
十、统计字符串中每个字符出现的次数
[Java] 纯文本查看 复制代码 package com.itheima04;
/*
统计字符串中每个字符出现的次数
*/
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class StringHashMapTest {
public static void main(String[] args) {
//键盘录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
//创建HashMap集合,键和值得类型:<Character,Integer>
// HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
TreeMap<Character,Integer> hm = new TreeMap<Character,Integer>();
//遍历字符串,得到每一个字符key
for (int i = 0; i < s.length(); i++) {
char key = s.charAt(i);
//用key当做Map的键,在Map集合里找相应的值如果值value,
Integer value = hm.get(key);
//如果值value是空,就添加这个键和值1,如果有,就重新添加这个键,并且让值value++,再添加值value。
if (value == null){
hm.put(key,1);
}else {
value++;
hm.put(key,value);
}
}
//创建StringBuilder,用来添加Map集合中的键和值
StringBuilder sb = new StringBuilder();
//遍历Map集合
Set<Character> keys = hm.keySet();
for (Character key : keys){
Integer value = hm.get(key);
sb.append(key).append(": ").append(value).append("次\r\n");
}
//tostring(),把StringBuilder类型转成String类型
String ss = sb.toString();
System.out.println(ss);
}
}
十一、Collections概述和使用Collections集合工具类常用方法 void sort() 对集合元素排序 void reverse() 对集合元素反转 void shuffle() 对集合元素随机置换
[Java] 纯文本查看 复制代码 package com.itheima05;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsTest1 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("ccc");
list.add("bbb");
//Collections的sort()方法,自然排序
//Collections.sort(list);
//Collections的reverse()方法,反转
//Collections.reverse(list);
//Collections的shuffle()方法,随机排序
Collections.shuffle(list);
for (String s : list){
System.out.println(s);
}
List<Integer> list1 = new ArrayList<Integer>();
list1.add(11);
list1.add(33);
list1.add(22);
Collections.sort(list1);
for (Integer i : list1){
System.out.println(i);
}
}
}
十二、ArrayList集合存储学生并排序
[Java] 纯文本查看 复制代码 //学生类
package com.itheima05;
/*public class Student implements Comparable<Student> {*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
/*@Override
public int compareTo(Student s) {
int num1 = this.age - s.age;
int num2 = num1 == 0 ? this.name.compareTo(s.name) : num1;
return num2;
}*/
}
//ArrayList集合存储学生并排序
package com.itheima05;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class CollectionsTest {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<>();
Student s1 = new Student("张三", 21);
Student s4 = new Student("李四", 22);
Student s3 = new Student("王五", 23);
Student s2 = new Student("赵六", 23);
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//调用Collections.sort(List list,比较器排序Comparator)方法,
Collections.sort(array, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num1 = s1.getAge() - s2.getAge();
int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1;
return num2;
}
});
for (Student s : array) {
System.out.println(s);
}
}
}
十三、模拟斗地主
[Java] 纯文本查看 复制代码 package com.itheima06;
/*
模拟斗地主案例的主要步骤
1.定义一个数组,用于保存牌的花色
2.定义一个数组,用于保存牌的数字
3.创建ArrayList集合对象,将花色和数字进行拼接后添加到集合中
4.调用Collections工具类中的shuffle()方法洗牌
5.创建三个玩家集合一个底牌集合,进行发牌动作
6.遍历玩家和底牌的集合,查看内容
*/
import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
String[] colors = {"♦", "♣", "♠", "♥"};
String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A"};
for (String color : colors) {
for (String number : numbers) {
array.add(color + number);
}
}
array.add("小王");
array.add("大王");
Collections.shuffle(array);
ArrayList<String> dzArray = new ArrayList<>();
ArrayList<String> nmArray1 = new ArrayList<>();
ArrayList<String> nmArray2 = new ArrayList<>();
ArrayList<String> dpArray = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
String poker = array.get(i);
if (i >= array.size() - 3) {
dpArray.add(poker);
} else if (i % 3 == 0) {
dzArray.add(poker);
} else if (i % 3 == 1) {
nmArray1.add(poker);
} else if (i % 3 == 2) {
nmArray2.add(poker);
}
}
lookPoker("地主",dzArray);
lookPoker("农民1",nmArray1);
lookPoker("农民2",nmArray2);
lookPoker("底牌",dpArray);
//System.out.println(array);
}
public static void lookPoker(String name, ArrayList<String> array) {
System.out.print(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}
十四、模拟斗地主升级版案例分析斗地主案例升级版案例的主要步骤 1.创建HashMap集合,键为编号,值为牌 2.创建ArrayList集合,存储编号 3.创建花色和点数的数组 4.将编号和花色存储到map集合中,将编号单独存储list集合中 5.使用Collections中的shuffle()方法将list集合进行随机置换 6.创建玩家和底牌的TreeSet集合,并进行发牌 7.遍历集合,查看内容 十五、模拟斗地主升级版代码实现
[Java] 纯文本查看 复制代码 package com.itheima06;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
斗地主案例升级版案例的主要步骤
1.创建HashMap集合,键为编号,值为牌
2.创建ArrayList集合,存储编号
3.创建花色和点数的数组
4.将编号和花色存储到map集合中,将编号单独存储list集合中
5.使用Collections中的shuffle()方法将list集合进行随机置换
6.创建玩家和底牌的TreeSet集合,并进行发牌
7.遍历集合,查看内容
*/
public class DouDiZhuBig {
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"};
//将编号和花色存储到map集合中,将编号单独存储list集合中
int index = 0;
for (String number : numbers) {
for (String color : colors) {
hm.put(index, color + number);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
//使用Collections中的shuffle()方法将list集合进行随机置换
Collections.shuffle(array);
//创建玩家和底牌的TreeSet集合,并进行发牌
TreeSet<Integer> dzSet = new TreeSet<Integer>();
TreeSet<Integer> nmSet1 = new TreeSet<Integer>();
TreeSet<Integer> nmSet2 = new TreeSet<Integer>();
TreeSet<Integer> dpSet = new TreeSet<Integer>();
for (int i = 0; i < array.size(); i++) {
Integer num = array.get(i);
if (i >= array.size() - 3) {
dpSet.add(num);
} else if (i % 3 == 0) {
dzSet.add(num);
} else if (i % 3 == 1) {
nmSet1.add(num);
} else if (i % 3 == 2) {
nmSet2.add(num);
}
}
lookPoker("地主", dzSet, hm);
lookPoker("农民1", nmSet1, hm);
lookPoker("农民2", nmSet2, hm);
lookPoker("地主", dpSet, 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();
}
}
|