浅谈java之Map集合

Map集合

在这里插入图片描述
Map接口概述
查看API可以知道:
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值

HashMap<String, String> hm = new HashMap<>();
    //存储键值对
  /*  V put (K key, V value)
    在此映射中关联指定值与指定键。*/
    String s = hm.put("文章", "马伊琍");
    System.out.println(s);
    //键相同,值覆盖,当你第一次存储这个键值对时,返回的是null
    //当你第二次存储相同的键,但是值不同,就会发生键相同,值覆盖,返回的是这个键第一次的旧值
    String s1 = hm.put("文章", "姚笛");
    System.out.println(s1);
    hm.put("贾乃亮", "李小璐");
    hm.put("王宝强", "蓉儿");
    hm.put("陈羽凡", "白百合");
    hm.put("王大治", "董洁");
    hm.put("大朗", "金莲");

    System.out.println(hm);

Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效

Integer和String重写了equals和hashCode方法

子类:LinkedHashMap、HashMap、TreeMap

Map集合的功能概述

​ a:添加功能
​ V put(K key,V value):添加元素。这个其实还有另一个功能?替换
​ 如果键是第一次存储,就直接存储元素,返回null
​ 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
​ b:删除功能
​ void clear():移除所有的键值对元素
​ V remove(Object key):根据键删除键值对元素,并把值返回
​ c:判断功能
​ boolean containsKey(Object key):判断集合是否包含指定的键
​ boolean containsValue(Object value):判断集合是否包含指定的值
​ boolean isEmpty():判断集合是否为空
​ d:获取功能
​ Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
​ V get(Object key):根据键获取值
​ Set keySet():获取集合中所有键的集合
​ Collection values():获取集合中所有值的集合
​ e:长度功能
​ int size():返回集合中的键值对的对数

 HashMap<String, String> hm = new HashMap<>();
 //存储键值对
 String s = hm.put("文章", "马伊琍");
 hm.put("贾乃亮", "李小璐");
 hm.put("王宝强", "蓉儿");
 hm.put("陈羽凡", "白百合");
 hm.put("王大治", "董洁");
 hm.put("大朗", "金莲");
 //移除一个键值对
 String value = hm.remove("大朗");
 System.out.println(value);
 System.out.println(hm);
 //清空集合中所有的键值对
// hm.clear();
 System.out.println(hm);
 boolean empty = hm.isEmpty(); //判断集合是否
 int size = hm.size();//获取集合的长度

 //判断集合中是否包含这个键
 System.out.println(hm.containsKey("贾乃亮"));

 //判断集合中是否包含这个值

 System.out.println(hm.containsValue("金莲"));

Map集合的遍历

1.键找值:
获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值

  //存储键值对
  String s = hm.put("文章", "马伊琍");
  hm.put("贾乃亮", "李小璐");
  hm.put("王宝强", "蓉儿");
  hm.put("陈羽凡", "白百合");
  hm.put("王大治", "董洁");
  hm.put("大朗", "金莲");

  //遍历map集合方式1 ,根据键找值
/*  String value = hm.get("文章");
  System.out.println(value);*/
  Set<String> strings = hm.keySet(); //获取所有键的集合
  //遍历键集,根据键找值
  for (String key : strings) {
      System.out.println(key+"==="+hm.get(key));
  }

  //获取值的集合
  Collection<String> values = hm.values();
  System.out.println(values);

2.键值对对象找键和值:
获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值

HashMap<String, String> hm = new HashMap<>();
//存储键值对
String s = hm.put("文章", "马伊琍");
hm.put("贾乃亮", "李小璐");// Node<K,V> getKey() getValue()
hm.put("王宝强", "蓉儿");
hm.put("陈羽凡", "白百合");
hm.put("王大治", "董洁");
hm.put("大朗", "金莲");

//map集合的遍历方式2:把键找对,统一看作一个整体取出来,放到一个集合中
//Map.Entry<String, String>
/*接口 Map.Entry<K, V>
K getKey ()
返回与此项对应的键。
V getValue ()
返回与此项对应的值。*/

Set<Map.Entry<String, String>> entries = hm.entrySet();
for (Map.Entry<String, String> entry : entries) {
   // System.out.println(entry); Node
    String key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+"==="+value);

LinkedHashMap

A:LinkedHashMap的概述: Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
B:LinkedHashMap的特点: 底层的数据结构是链表和哈希表 元素有序 并且唯一元素的有序性由链表数据结构保证,唯一性由哈希表数据结构保证Map集合的数据结构只和键有关

TreeMap

键的数据结构是红黑树,可保证键的排序和唯一性

排序分为自然排序和比较器排序

线程是不安全的效率比较高

HashMap和Hashtable的区别

底层数据结构都是哈希表!

HashMap: 线程不安全,效率高.允许null值和null键
Hashtable: 线程安全 , 效率低.不允许null值和null键

Collections(集合工具类)

A:Collections类概述: 针对集合操作 的 工具类
B:Collections成员方法
public static void sort(List list): 排序,默认按照自然顺序
public static int binarySearch(List<?> list,T key): 二分查找
public static T max(Collection<?> coll): 获取最大值
public static void reverse(List<?> list): 反转
public static void shuffle(List<?> list): 随机置换

集合练习

模拟斗地主(洗牌,发牌,看牌)
1.无顺序

//斗地主  无顺序
        //创建集合装牌
        ArrayList<String> arrayList = new ArrayList<>();
        String[]arr={"黑桃","红桃","方块","梅花"};
        String[]arr1={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        for (int i = 0; i < arr.length; i++) {
            for (int j= 0; j < arr1.length; j++) {
                arrayList.add(arr[i]+arr1[j]);
            }
        }

        arrayList.add("大王");
        arrayList.add("小王");

        //洗牌
        Collections.shuffle(arrayList);
        Collections.shuffle(arrayList);
        //发牌
        //创建四个集合装玩家和底牌
        ArrayList<String> xiaoming = new ArrayList<>();
        ArrayList<String> dadong  = new ArrayList<>();
        ArrayList<String> laoli = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            if(i>=arrayList.size()-3){
                dipai.add(arrayList.get(i));
            }else if(i%3==0){
                xiaoming.add(arrayList.get(i));
            }else if(i%3==1){
                dadong.add(arrayList.get(i));
            }else
                laoli.add(arrayList.get(i));
        }
        //看牌
        lookpoker("小明",xiaoming);
        System.out.println();
        lookpoker("大东",dadong);
        System.out.println();
        lookpoker("老李",laoli);
        System.out.println();
        lookpoker("底牌",dipai);
        System.out.println();
    }

    private static void lookpoker(String name, ArrayList<String> wanjia) {
        System.out.println(name);
        for (String s : wanjia) {
            System.out.print(s+" ");

        }

2.有顺序

 //斗地主  有顺序
        //创建集合装牌
        ArrayList<Integer> indexnum = new ArrayList<>();
        HashMap<Integer,String> pokerbox = new HashMap<>();
        String[]arr={"黑桃","红桃","方块","梅花"};
        String[]arr1={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        int index=0;
        for (int i = 0; i < arr.length; i++) {
            for (int j= 0; j < arr1.length; j++) {
               indexnum.add(index);
               pokerbox.put(index,arr[i]+arr1[i]);
               index++;
            }

        }
        //System.out.println(index);
        indexnum.add(index);
        pokerbox.put(index,"小王");
        index++;
        indexnum.add(index);
        pokerbox.put(index,"大王");
        //洗牌
        Collections.shuffle(indexnum);
        Collections.shuffle(indexnum);
        Collections.shuffle(indexnum);
        //发牌
        TreeSet<Integer> zhangsan = new TreeSet<>();
        TreeSet<Integer>  lisi= new TreeSet<>();
        TreeSet<Integer> wangwu = new TreeSet<>();
        TreeSet<Integer> dipai = new TreeSet<>();
        for (int i = 0; i < indexnum.size(); i++) {
            if(i>=indexnum.size()-3){
                dipai.add(indexnum.get(i));
            }else if(i%3==0){
               zhangsan .add(indexnum.get(i));
            }else if(i%3==1){
                lisi.add(indexnum.get(i));
            }else
               wangwu .add(indexnum.get(i));
        }
      //  System.out.println(wangwu);
        //看牌
        lookpoker("张三",zhangsan,pokerbox);
        lookpoker("李四",lisi,pokerbox);
        lookpoker("王五",wangwu,pokerbox);
        lookpoker("底牌",dipai,pokerbox);
    }

    private static void lookpoker(String name, TreeSet<Integer> wanjia, HashMap<Integer, String> pokerbox) {
        System.out.println(name);
        for (Integer integer : wanjia) {
            String value = pokerbox.get(integer);
            System.out.print(integer+value+" ");
        }
        System.out.println();
发布了46 篇原创文章 · 获赞 1 · 访问量 1010

猜你喜欢

转载自blog.csdn.net/qq_42022411/article/details/103072107
今日推荐