JAVA Map集合
目录[ Hide ]
Map集合的功能概述
package com.heima.map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class Demo1_Map {
public static void main(String[] args) {
// demo1();
// demo2();
// demo3();
// demo4();
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
Collection<Integer> c = map.values();//获取集合中所有值的集合
System.out.println(c);
System.out.println(map.size());
}
private static void demo4() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
System.out.println(map.containsKey("张三"));//判断是否包含传入的键
System.out.println(map.containsValue(100));//判断是否包含传入的值
}
private static void demo3() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
Integer i1 = map.remove("张三");//根据键删除元素,返回键对应的值
System.out.println(i1);
System.out.println(map);
}
private static void demo2() {
Map<String, Integer> map = new HashMap<>();
Integer i1 = map.put("张三", 23);
Integer i2 = map.put("李四", 23);
Integer i3 = map.put("王五", 23);
Integer i4 = map.put("赵六", 23);
Integer i5 = map.put("张三", 26);//相同的键不存储,值覆盖,把被覆盖的值返回
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
System.out.println(map);
}
private static void demo1() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
System.out.println(map);
}
}
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class Demo1_Map {
public static void main(String[] args) {
// demo1();
// demo2();
// demo3();
// demo4();
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
Collection<Integer> c = map.values();//获取集合中所有值的集合
System.out.println(c);
System.out.println(map.size());
}
private static void demo4() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
System.out.println(map.containsKey("张三"));//判断是否包含传入的键
System.out.println(map.containsValue(100));//判断是否包含传入的值
}
private static void demo3() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
Integer i1 = map.remove("张三");//根据键删除元素,返回键对应的值
System.out.println(i1);
System.out.println(map);
}
private static void demo2() {
Map<String, Integer> map = new HashMap<>();
Integer i1 = map.put("张三", 23);
Integer i2 = map.put("李四", 23);
Integer i3 = map.put("王五", 23);
Integer i4 = map.put("赵六", 23);
Integer i5 = map.put("张三", 26);//相同的键不存储,值覆盖,把被覆盖的值返回
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
System.out.println(map);
}
private static void demo1() {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 23);
map.put("王五", 23);
map.put("赵六", 23);
System.out.println(map);
}
}
Map集合的遍历之键找值
package com.heima.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo2_Iterator {
/*
* 通过查看map集合的api 发现没有Iterator方法,那么双列集合如何迭代
* */
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
/* Integer i = map.get("张三"); //get()方法根据键获取值
System.out.println(i);
//获取所有的键
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
Integer value = map.get(key);
System.out.println(key+"="+value);
}*/
//使用增强for循环
for (String key : map.keySet()) { //map.keySet是所有键的集合
System.out.println(key+"="+map.get(key));
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo2_Iterator {
/*
* 通过查看map集合的api 发现没有Iterator方法,那么双列集合如何迭代
* */
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
/* Integer i = map.get("张三"); //get()方法根据键获取值
System.out.println(i);
//获取所有的键
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
Integer value = map.get(key);
System.out.println(key+"="+value);
}*/
//使用增强for循环
for (String key : map.keySet()) { //map.keySet是所有键的集合
System.out.println(key+"="+map.get(key));
}
}
}
Map集合的遍历之键值对对象找键和值
package com.heima.map;
import java.beans.IntrospectionException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo3_Iterator {
//map集合中的第二种迭代,根据键值对对象,获取键和值
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
/*//map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//获取每一个对象
Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
while(it.hasNext()){
//获取每一个entry对象
Map.Entry<String, Integer> en = it.next();
String key = en.getKey();
Integer value = en.getValue();//根据键值对对象获取键
System.out.println(key+"="+ value);//根据键值对对象获取值
}*/
for (Map.Entry<String, Integer> en : map.entrySet()) {
System.out.println(en.getKey()+"="+en.getValue());
}
}
}
import java.beans.IntrospectionException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo3_Iterator {
//map集合中的第二种迭代,根据键值对对象,获取键和值
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("赵六", 26);
/*//map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//获取每一个对象
Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
while(it.hasNext()){
//获取每一个entry对象
Map.Entry<String, Integer> en = it.next();
String key = en.getKey();
Integer value = en.getValue();//根据键值对对象获取键
System.out.println(key+"="+ value);//根据键值对对象获取值
}*/
for (Map.Entry<String, Integer> en : map.entrySet()) {
System.out.println(en.getKey()+"="+en.getValue());
}
}
}
Map集合的遍历之键值对对象找键和值源码分析
package com.heima.map;
import java.util.HashMap;
import com.heima.bean.Student;
public class Demo5_HashMap {
//HashMap集合是Student值是String的案例
//键是学生对象,代表每一个学生
//值是字符串对象,代表学生归属地
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("张三", 23), "北京");
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("李四", 24), "广州");
hm.put(new Student("王五", 25), "深圳");
System.out.println(hm);
}
}
import java.util.HashMap;
import com.heima.bean.Student;
public class Demo5_HashMap {
//HashMap集合是Student值是String的案例
//键是学生对象,代表每一个学生
//值是字符串对象,代表学生归属地
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("张三", 23), "北京");
hm.put(new Student("张三", 23), "上海");
hm.put(new Student("李四", 24), "广州");
hm.put(new Student("王五", 25), "深圳");
System.out.println(hm);
}
}
package com.heima.bean;
public class Student {
private String name;
private int age;
public Student() {
super();
<pre><code>}
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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", 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;
}</code></pre>
}
public class Student {
private String name;
private int age;
public Student() {
super();
<pre><code>}
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;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", 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;
}</code></pre>
}
LinkedHashMap的概述和使用
TreeMap集合键是Student值是String的案例
import java.util.Comparator;
import java.util.TreeMap;
import com.heima.bean.Student;
public class Demo7_TreeMap {
//TreeMap集合键是Student值是String的案例
public static void main(String[] args) {
// demo1();
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
return num == 0?s1.getAge() - s2.getAge():num;
}
});
tm.put(new Student("张三", 12), "北京");
tm.put(new Student("李四", 24), "广州");
tm.put(new Student("王五", 25), "深圳");
System.out.println(tm);
}
private static void demo1() {
TreeMap<Student, String> tm = new TreeMap<>();
tm.put(new Student("张三", 12), "北京");
tm.put(new Student("李四", 24), "广州");
tm.put(new Student("王五", 25), "深圳");
System.out.println(tm);
}
}
import java.util.TreeMap;
import com.heima.bean.Student;
public class Demo7_TreeMap {
//TreeMap集合键是Student值是String的案例
public static void main(String[] args) {
// demo1();
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
return num == 0?s1.getAge() - s2.getAge():num;
}
});
tm.put(new Student("张三", 12), "北京");
tm.put(new Student("李四", 24), "广州");
tm.put(new Student("王五", 25), "深圳");
System.out.println(tm);
}
private static void demo1() {
TreeMap<Student, String> tm = new TreeMap<>();
tm.put(new Student("张三", 12), "北京");
tm.put(new Student("李四", 24), "广州");
tm.put(new Student("王五", 25), "深圳");
System.out.println(tm);
}
}
统计字符串中每个字符出现的次数
package com.heima.test;
import java.util.HashMap;
public class Test1 {
//统计字符串中每个字符出现的次数
/*
* 分析
* 1.首先定义一个需要被统计字符的字符串
* 2.将字符串转换为字符数组
* 3.定义双列集合,存储字符串中字符以及字符出现的次数
* 4.遍历字符数组获取每一个字符,并将字符存储在双列集合中
* 5.存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
* 6.打印双列集合,获取字符出现的次数
* */
public static void main(String[] args) {
//1.首先定义一个需要被统计字符的字符串
String s = "aaaabbbbccccccccc";
//2.将字符串转换为字符数组
char[] arr = s.toCharArray();
//3.定义双列集合,存储字符串中字符以及字符出现的次数
HashMap<Character, Integer> hm = new HashMap<>();
//4.遍历字符数组获取每一个字符,并将字符存储在双列集合中
for(char c : arr){
//5.存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
if(!hm.containsKey(c)){
hm.put(c, 1);
}else{
//如果 不包含这个键
hm.put(c, hm.get(c)+1);
}
}
//6.打印双列集合,获取字符出现的次数
for (Character key : hm.keySet()) { //hm.keySet()代表所有键的集合
System.out.println(key + "=" + hm.get(key));//h.get(key)代表根据键获取值
}
}
}
import java.util.HashMap;
public class Test1 {
//统计字符串中每个字符出现的次数
/*
* 分析
* 1.首先定义一个需要被统计字符的字符串
* 2.将字符串转换为字符数组
* 3.定义双列集合,存储字符串中字符以及字符出现的次数
* 4.遍历字符数组获取每一个字符,并将字符存储在双列集合中
* 5.存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
* 6.打印双列集合,获取字符出现的次数
* */
public static void main(String[] args) {
//1.首先定义一个需要被统计字符的字符串
String s = "aaaabbbbccccccccc";
//2.将字符串转换为字符数组
char[] arr = s.toCharArray();
//3.定义双列集合,存储字符串中字符以及字符出现的次数
HashMap<Character, Integer> hm = new HashMap<>();
//4.遍历字符数组获取每一个字符,并将字符存储在双列集合中
for(char c : arr){
//5.存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储
if(!hm.containsKey(c)){
hm.put(c, 1);
}else{
//如果 不包含这个键
hm.put(c, hm.get(c)+1);
}
}
//6.打印双列集合,获取字符出现的次数
for (Character key : hm.keySet()) { //hm.keySet()代表所有键的集合
System.out.println(key + "=" + hm.get(key));//h.get(key)代表根据键获取值
}
}
}
集合嵌套之HashMap嵌套HashMap
import java.util.HashMap;
import com.heima.bean.Student;
public class Demo8_HashMapHashMap {
//集合嵌套之HashMap嵌套HashMap
public static void main(String[] args) {
//定义88期基础班
HashMap<Student, String> hm88 = new HashMap<>();
hm88.put(new Student("张三", 23), "北京");
hm88.put(new Student("李四", 23), "北京");
hm88.put(new Student("王五", 23), "上海");
hm88.put(new Student("赵六", 23), "北京");
//定义99期基础班
HashMap<Student, String> hm99 = new HashMap<>();
hm99.put(new Student("孙悟空", 23), "北京");
hm99.put(new Student("贝吉塔", 23), "北京");
hm99.put(new Student("孙悟饭", 23), "上海");
hm99.put(new Student("弗利萨", 23), "北京");
//定义双元课堂
HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
hm.put(hm88, "第88期基础班");
hm.put(hm99, "第99期基础班");
//遍历双列集合
for (HashMap<Student, String> h : hm.keySet()) {//hm.keySet()代表的是双列集合中键的集合
String value = hm.get(h);//hm.get(h)根据键对象获取值对象
for (Student key : h.keySet()) { //h.keySet()是获取集合中所有的学生键对象
String value2 = h.get(key);
System.out.println(key + "=" +value2 +"=" + value);
}
}
}
}
import com.heima.bean.Student;
public class Demo8_HashMapHashMap {
//集合嵌套之HashMap嵌套HashMap
public static void main(String[] args) {
//定义88期基础班
HashMap<Student, String> hm88 = new HashMap<>();
hm88.put(new Student("张三", 23), "北京");
hm88.put(new Student("李四", 23), "北京");
hm88.put(new Student("王五", 23), "上海");
hm88.put(new Student("赵六", 23), "北京");
//定义99期基础班
HashMap<Student, String> hm99 = new HashMap<>();
hm99.put(new Student("孙悟空", 23), "北京");
hm99.put(new Student("贝吉塔", 23), "北京");
hm99.put(new Student("孙悟饭", 23), "上海");
hm99.put(new Student("弗利萨", 23), "北京");
//定义双元课堂
HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
hm.put(hm88, "第88期基础班");
hm.put(hm99, "第99期基础班");
//遍历双列集合
for (HashMap<Student, String> h : hm.keySet()) {//hm.keySet()代表的是双列集合中键的集合
String value = hm.get(h);//hm.get(h)根据键对象获取值对象
for (Student key : h.keySet()) { //h.keySet()是获取集合中所有的学生键对象
String value2 = h.get(key);
System.out.println(key + "=" +value2 +"=" + value);
}
}
}
}
HashMap和Hashtable的区别
Collections工具类的概述和常见方法讲解
package com.heima.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class Demo1_Collections {
public static void main(String[] args) {
// demo1();
// demo2();
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("f");
list.add("g");
System.out.println(Collections.max(list)); //根据默认排序结果获取集合中的最大值
System.out.println(Collections.min(list)); //最小
// Collections.reverse(list);//反转集合
Collections.shuffle(list);//随机置换,可以用来洗牌
System.out.println(list);
}
private static void demo2() {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("f");
list.add("g");
System.out.println(Collections.binarySearch(list, "c"));
System.out.println(Collections.binarySearch(list, "b"));
}
private static void demo1() {
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("a");
list.add("a");
list.add("b");
list.add("d");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class Demo1_Collections {
public static void main(String[] args) {
// demo1();
// demo2();
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("f");
list.add("g");
System.out.println(Collections.max(list)); //根据默认排序结果获取集合中的最大值
System.out.println(Collections.min(list)); //最小
// Collections.reverse(list);//反转集合
Collections.shuffle(list);//随机置换,可以用来洗牌
System.out.println(list);
}
private static void demo2() {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("d");
list.add("f");
list.add("g");
System.out.println(Collections.binarySearch(list, "c"));
System.out.println(Collections.binarySearch(list, "b"));
}
private static void demo1() {
ArrayList<String> list = new ArrayList<>();
list.add("c");
list.add("a");
list.add("a");
list.add("b");
list.add("d");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
模拟斗地主洗牌和发牌
package com.heima.test;
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
/*模拟斗地主洗牌和发牌
*
* 分析:
* 1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
* 2,洗牌
* 3,发牌
* 4,看牌
* */
public static void main(String[] args) {
//1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K",};
String[] color = {"红桃","黑桃","方块","梅花"};
ArrayList<String> poker = new ArrayList<>();
//拼接花色和数字
for(String s1 : color){
for(String s2 : num){
poker.add(s1.concat(s2)); //concat1连接两个字符串
}
}
// System.out.println(poker);
poker.add("小王");
poker.add("大王");
//2.洗牌
Collections.shuffle(poker);
//3.发牌
ArrayList<String> gaojin = new ArrayList<>();
ArrayList<String> longwu = new ArrayList<>();
ArrayList<String> me = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if(i>=poker.size() -3 ){
dipai.add(poker.get(i)); //将三张底牌存储在底牌集合
}else if(i % 3 == 0){
gaojin.add(poker.get(i));
}else if(i % 3 == 1){
longwu.add(poker.get(i));
}else{
me.add(poker.get(i));
}
}
//看牌
System.out.println(gaojin);
System.out.println(longwu);
System.out.println(me);
System.out.println(dipai);
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
/*模拟斗地主洗牌和发牌
*
* 分析:
* 1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
* 2,洗牌
* 3,发牌
* 4,看牌
* */
public static void main(String[] args) {
//1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K",};
String[] color = {"红桃","黑桃","方块","梅花"};
ArrayList<String> poker = new ArrayList<>();
//拼接花色和数字
for(String s1 : color){
for(String s2 : num){
poker.add(s1.concat(s2)); //concat1连接两个字符串
}
}
// System.out.println(poker);
poker.add("小王");
poker.add("大王");
//2.洗牌
Collections.shuffle(poker);
//3.发牌
ArrayList<String> gaojin = new ArrayList<>();
ArrayList<String> longwu = new ArrayList<>();
ArrayList<String> me = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if(i>=poker.size() -3 ){
dipai.add(poker.get(i)); //将三张底牌存储在底牌集合
}else if(i % 3 == 0){
gaojin.add(poker.get(i));
}else if(i % 3 == 1){
longwu.add(poker.get(i));
}else{
me.add(poker.get(i));
}
}
//看牌
System.out.println(gaojin);
System.out.println(longwu);
System.out.println(me);
System.out.println(dipai);
}
}
模拟斗地主洗牌和发牌并对牌进行排序的代码实现
package com.heima.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
import javax.swing.LookAndFeel;
public class Test3 {
/*
* 分析:
* 1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
* 2,洗牌
* 3,发牌
* 4,看牌
* */
public static void main(String[] args) {
//1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"红桃","黑桃","方块","梅花"};
HashMap<Integer, String> hm = new HashMap<>();//存储索引和扑克牌
ArrayList<Integer> list = new ArrayList<>();//存储索引
int index = 0;
//拼接扑克牌,并将索引和扑克牌存储在HashMap中
for(String s1 : num){ //获取数字
for(String s2 : color){//获取颜色
hm.put(index, s2.concat(s1));
list.add(index); //将0-51索引添加到集合
index++;
}
}
//将小王添加到双列集合中
hm.put(index, "小王");
list.add(index); //将52索引添加到集合
index++;
hm.put(index, "大王");
list.add(index); //将53索引添加到集合
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer> gaojin = new TreeSet<>();
TreeSet<Integer> longwu = new TreeSet<>();
TreeSet<Integer> me = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
if(i>=list.size() -3 ){
dipai.add(list.get(i)); //将三张底牌存储在底牌集合
}else if(i % 3 == 0){
gaojin.add(list.get(i));
}else if(i % 3 == 1){
longwu.add(list.get(i));
}else{
me.add(list.get(i));
}
}
//看牌
lookPoker(hm, gaojin, "高进");
lookPoker(hm, longwu, "龙五");
lookPoker(hm, me, "我");
lookPoker(hm, dipai, "底牌");
}
//返回值类型void
//参数列表 HashMap,TreeSet,String name
public static void lookPoker(HashMap<Integer, String> hm ,TreeSet<Integer> ts, String name){
System.out.print(name + "的牌是:");
for(Integer i : ts){ //i代表双列集合中的每一个键
System.out.print(hm.get(i)+" ");
}
System.out.println();
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
import javax.swing.LookAndFeel;
public class Test3 {
/*
* 分析:
* 1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
* 2,洗牌
* 3,发牌
* 4,看牌
* */
public static void main(String[] args) {
//1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"红桃","黑桃","方块","梅花"};
HashMap<Integer, String> hm = new HashMap<>();//存储索引和扑克牌
ArrayList<Integer> list = new ArrayList<>();//存储索引
int index = 0;
//拼接扑克牌,并将索引和扑克牌存储在HashMap中
for(String s1 : num){ //获取数字
for(String s2 : color){//获取颜色
hm.put(index, s2.concat(s1));
list.add(index); //将0-51索引添加到集合
index++;
}
}
//将小王添加到双列集合中
hm.put(index, "小王");
list.add(index); //将52索引添加到集合
index++;
hm.put(index, "大王");
list.add(index); //将53索引添加到集合
//洗牌
Collections.shuffle(list);
//发牌
TreeSet<Integer> gaojin = new TreeSet<>();
TreeSet<Integer> longwu = new TreeSet<>();
TreeSet<Integer> me = new TreeSet<>();
TreeSet<Integer> dipai = new TreeSet<>();
for (int i = 0; i < list.size(); i++) {
if(i>=list.size() -3 ){
dipai.add(list.get(i)); //将三张底牌存储在底牌集合
}else if(i % 3 == 0){
gaojin.add(list.get(i));
}else if(i % 3 == 1){
longwu.add(list.get(i));
}else{
me.add(list.get(i));
}
}
//看牌
lookPoker(hm, gaojin, "高进");
lookPoker(hm, longwu, "龙五");
lookPoker(hm, me, "我");
lookPoker(hm, dipai, "底牌");
}
//返回值类型void
//参数列表 HashMap,TreeSet,String name
public static void lookPoker(HashMap<Integer, String> hm ,TreeSet<Integer> ts, String name){
System.out.print(name + "的牌是:");
for(Integer i : ts){ //i代表双列集合中的每一个键
System.out.print(hm.get(i)+" ");
}
System.out.println();
}
}
泛型固定下边界
package com.heima.collections;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
import com.heima.bean.BaseStudent;
import com.heima.bean.Student;
public class Demo2_Genric {
/*
* 泛型固定下边界
* ? super E
*
* 泛型固定上边界
* ?extends E
*
* */
public static void main(String[] args) {
// demo1();
TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
ts1.add(new Student("张三",23));
ts1.add(new Student("李四",13));
ts1.add(new Student("王五",33));
ts1.add(new Student("赵六",63));
System.out.println(ts1);
TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge());
ts2.add(new BaseStudent("张三",23));
ts2.add(new BaseStudent("李四",13));
ts2.add(new BaseStudent("王五",33));
ts2.add(new BaseStudent("赵六",63));
System.out.println(ts2);
}
private static void demo1() {
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("张三", 23));
list1.add(new Student("李四", 23));
ArrayList<BaseStudent> list2 = new ArrayList<>();
list2.add(new BaseStudent("张三", 23));
list2.add(new BaseStudent("李四", 23));
list1.addAll(list2);
}
}
class CompareByAge implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
return num == 0 ? s1.getName().compareTo(s2.getName()) : num;
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
import com.heima.bean.BaseStudent;
import com.heima.bean.Student;
public class Demo2_Genric {
/*
* 泛型固定下边界
* ? super E
*
* 泛型固定上边界
* ?extends E
*
* */
public static void main(String[] args) {
// demo1();
TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
ts1.add(new Student("张三",23));
ts1.add(new Student("李四",13));
ts1.add(new Student("王五",33));
ts1.add(new Student("赵六",63));
System.out.println(ts1);
TreeSet<BaseStudent> ts2 = new TreeSet<>(new CompareByAge());
ts2.add(new BaseStudent("张三",23));
ts2.add(new BaseStudent("李四",13));
ts2.add(new BaseStudent("王五",33));
ts2.add(new BaseStudent("赵六",63));
System.out.println(ts2);
}
private static void demo1() {
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("张三", 23));
list1.add(new Student("李四", 23));
ArrayList<BaseStudent> list2 = new ArrayList<>();
list2.add(new BaseStudent("张三", 23));
list2.add(new BaseStudent("李四", 23));
list1.addAll(list2);
}
}
class CompareByAge implements Comparator<Student>{
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
return num == 0 ? s1.getName().compareTo(s2.getName()) : num;
}
}
package com.heima.bean;
public class BaseStudent extends Student {
public BaseStudent() {
}
public BaseStudent(String name, int age) {
super(name, age);
}
}
public class BaseStudent extends Student {
public BaseStudent() {
}
public BaseStudent(String name, int age) {
super(name, age);
}
}