• Java之HashMap和TreeMap


    Map集合概述

    Collection接口下的集合,元素都是单个存储的,称为单列集合。

    比如身份证和个人,学生与学号,这种一一对应的关系叫做映射。

    Java中提供了Map集合,用于保存具有映射关系的数据,称为双列集合。

    Map集合特点

    Map

    Map是一种键值对集合,每一个元素都包含了一个键对象和一个值对象。

    Map集合中,键不允许重复,值可以重复。(比如身份证和姓名)

    键和值是一一对应的,通过键可以找到与之对应的唯一的值。

    HashMap

    常用方法

    项目Value
    V put(K key,V value)添加元素
    V get(Object key )根据指定的值在Map中取得对应的值
    V remove(Object key )根据键删除键值对元素
    void clear()移除所有的键值对元素,清空Map集合
    boolean containsKey(Object key)判断集合是否包含指定的键
    boolean containsValue(Object value)判断集合是否包含指定的值
    boolean isEmpty()判断集合是否为空
    int size()获取集合的长度,也就是集合中键值对的个数
     public static void main(String[] args) {
            //HashMap<键,值>
            HashMap<String, String> hashMap = new HashMap<>();
    
            //put添加
            hashMap.put("001","张三");
            hashMap.put("002","李四");
            hashMap.put("003","王五");
            hashMap.put("003","王五");//键相同时,键不变,值被覆盖
    
            //remove根据键删除
            hashMap.remove("001");
    
            //get根据键获取
            String s = hashMap.get("002");
            System.out.println(s);
    
            //clear清空
            hashMap.clear();
    
            //判断containsKey()包含键,存在返回true
            System.out.println(hashMap.containsKey("001"));
    
            //containsValue() 包含值
            System.out.println(hashMap.containsValue("张三"));
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    集合遍历

    根据键找值遍历

    键找值遍历

    1.从集合中获取所有的键

    2.遍历这个集合,根据每个键获取值

    public static void main(String[] args) {
            //HashMap<键,值>
            HashMap<String, String> hashMap = new HashMap<>();
    
            //put添加
            hashMap.put("001","张三");
            hashMap.put("002","李四");
            hashMap.put("003","王五");
            hashMap.put("003","王五");//键相同时,键不变,值被覆盖
    
            //键找值遍历
            //1.从集合中获取所有的键
            Set<String> set = hashMap.keySet();
            //2.遍历这个集合,根据每个键获取值
            for(String s : set){
                String value = hashMap.get(s);
                System.out.println(s+":"+value);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    根据键值对遍历

    1.获取键值对的集合 entrySet()

    2.遍历set集合,获取每个entry对象的键值对 getkey() getvalue()

    public static void main(String[] args) {
            HashMap<String, String> hashMap = new HashMap<>();
    
            //put添加
            hashMap.put("001","张三");
            hashMap.put("002","李四");
            hashMap.put("003","王五");
            hashMap.put("003","王五");//键相同时,键不变,值被覆盖
    
            //1.获取键值对的集合  entrySet()
            Set<Map.Entry<String, String>> set = hashMap.entrySet();
    
            //2.遍历set集合,获取每个entry对象的键值对 getkey()  getvalue()
            for (Map.Entry<String,String> entry : set){
                String key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key+":"+value);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    HashMap详情

    HashSet底层其实就是借助了HashMap实现存储

    //HashSet构造方法就是去new了一个HashMap
    public HashSet() {
            map = new HashMap<>();
        }
    
    • 1
    • 2
    • 3
    • 4

    所以HashSet和HashMap用法相差不大

    链接: HashSet

    HashMap<键,值>
    保证键的唯一,默认是根据键的地址判断是否重复,需要重写键的equals和hashCode方法

    重写后:

    public static void main(String[] args) {
            HashMap<Student, String> hashMap = new HashMap<>();
            hashMap.put(new Student("张三",22),"广州");
            hashMap.put(new Student("李四",11),"深圳");
            hashMap.put(new Student("王五",45),"北京");
            hashMap.put(new Student("王五",45),"成都");//键保存不变,值进行覆盖
    
            //键值对方式
            Set<Map.Entry<Student, String>> entries = hashMap.entrySet();
            for (Map.Entry<Student,String> map:entries){
                Student key = map.getKey();
                String value = map.getValue();
                System.out.println(key.getName()+","+key.getAge()+","+value);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    TreeMap

    TreeSet底层就是使用了TreeMap实现存储的

    链接: TreeSet

     public static void main(String[] args) {
            //自然排序数字类型
            TreeMap<Integer, String> treeMap = new TreeMap<>();
            treeMap.put(2,"张三");
            treeMap.put(1,"李四");
            treeMap.put(4,"王五");
            System.out.println(treeMap);
    
            //字符串按编码顺序排序
            TreeMap<String, String> treeMap2 = new TreeMap<>();
            treeMap2.put("D","张三");
            treeMap2.put("A","李四");
            treeMap2.put("B","王五");
            System.out.println(treeMap2);
    
            TreeMap<Student, String> treeMap3 = new TreeMap<>(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getAge() - o2.getAge();
                }
            });
            treeMap3.put(new Student("张三",22),"广州");
            treeMap3.put(new Student("李四",11),"深圳");
            treeMap3.put(new Student("王五",45),"北京");
            treeMap3.put(new Student("王五",45),"成都");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    红队专题-从零开始VC++C/S远程控制软件RAT-MFC-超级终端
    【Redis】谈谈我对Redis布隆过滤器的理解
    NetApp FAS2554故障灯常亮case处理过程分享
    int *a, int **a, int a[], int *a[]的区别
    最新文章合集
    【SLAM】初识SLAM
    css3-盒子模型、内外边距、圆角边框
    Python基于PHP+MySQL的个人网页设计与实现
    Linux - 驱动开发 - RNG框架
    论文翻译:2021_语音增强模型压缩_Performance optimizations on deep noise suppression models
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/127744138