Map集合Collections集合工具类斗地主案例

    技术2025-12-07  15

    文章目录

    Map集合Map集合概述和特点Map集合的基本功能Map集合的获取功能Map集合的遍历方式 Collections集合工具类Collections概述和使用斗地主案例 CET4P225

    Map集合

    Map集合概述和特点

    Map集合概述

    interface Map<K,V> K:键的类型;V:值的类型

    Map集合的特点 键值对映射关系一个键对应一个值键不能重复,值可以重复元素存取无序

    基本使用

    public class example_1 { public static void main(String[] args) { HashMap<String, Integer> stringIntegerHashMap = new HashMap<String, Integer>(); //创建一个Map集合 stringIntegerHashMap.put("哥哥",19); //往集合里添加键值对 stringIntegerHashMap.put("妹妹",16); stringIntegerHashMap.put("弟弟",14); System.out.println(stringIntegerHashMap); } }

    Map集合的基本功能

    方法名说明V put(K key,V value)添加元素V remove(Object key)根据键删除键值对元素void clear()移除所有的键值对元素boolean containsKey(Object key)判断集合是否包含指定的键boolean containsValue(Object value)判断集合是否包含指定的值boolean isEmpty()判断集合是否为空int size()集合的长度,也就是集合中键值对的个数

    Map集合的获取功能

    方法名说明V get(Object key)根据键获取值Set keySet()获取所有键的集合Collection values()获取所有值的集合Set<Map.Entry<K,V>> entrySet()获取所有键值对对象的集合

    获取所有键值对并遍历的例子

    public class example_1 { public static void main(String[] args) { HashMap<String, Integer> stringIntegerHashMap = new HashMap<String, Integer>(); //创建一个Map集合 stringIntegerHashMap.put("哥哥",19); //往集合里添加键值对 stringIntegerHashMap.put("妹妹",16); stringIntegerHashMap.put("弟弟",14); //System.out.println(stringIntegerHashMap); Set<Map.Entry<String, Integer>> entries = stringIntegerHashMap.entrySet(); for (Map.Entry<String, Integer> st : //用for循环进行遍历 entries) { System.out.println(st); } } }

    Map集合的遍历方式

    第一种

    获取所有键的集合。用keySet()方法实现遍历键的集合,获取到每一个键。用增强for实现根据键去找值。用get(Object key)方法实现

    关键代码

    Set<String> strings = stringIntegerHashMap.keySet(); //1.调用方法返回一个set集合 for (String key : //2.然后通过遍历set来遍历Map strings) { Integer value = stringIntegerHashMap.get(key); //3.根据键去找值 System.out.println(key+value);

    第二种

    获取所有键值对对象的集合 Set<Map.Entry<K,V>> entrySet():获取所有键值对对象的集合遍历键值对对象的集合,得到每一个键值对对象 用增强for实现,得到每一个Map.Entry根据键值对对象获取键和值 用getKey()得到键 用getValue()得到值 Set<Map.Entry<String, Integer>> entries = stringIntegerHashMap.entrySet(); //1.调用方法获取键值对对象 for (Map.Entry<String, Integer> Me : //2.利用增强for遍历 entries) { String key = Me.getKey(); //3.根据键值对对象获取键 Integer value = Me.getValue(); //3.根据键值对对象获取值 System.out.println(key + value); }

    统计字符串中每个字符出现的个数例子

    public class example_10 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个字符串:"); String s = scanner.nextLine(); HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>(); for (int i = 0; i < s.length(); i++) { char key = s.charAt(i); //通过索引获取具体字符 Integer value = characterIntegerHashMap.get(key); if (value == null) { //最开始Map是空的,也就是没有这个key时就为1 characterIntegerHashMap.put(key, 1); } else { //当key存在的时候value就加1 value++; characterIntegerHashMap.put(key, value); } } StringBuilder stringBuilder = new StringBuilder(); Set<Character> characters = characterIntegerHashMap.keySet(); for (Character c : characters) { Integer value = characterIntegerHashMap.get(c); stringBuilder.append(c).append("(").append(value).append(")"); //将字符串进行拼接 } String string = stringBuilder.toString(); System.out.println(string); } }

    Collections集合工具类

    Collections概述和使用

    Collections类的作用是针对集合操作的工具类

    方法名说明Collections类常用方法public static void sort(List list)将指定的列表按升序排序public static void reverse(List<?> list)反转指定列表中元素的顺序public static void shuffle(List<?> list)使用默认的随机源随机排列指定的列表

    斗地主案例

    导包这个应该问题不大

    import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; public class example_14 { public static void main(String[] args) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); //1.创建HashMap数组,键是编号,值是牌 ArrayList<Integer> arrayList = new ArrayList<Integer>(); //2.创建ArrayList用于存储编号 String[] color = {"♦", "♣", "♥", "♠"}; //3.创建花色数组和点数数组 String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; int index = 0; for (String n : //4.从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号 number) { for (String c : color) { hashMap.put(index, c + n); arrayList.add(index); index++; } } hashMap.put(index, "小王"); //添加大王 arrayList.add(index); //添加编号 index++; hashMap.put(index, "大王"); arrayList.add(index); Collections.shuffle(arrayList); //使用工具类的方法进行洗牌 TreeSet<Integer> A = new TreeSet<Integer>(); //使用树集的目的是确保序列 TreeSet<Integer> B = new TreeSet<Integer>(); TreeSet<Integer> C = new TreeSet<Integer>(); TreeSet<Integer> bottom = new TreeSet<Integer>(); //地主牌树集 for (int i = 0; i < arrayList.size(); i++) { int x = arrayList.get(i); if (i >= arrayList.size() - 3) { //分派地主牌 bottom.add(x); } else if (i % 3 == 0) { A.add(x); } else if (i % 3 == 1) { B.add(x); } else if (i % 3 == 2) { C.add(x); } } lookPoker("大哥", A, hashMap); //调用静态方法进行看牌 lookPoker("弟弟", B, hashMap); lookPoker("妹妹", C, hashMap); lookPoker("地主牌", bottom, hashMap); } //定义一个看牌的方法,利用编号对应牌值 public static void lookPoker(String name, TreeSet<Integer> treeSet, HashMap<Integer, String> hashMap) { System.out.println(name + "的牌是:"); for (Integer integer : treeSet) { String s = hashMap.get(integer); //对应编号的牌值 System.out.print(s + " "); } System.out.println(); } }

    CET4P225

    clickpostponedisastergallonheadlinemarkercompetitionconvertliterpublishconvincekneellogical
    Processed: 0.009, SQL: 9