首页 > 其他分享 >双列集合

双列集合

时间:2023-09-09 11:56:46浏览次数:23  
标签:map put System 双列 key 集合 println out

title: 双列集合
index_img: img/21.svg
tags:
  - Java SE
  - 集合
categories:
  - Java SE
hide: false
excerpt: 原理、常用方法、遍历

双列集合

由一对对元素组成{key-value}的集合。

  • 一次存一对数据
  • key不能重复【元素对唯一】,value可以
  • key和value是一一映射关系
  • {key-value}称为键值对(对象),再java中叫做Entry对象

双列集合接口Map

常用方法

方法名称 说明
V put(K key, V value) 添加元素(元素存在【看key】就会覆盖)
V remove(Object key) 根据键删除键值对元素,返回value或null
clear() 移除所有的键值对元素
boolean containsKey(Object key) 判断集合是否包含指定的
boolean containsValue(Object value) 判断集合是否包含指定的
boolean isEmpty() 判断集合是否为空
size() 集合的长度,也就是集合中键值对的个数
public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("张三", 12); //添加和覆盖数据
        map.put("李斯", 23);
        map.put("李斯", 32); //key相同覆盖前一个
        map.put("Tom", 12);
        System.out.println(map);

        System.out.println("--------------");
        if (map.containsKey("张三")) {
            map.remove("张三");
        }
        System.out.println(map);

        System.out.println("--------------");
        if (map.isEmpty()) {
            System.out.println("map空");
        } else {
            System.out.println(map.size());
        }
    }
}

image-20230809130120199

遍历

键找值

  • 键是唯一的,可以根据键找值
  • 将键放入单列集合,然后遍历【单列集合】
public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Tom", 100);
        map.put("Lay", 90);
        map.put("kup", 90);
        map.put("duk", 0);

        Set<String> keys = map.keySet();  //将键放在单列集合中

        //forEach
        keys.forEach(s -> {
            System.out.println("{" + s + "-" + map.get(s) + "}"); //通过map集合的get(key)获取Value
        });

        //增强for
        System.out.println("-------------");
        for (String s : keys) {
            System.out.println("{" + s + "-" + map.get(s) + "}");
        }

        //迭代器
        System.out.println("-------------");
        Iterator<String> it = keys.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println("{" + s + "-" + map.get(s) + "}");
        }
    }
}

image-20230809131448871

键值对

获取键值对对象,放入单列集合,遍历【单列集合】

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("Tom", 100);
        map.put("Lay", 90);
        map.put("kup", 90);
        map.put("duk", 0);

        Set<Map.Entry<String, Integer>> entries = map.entrySet(); //Entry是Map中定义的一个接口,用于管理键值对set对象

        entries.forEach(s -> System.out.println("{" + s.getKey() + "-" + s.getValue() + "}"));

        System.out.println("------------------");
        for (Map.Entry<String, Integer> s : entries) {
            System.out.println("{" + s.getKey() + "-" + s.getValue() + "}");
        }

        System.out.println("----------------");
        Iterator<Map.Entry<String, Integer>> it = entries.iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> s = it.next();
            System.out.println("{" + s.getKey() + "-" + s.getValue() + "}");
        }
    }
}

image-20230809132939577

forEach遍历Map

  • 这里底层使用的是键值对方式
public class Main {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("李白", "天生我材必有用");
        map.put("杜甫", "国破山河在");
        map.put("李清照", "不肯过江东");

        map.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String key, String value) {
                System.out.println("{" + key + "-" + value + "}");
            }
        });

        System.out.println("------------------");
        map.forEach((key, value) -> System.out.println("{" + key + "-" + value + "}"));
    }
}

image-20230809134325845

双列集合实现类

HashMap

  • 无序、不重复【key决定的】、无索引
  • 底层结构与HashSet一样(数组、链表、红黑树),但规则有些不同
    • 使用的只是Key的哈希值【自定义对象只需要重写key的hashCode和equals即可】
    • 哈希值一样,是直接覆盖,而不是丢弃不存
    • 哈希值不一样是与HashSet一样
    • 当HashMap中的元素数量超过了负载因子(默认为0.75)与当前容量的乘积时,HashMap就会进行扩容。扩容后的容量是原来的两倍【新建数组】。

如此:

通过重写equalshashCode来保证Key的唯一性

  • 如果键是自定义对象【如学生】需要两个方法都重写【非自定义对象,java写好了】
  • 如果值是自定义对象,都不需要重写

问题描述

创建一个HashMap集合,【重写】是学生对象(Student),值是籍贯(String)

  • 存储三个键值对元素,并遍历
  • 要求:同姓名,同年龄认为是同一个学生【重写】
    • 可以通过的新学生对象否覆盖来判断
public class Main {
    public static void main(String[] args) {
        HashMap<Student, String> stu = new HashMap<>();

        Student stu1 = new Student("张三", 18);
        Student stu2 = new Student("李四", 8);
        Student stu3 = new Student("王五", 12);
        Student stu4 = new Student("王五", 12);

        stu.put(stu1, "云南");
        stu.put(stu2, "江苏");
        stu.put(stu3, "浙江");
        stu.put(stu4, "黑龙江"); //重写则覆盖stu3【已经重写】,否则会由两个王五,不符合要求

        //键找值遍历
        Set<Student> set = stu.keySet();
        set.forEach(s -> System.out.println("{" + s + "-" + stu.get(s) + "}"));
        System.out.println("---------");

        //键值对遍历
        for (Map.Entry<Student, String> s : stu.entrySet()) {
            System.out.println("{" + s.getKey() + "-" + s.getValue() + "}");
        }
        System.out.println("---------");

        //map的forEach
        stu.forEach((key, value) -> System.out.println("{" + key + "-" + value + "}"));
    }
}

image-20230809141234414

某个班级80名学生,现在需要组成秋游活动,班长提供了四个景点依次是(A、B、C、D),每个学生只能选择一个景点,请统计出最终哪个景点想去的人数最多。

  • 可以定义为map集合
  • 判断收集到的数据中,key是否存在
    • 存在,数目+1,覆盖
    • 不存在,新建
public class Main {
    public static void main(String[] args) {
        //投票的数据
        String[] arr = {"A", "B", "C", "D"}; //存储键,也可以使用单列集合

        //模拟投票和存储
        Random random = new Random();
        ArrayList<String> list = new ArrayList<>();//存储80个投票的结果【ABCD之间】
        for (int i = 0; i < 80; i++) {
            int index = random.nextInt(arr.length); //再【0~4)之间生成数字,模拟投票的选择在ABCD之间
            list.add(arr[index]);
        }

        //统计规则:使用map适应性比计数器更好
        HashMap<String, Integer> map = new HashMap<>();
        for (String key : list) {
            if (map.containsKey(key)) {
                //当前key存在,获取先前票数【Value】,然后++,然后覆盖
                int count = map.get(key);
                count++;
                map.put(key, count);
            } else {
                //当前key不存在
                map.put(key, 1);
            }
        }

        //打印统计信息
        System.out.println(map);

        //统计投票信息,求最大值(可能由多个)
        int max = 0; //以0为基准
        for (Map.Entry<String, Integer> kv : map.entrySet()) {
            int num = kv.getValue();
            if (num > max) {
                max = num;
            }
        }

        System.out.println("max:" + max);

        //如果由多个最大值,需要都打出
        for (Map.Entry<String, Integer> skv : map.entrySet()) {
            int num = skv.getValue();
            if (num == max) {
                System.out.println("max:" + num);
            }
        }
    }
}

image-20230809144452337

LinkedHashMap

  • 有序、不重复【key】、无索引
    • 存取有序
  • 底层结构,哈希表+双链表存储存取顺序
public class Main {
    public static void main(String[] args) {
        LinkedHashMap<String, String> linkMap = new LinkedHashMap<>();
        linkMap.put("C", "1");
        linkMap.put("D", "2");
        linkMap.put("A", "3");
        linkMap.put("F", "4");
        linkMap.forEach((key, value) -> System.out.println("{" + key + "-" + value + "}")); //有序
    }
}

image-20230809145426891

TreeMap

TreeMap跟TreeSet底层原理一样,都是红黑树结构的。

由键决定特性:不重复、无索引、可排序【键排序】

自定义排序依旧可以使用两种方法(参考TreeSet)

问题描述

键:整数表示id
值:字符串表示商品名称
要求:按照id的升序排列

方法1:javabean实现Compareable接口

public class Goods implements Comparable<Goods> {

    @Override
    public int compareTo(Goods goods) {
        return this.ID - goods.ID;
    }
  	重写hashCode和equals
    ...
      
public class Main {
    public static void main(String[] args) {
        Goods goods1 = new Goods("小吃1", 5);
        Goods goods2 = new Goods("小吃2", 3);
        Goods goods3 = new Goods("小吃3", 4);
        Goods goods4 = new Goods("小吃4", 1);

        TreeMap<Goods, String> treeMap = new TreeMap<>();
        treeMap.put(goods1, "小吃区");
        treeMap.put(goods2, "小吃区");
        treeMap.put(goods3, "小吃区");
        treeMap.put(goods4, "小吃区");

        treeMap.forEach((key, value) -> System.out.println("[" + key + "-" + value + "]"));
    }
}

image-20230809151235015

方法2:javabean实现Comparator接口

  • 这里我们用降序来区分
public class Main {
    public static void main(String[] args) {
        Goods goods1 = new Goods("小吃1", 5);
        Goods goods2 = new Goods("小吃2", 3);
        Goods goods3 = new Goods("小吃3", 4);
        Goods goods4 = new Goods("小吃4", 1);

        TreeMap<Goods, String> treeMap = new TreeMap<>((s1,s2)->Integer.compare(s2.ID,s1.ID)); //注意
        treeMap.put(goods1, "小吃区");
        treeMap.put(goods2, "小吃区");
        treeMap.put(goods3, "小吃区");
        treeMap.put(goods4, "小吃区");

        treeMap.forEach((key, value) -> System.out.println("[" + key + "-" + value + "]"));
    }
}

image-20230809151445720

标签:map,put,System,双列,key,集合,println,out
From: https://www.cnblogs.com/SimpleWord/p/17689191.html

相关文章

  • 【Ps小问题集合】
    (Ps小问题集合)一、小问题1.1Ps将图片裁剪成圆的首先打开ps,打开需要进行裁剪的图片选择右侧的椭圆选框工具按着shift可以进行圆形的绘制,不按shift则会是椭圆绘制完成后,按着ctrl+j键,新建图层,即可在右侧图层栏新建一图层,图层内容为刚刚选中的部分,将最开始的图层的眼睛选择......
  • sql集合-查询语句
    1,张飞借阅书的籍-子查询SELECTb.book_nameFROMbookinfobINNERJOINbook_borrowbbONb.book_id=bb.book_idWHEREbb.card_id=(SELECTcard_idFROMreaderWHEREname="张飞")2,左/右链接查询SELECTb.book_name,bb.borrow_dataFROMbookinfobRIGHTJOINbook_borrowb......
  • 集合数据类型内置方法
    集合基本用不到,考试会考作用就是存储一大堆元素的东西,容器数据类型定义方式以{}用逗号隔开不可变数据类型s={1,2,1,'a','a','c'}print(s)去重 #用得到乱序lis=[1,2,3,1,3]print(set(lis))print(list(set(lis)))s={}#空大括号是字典,不是集合,定义空集合必须得......
  • list集合分组拆分
    #n组publicstatic<T>List<List<T>>averageAssign(List<T>source,intn){List<List<T>>result=Lists.newArrayList();intrenumber=source.size()%n;intnumber=source.size()/n;......
  • 集合学习总结
    集合总结一、概述作用:存储对象的容器,代替数组的,使用更加的便捷所处的位置:java.util体系结构二、Collection内部的每一个元素都得是引用数据类型常用方法add(Objecto)添加元素addAll(Collectionc)将指定集合中的所有元素存入到当前集合remove(Objecto)......
  • mysql集合查询
    统计一个班级共有多少学生?selectcount(*)ASnumsfromstudent;统计数学成绩大于90分的学生有多少个?selectcount(*)ASnumsfromstudentwheremath>=90;统计总分大于250分的人数有多少?selectcount(*)ASnumsfromstudentwhere(math+chinese+englist)>250;查看表的创建......
  • 集合
    集合一、集合框架图(简化)二、Collection接口Collection接口是处理对象集合的根接口,其中定义了很多对元素进行操作的方法。Collection接口有两个主要的子接口List和Set,注意:Map不是Collection的子接口Collection常用的方法:booleanadd(Objecto)向集合中添加一个元素......
  • 新人笔记-集合2.0
    importjava.util.ArrayList;/*publicbooleanremove(Object)删除指定的元素,返回删除是否成功publicEremove(intindex)删除指定索引处的元素,返回被删除的元素publicEset(intindex.Eelement)修改指定索引处的元素,返回被修改的元素publicEget(intin......
  • 刷面经的正确姿势,快来get!大厂面经集合2.0
    本文首发自公粽hao「林行学长」,欢迎来撩,免费领取20个求职工具资源包。了解校招、分享校招知识的学长来了!想必24届的朋友们都在面试的路上了吧,面试可是校招的必经环境。作为经历过校招的学长,曾经也是一枚面试小白,完全不知道如何应对面试、不知道面试官会问什么问题。但是现在,许多......
  • 每个.NET开发都应掌握的C#集合知识点
    上篇文章讲述了C#委托和事件知识点,本文将介绍C#集合知识点。作为.NET开发人员,C#集合是你在构建强大和高效应用程序时的关键技能之一。C#集合提供了一系列丰富的数据结构,可以帮助你更好地管理、操作和组织数据。本文将介绍一些每个.NET开发人员都应该掌握的C#集合知识点。1、灵活......