Map集合,Collections(集合工具类)

一:Map接口概述
将键映射到值的对象 ,每个键只能对应一个映射值,一个映射不能包含重复的键
二:Map集合基本功能
V put(K key,V value) K是键 V是映射的值 如果键相同则值覆盖
V remove(Object key) 移除键对应的映射值
void clear() 从此映射中移除所有映射关系
boolean containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true。
boolean containsValue(Object value) 如果此映射将一个或多个键映射到指定值,则返回 true。
boolean isEmpty() 如果此映射未包含键-值映射关系,则返回 true。
int size() 返回此映射中的键-值映射关系数
三 Map集合的遍历

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo4 {
    public static void main(String[] args) {
          Map   <String, String>  hm `这里写代码片` = new HashMap<String, String>();
         hm.put("小白", "小黑");
        hm.put("高富帅", "白富美");
        hm.put("小红", "小绿");
        //HashMap允许插入null值null键
        hm.put(null,null);
        //遍历map集合
        //Set<String> strings = hm.keySet();
        //for(String key:strings){
        //
        //    System.out.println(key+"==="+hm.get(key));
        //}

        //遍历map集合的方式2
        // Entry 键值对 对象
        Set<Map.Entry<String, String>> entries = hm.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            // K getKey()
            //          返回与此项对应的键。
            // V getValue()
            //          返回与此项对应的值。
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "===" + value);
        }
    }
}

四 集合嵌套
集合嵌套——HashMap嵌套HashMap

import java.util.HashMap;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        //篮球组
        //     姚明   24
        //     乔丹   22

        //足球组
        //    梅西    26
        //    c罗     29
        //存储这组数据
        //HashMap  嵌套HashMap
        HashMap<String, Integer> basektboll = new HashMap<>();//
        basektboll.put(" 姚明", 24);
        basektboll.put("乔丹", 22);
        HashMap<String, Integer> footboll = new HashMap<>();
        footboll.put("梅西", 26);
        footboll.put("c罗", 29);
        //再创建一个map集合
        HashMap<String, HashMap<String, Integer>> bigMap = new HashMap<>();
        bigMap.put("篮球组", basektboll);
        bigMap.put("足球组", footboll);
        //遍历 键找值
        Set<String> keys = bigMap.keySet();
        for (String k : keys) {
            System.out.println(k);
            HashMap<String, Integer> value = bigMap.get(k);
            Set<String> strings = value.keySet();
            for (String s : strings) {
                System.out.println("\t" + s + "==" + value.get(s));
            }
            System.out.println();
        }

    }
}

集合嵌套———HashMap嵌套ArrayList

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MyMapDemo {
    public static void main(String[] args) {
        //三国演义
        //        吕布
        //        小乔
        // 水浒传
        //        宋江
        //        吴用
        //西游记
        //      孙悟空
        //      猪八戒
        //把上面这组数据存起来  HashMap 嵌套List
        ArrayList<String> sgyy = new ArrayList<>();
        sgyy.add("吕布");
        sgyy.add("小乔");
        ArrayList<String> shz = new ArrayList<>();
        shz.add("宋江");
        shz.add("吴用");
        ArrayList<String> xyj = new ArrayList<>();
        xyj.add("孙悟空");
        xyj.add("猪八戒");

        HashMap<String, ArrayList<String>> hm = new HashMap<>();
        hm.put("西游记", xyj);
        hm.put("水浒传", shz);
        hm.put("三国演义",sgyy);

        //遍历
        Set<Map.Entry<String, ArrayList<String>>> entries = hm.entrySet();
        for (Map.Entry<String, ArrayList<String>> en : entries) {
            String key = en.getKey();
            System.out.println(key);
            ArrayList<String> value = en.getValue();
            for (String k : value) {
                System.out.println("\t"+k);
            }
            System.out.println();

        }


    }
}

集合嵌套—–ArrayList嵌套HashMap

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MyMapDemo4 {
    public static void main(String[] args) {
        //         周瑜-- - 小乔
        //        //吕布-- - 貂蝉
        //        //
        //        //郭靖-- - 黄蓉
        //        //杨过-- - 小龙女
        //        //
        //        //令狐冲-- - 任盈盈
        //        //林平之-- - 岳灵珊
        //List 嵌套 Map

        Map<String, String> sgyy = new HashMap<>();
        sgyy.put("周瑜", "小乔");
        sgyy.put("吕布", "貂蝉");

        Map<String, String> sdxl = new HashMap<>();
        sdxl.put("郭靖", "黄蓉");
        sdxl.put("杨过", "小龙女");

        Map<String, String> xajh = new HashMap<>();
        sdxl.put("令狐冲", "任盈盈");
        sdxl.put("林平之", "岳灵珊");
        ArrayList<Map<String, String>> maps = new ArrayList<>();
        maps.add(sgyy);
        maps.add(sdxl);
        maps.add(xajh);
        //遍历
        for (Map<String, String> map : maps) {
            Set<String> strings = map.keySet();
            for (String key : strings) {
                System.out.println(key + "===" + map.get(key));
            }
        }
    }
}

四:HashMap和Hashtable的区别
HashMap: 线程不安全,效率高.允许null值和null键
Hashtable: 线程安全 , 效率低.不允许null值和null键
五:Collections成员方法

    public static <T> void sort(List<T> list):     排序,默认按照自然顺序
        public static <T> int binarySearch(List<?> list,T key):二分查找
        public static <T> T max(Collection<?> coll): 获取最大值
        public static void reverse(List<?> list):   反转
        public static void shuffle(List<?> list):   随机置换

例斗地主(简易版)

import java.util.ArrayList;
import java.util.Collections;

public class 斗地主 {
    public static void main(String[] args) {
        //实现斗地主的发牌,洗牌,看牌,
        //思路:1.生成一副牌,放在集合(牌盒)
        //2.洗牌
        //3.三个人斗地主,留三张底牌 发牌
        //4.看牌 ,看谁拿了一副好牌

        //创建牌盒子
        ArrayList<String> pokerList = new ArrayList<>();
        //生成54张牌放进牌盒子中
        String[] colors = {"♥", "♠", "♣", "♦"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String color : colors) {
            for (String num : nums) {
                pokerList.add(color.concat(num));
            }
        }
        //手动添加大小王
        pokerList.add("☀");
        pokerList.add("?");
        //洗牌
        Collections.shuffle(pokerList);
        Collections.shuffle(pokerList);
        Collections.shuffle(pokerList);
        //发牌
        //定义四个集合
        ArrayList<String> 周星驰 = new ArrayList<>();
        ArrayList<String> 刘德华 = new ArrayList<>();
        ArrayList<String> 周润发 = new ArrayList<>();
        ArrayList<String> 底牌 = new ArrayList<>();
        //发牌规则
        //周星驰 0 3 6 9
        //刘德华 1 4 7
        //周润发 2 5 8
        for (int i = 0; i < pokerList.size(); i++) {
            if (i >= pokerList.size() - 3) {
                底牌.add(pokerList.get(i));
            } else if (i % 3 == 0) {
                周星驰.add(pokerList.get(i));

            } else if (i % 3 == 1) {
                刘德华.add(pokerList.get(i));

            } else if (i % 3 == 2) {
                周润发.add(pokerList.get(i));

            }


        }
        //看牌
        lookPoker("周星驰", 周星驰);
        lookPoker("刘德华", 刘德华);
        lookPoker("周润发", 周润发);
        lookPoker("底牌",底牌);



       // System.out.println(pokerList);
    }

    private static void lookPoker(String name, ArrayList<String> list) {
        System.out.println(name);
        for(String p:list){
            System.out.print(p+"  ");
        }
        System.out.println();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值