zoukankan      html  css  js  c++  java
  • Map容器

    映射(Map) 是一个存储键/值对的对象

    键是唯一的,值可以重复

    方法

      size()

      isEmpty()

      containsKey()      判断容器里是否包含指定的键

      containsValue()  判断容器里是否包含指定的值

      get()   根据键来获取对应的值

      put()   把键值对数据添加到数组中

      remove()    删除对应的key

      values()  获取所有的值

      keySet( )   获取map中所有的键

      entrySet()   返回包含的映射关系的Set视图

    HashMap类是基于哈希表的map接口的实现,并允许使用null键和null值

    实现Map并扩展AbstractMap,本身并没有增加任何新的方法

    Hashtable   线程安全的  单线程使用会低

    package com.day1;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    public class HashMapDemo {
      public static void main(String[] args) {
         Map<String, String> hMap=new HashMap<String, String>();
         hMap.put("A", "张三");
         hMap.put("B", "李四");
         hMap.put("C", "王五");
         hMap.put("D", "周六");
         System.out.println(hMap);
        // 获取map中所有的键
         Set<String>  hsSet=hMap.keySet( );
         System.out.println(hsSet);
         for (String string : hsSet) {
            System.out.println(string);
         }
        // 获取map中所有的值
         Collection<String> strings=hMap.values();
         System.out.println(strings);
         for (String string : strings) {
            System.out.println(string);
        }
         //得到key,并得到key所对应的值
         Set<String>  k=hMap.keySet( );
         for (String string : k) {
            System.out.println(string+hMap.get(string));
        }
         //当我们调用put(key,value)方法的时候,首先会把key和value封装到Entry这个静态内部类对象中
         //把Entry对象添加到数组中,所以我们想获取map中的所有键值对 ,我们只要获取数组中所有的Entry对象.接下来调用
         //Entry对象中的getkey和getvalue方法,获取键值对
        Set<Entry<String, String>> eSet= hMap.entrySet();
        for (Entry<String, String> entry : eSet) {
            System.out.println(entry.getValue());
        }
    }
    }

    /*
    * hashmap调用默认构造方法会产生一个底层是16的Entry的数组
    * int hash=hash(key.hashcode())
    * 首先调用key的hashcode方法来得到一个整数--哈希码
    * 把哈希码作为参数传到hash函数中来进行运算--散列运算-得到了一个整型--散列值
    * int i=indexFor(hash,table.length);
    * 把散列值和数组的长度来进行运算 ,最终得到Entry对象存放到数组的位置,下标
    *
    * hashmap内部结构是数组链表结构
    * 因为不同的key有可能算出来是相同的散列值,根据散列值计算出存放到数组的下标会冲突
    *
    */

    哈希码的产生和使用

    当调用对象的hashcode()方法时就会返回当前对象的哈希码值,支持此方法时为了提高哈希表的性能

    package com.day1;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class HashMapDemo2 {
      public static void main(String[] args) {
         Map<person, String> map=new HashMap<person, String>();
         map.put(new person("A", 20),"张三");
         map.put(new person("B", 30),"李四");
         map.put(new person("C", 60),"王五");
         map.put(new person("B", 30),"玫瑰");
         System.out.println(map.size());
         
    }
    }
    
    class person{
        private String name;
        private int age;
        public person(String name, int age) {
            
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            person other = (person) obj;
            if (age != other.age)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
        
        
        
    }

  • 相关阅读:
    如何在腾讯云上安装Cloud Foundry
    Chrome浏览器扩展程序的本地备份
    如何在Kubernetes里创建一个Nginx service
    如何在Kubernetes里创建一个Nginx应用
    在Mac里给Terminal终端自定义颜色
    linux sed命令详解
    跟我一起写Makefile--- 变量(嵌套变量+追加变量+overrid+多行变量+环境变量+目标变量+模式变量)
    makefile详解 嵌套执行make,定义命令包
    makefile学习笔记(多目录嵌套调用、变量使用)
    Makefile所有内嵌函数
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9980279.html
Copyright © 2011-2022 走看看