目录
一:Map
1.1 Map集合概述和使用:
Map集合概述
- Interface Map<K,V> K:键的使用 ; V:值的使用
- 将键映射到值的对象; 不能包含重复的键;每个键可以映射到最多一个值
- 举例:学生的学号和姓名
itheima001 小花
itheima002 小刚
itheima003 小雨
创建Map集合的对象
- 多态的方式
- 具体的实现类HashMap
public class Main {
public static void main(String[] args) {
//创建集合对象
Map<String, String> map=new HashMap<>();
//V put(k key,V value)将指定的值与该映射中的指定键相关联
map.put("itheima01","小周");
map.put("itheima02","小王");
map.put("itheima03","小雨");
map.put("itheima03","小白");//小雨会被小白覆盖掉
//输出集合对象
System.out.println(map);//{itheima01=小周, itheima02=小王, itheima03=小白}
}
}
1.2 Map集合的基本功能:
1.3 Map集合的获取功能:
public class Main {
public static void main(String[] args) {
//创建集合对象
Map<String, String> map=new HashMap<>();
//添加元素
map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
//V get(Object key):根据键获取值
System.out.println(map.get("张无忌"));//赵敏
System.out.println(map.get("张三丰"));//null
//Set<K> keySet():获取所有键的集合
Set<String> keySet=map.keySet();
for(String key:keySet){
System.out.println(key);//杨过 郭靖 张无忌
}
//Collection<V> values():获取所有值的集合
Collection<String> values=map.values();
for(String value:values){
System.out.println(value);//小龙女 黄蓉 赵敏
}
}
}
1.4 Map集合的遍历(方式一):
public class Main {
public static void main(String[] args) {
//创建集合对象
Map<String, String> map=new HashMap<>();
//添加元素
map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
//获取所有键的集合,用keySet()方法实现
Set<String> keySet=map.keySet();
//遍历键的集合,获取到每一个键,用增强for实现
for (String key : keySet) {//快捷键:集合名.iter,可以实现for循环
//根据键找到对应的值
String value=map.get(key);
System.out.println(key+","+value);
}
}
}
1.5 Map集合的遍历(方式2)
public class Main {
public static void main(String[] args) {
//创建集合对象
Map<String, String> map=new HashMap<>();
//添加元素
map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
//获取所有键的集合,用keySet()方法实现
Set<Map.Entry<String, String>> entrySet=map.entrySet();
//遍历键值对对象的集合,的到每一个键值对对象
for(Map.Entry<String, String> me:entrySet){
//根据键值对对象获取键和值
String key=me.getKey();
String value=me.getValue();
System.out.println(key+","+value);
}
}
}
案例1:HashMap集合存储学生对象并遍历
import java.util.*;
public class Main {
public static void main(String[] args) {
//创建集合对象
Map<student,String> map=new HashMap<>();
//定义学生类对象
student s1=new student("林青霞",18);
student s2=new student("张曼玉",19);
student s3=new student("王祖贤",20);
student s4=new student("王祖贤",20);
//添加元素
map.put(s1,"西安");
map.put(s2,"武汉");
map.put(s3,"郑州");
map.put(s4,"北京");//郑州被北京覆盖了
//遍历集合
Set<student> keySet=map.keySet();
for(student s:keySet){
String value=map.get(s);
System.out.println(s.getName()+","+s.getAge()+","+value);
}
}
}
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 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;
}
}
案例2:集合嵌套之ArrayList嵌套HashSet
import java.util.*;
public class Main {
public static void main(String[] args) {
//创建ArrayList集合
ArrayList<HashMap<String, String>> array = new ArrayList<>();
//创建HashMap集合,并添加键值对元素
HashMap<String, String> hashMap1 = new HashMap<>();
hashMap1.put("孙策", "大乔");
hashMap1.put("周瑜", "小乔");
HashMap<String, String> hashMap2 = new HashMap<>();
hashMap2.put("郭靖", "黄蓉");
hashMap2.put("杨过", "小龙女");
HashMap<String, String> hashMap3 = new HashMap<>();
hashMap3.put("令狐冲", "任盈盈");
hashMap3.put("林平之", "岳灵珊");
//把hashMap作为元素添加到ArrayList集合
array.add(hashMap1);
array.add(hashMap2);
array.add(hashMap3);
//遍历ArrayList集合
for (HashMap<String, String> hm : array) {
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "," + value);
}
}
}
}
案例3:集合嵌套之HashSett嵌套ArrayList:
import java.util.*;
public class Main {
public static void main(String[] args) {
//创建HashSet集合
Map<String,ArrayList<String>> hashmap=new HashMap<>();
//创建ArrayList集合
ArrayList<String> array1=new ArrayList<>();
array1.add("诸葛亮");
array1.add("赵云");
ArrayList<String> array2=new ArrayList<>();
array2.add("唐僧");
array2.add("孙悟空");
ArrayList<String> array3=new ArrayList<>();
array3.add("武松");
array3.add("鲁智深");
hashmap.put("sgyy",array1);
hashmap.put("xyj",array2);
hashmap.put("shz",array3);
//遍历HashSet集合
Set<String> keyset=hashmap.keySet();
for(String s:keyset){
System.out.println(s);
ArrayList<String> values=hashmap.get(s);
for(String value:values){
System.out.println("\t"+value);
}
}
}
}
案例4:统计字符串中每个字符出现的次数:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一段字符串:");
String s=sc.nextLine();
TreeMap<Character,Integer> tm=new TreeMap<>();
for(int i=0;i<s.length();i++){
char key=s.charAt(i);
Integer value=tm.get(key);
if(value==null){
tm.put(key,1);
}else{
value++;
tm.put(key,value);
}
}
StringBuilder sb=new StringBuilder();
Set<Character> keymap = tm.keySet();
for(Character c:keymap){
Integer value=tm.get(c);
sb.append(c).append("(").append(value).append(")");
}
System.out.println(sb.toString());
}
}
二:Collentions
2.1 Collentions概述和使用
是针对集合操作的工具类
Collentions类的常用方法
- public static <T extends Comparable<? super T>> void sort(List<T>list):将指定的列表按升序排序。
- public static void reverse(List<?>list):反转指定列表中元素的顺序。
- public static void shuffle(List<?>list):使用默认的随机源随机排列指定的列表。
import java.util.*;
public class Main {
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);
Collections.sort(list);//升序
Collections.reverse(list);//反转
Collections.shuffle(list);//使用默认的随机源随机排列指定的列表
System.out.println(list);
}
}
案例:ArrayList存储学生对象并排序
import java.util.*;
public class Main {
public static void main(String[] args) {
//创建集合对象
ArrayList<student> array=new ArrayList<>();
//定义学生类
student s1=new student("lingqingxia",32);
student s2=new student("zhangmanyu",35);
student s3=new student("wangzuxian",33);
student s4=new student("liuyan",33);
student s5=new student("liuyan",33);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
//使用Collentions对ArrayList集合排序
//sort(List<T> list,Comparator<? super T> c)
Collections.sort(array, new Comparator<student>() {
@Override
public int compare(student s1,student s2) {
int num= s1.getAge()- s2.getAge();
int num1=num==0?s1.getName().compareTo(s2.getName()):num;
return num1;
}
});
//遍历集合
for(student s:array){
System.out.println(s.getName()+","+s.getAge());
}
}
}
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;
}
}
三:java实现斗地主
import java.util.*;
public class Main {
//定义方法看牌
public static void lookPoker(String name,TreeSet<Integer> ts,Map<Integer,String> hashMap){
System.out.print(name+"的牌是:");
for(Integer i:ts){
String value=hashMap.get(i);
System.out.print(value+" ");
}
System.out.println();
}
public static void main(String[] args) {
//创建HashMap,键是编号,值是牌
Map<Integer, String> hashMap = new HashMap<>();
//创建ArrayList,存储编号
ArrayList<Integer> array = new ArrayList<>();
//创建花色数组和点数数组
String[] pokerColors = {"◆", "♣", "♠", "♥"};
String[] pokerPoints = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
int index = 0;
for (String point : pokerPoints) {
for (String color : pokerColors) {
hashMap.put(index, color + point);
array.add(index);
index++;
}
}
//将大王和小王也存入牌盒中
hashMap.put(index, "小王");
array.add(index);
index++;
hashMap.put(index, "大王");
array.add(index);
//洗牌
Collections.shuffle(array);
//创建三个玩家对象以及三张底牌集合
TreeSet<Integer> farmer1=new TreeSet<>();
TreeSet<Integer> farmer2=new TreeSet<>();
TreeSet<Integer> farmer3=new TreeSet<>();
TreeSet<Integer> dpPokers=new TreeSet<>();
//发牌
for(int i=0;i<array.size();i++){
int x=array.get(i);
if(i>=array.size()-3){
dpPokers.add(x);
}else if(i%3==0){
farmer1.add(x);
}else if(i%3==1){
farmer2.add(x);
}else if(i%3==2){
farmer3.add(x);
}
}
//看牌
lookPoker("农民一号",farmer1,hashMap);
lookPoker("农民二号",farmer2,hashMap);
lookPoker("农民三号",farmer3,hashMap);
lookPoker("底牌",dpPokers,hashMap);
}
}