Map接口详解

Map接口

  • 双列集合,存储key-value键值对数据; key值不能重复,value值可以重复;
  • 一个key-value键值对构成了一个Entry对象;
  • key所在的类要重写equals()和hashCode()方法(以HashMap为例);
  • key所在的类要重写equals()方法;

HashMap

  • 是Map的主要实现类;
  • 是无序的;
  • 线程不安全的,效率高;
  • 可以存储null的key和value;
LinkedHashMap
  • HashMap子类,保证遍历map元素时,可以按照添加顺序(链表:在HashMap基础上,添加了一对指针,分别指向前一个元素和后一个元素)进行遍历;
  • 对于频繁的遍历操作,此类执行的效率高于HashMap;

TreeMap

  • 保证按照添加的key-value键值对进行排序,实现排序遍历;
  • 要考虑key的自然排序或定制排序;
  • 底层使用数组+链表+红黑树(jdk8)

Hashtable

  • 古老的实现类;
  • 线程安全的,效率低;
  • 不能存储null的key和value;
Properties
  • Hashtable子类
  • 常用来处理配置文件;
  • key和value都是String类型;

HashMap的底层实现原理

JDK7

  1. 实例化以后,底层创建了一个长度为16的一维数组Entry[] table
HashMap map=new HashMap();
  1. 调用put方法添加元素
map.put(key1,value1);

首先,调用key1所在类的hashcode()计算key1哈希值,经过计算得到在数组中的存放位置。

  • 如果此位置上的数据为空,那么key1-value1添加成功;
  • 如果此位置上的数据不为空(此位置以链表形式存在一个或者多个数据),比较key1与已经存在的数据的哈希值:如果不同,则key1-value1添加成功;
  • 如果与已经存在的某一个数据(key2,value2)的哈希值相同,继续比较:调用key1所在类的equals(key2)方法,如果equals()返回false,那么key1-value1添加成功;
  • 如果equals()返回true,那么使用value1替换value2;
  • 对于情况2和情况3:此时的key1-value1与原来的数据以链表形式存储;
  1. 默认扩容方式:扩容为原来容量的2倍,并将原有的数据复制过来;

JDK8

  1. 实例化时没有创建一个长度为16的数组;
  2. jdk8底层的数组是Node[],而非Entry[];
  3. 首次使用put()方法时,底层创建一个长度为16的数组;
  4. jdk7底层结构只有:数组+链表。jdk8中底层结构:数组+链表+红黑树。
  • jdk8:当数组上某一个索引上链表形式存在的数据个数>8且当前数组长度>64时,此时索引位置上的所有数据改为使用红黑树。

Map常用的方法

  • put:添加key-value键值对
        HashMap map=new HashMap();
        map.put("sophia","568");
        map.put("refine","45");
  • putAll:将map1中的键值对全部添加到map中去
        HashMap map1=new HashMap();
        map1.put("AA",123);
        map1.put("BB",123);
        map.putAll(map1);
  • remove:根据key移除键值对
        Object value=map.remove("BB");
        System.out.println(value);
  • clear:清空map
        map.clear();
  • get:获取对应key的value值
        Object value=map.get("docker");
        System.out.println(value);
  • containsKey:判断是否包含对应的key
        System.out.println(map.containsKey("docker"));
  • containsValue:判断是否包含对应的value
        System.out.println(map.containsValue("45"));
  • size:返回map包含的键值对数量
        System.out.println(map.size());
  • isEmpty:判断map集合是否为空
        System.out.println(map.isEmpty());
  • equals:判断当前map结合与其他对象是否相等
        System.out.println(map.equals("ddd"));

        HashMap map1=new HashMap();
        map1.put("sophia","568");
        map1.put("refine","45");
        map1.put("docker","478");
        System.out.println(map.equals(map1));

HashMap的遍历

  1. 遍历keys:map.keySet();
		//遍历keys
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
  1. 遍历values:map.values();
 		//遍历values
        Collection values = map.values();
        Iterator iterator1=values.iterator();
        while (iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
  1. 遍历map:map.entrySet();
        //遍历map
        Set entrySet = map.entrySet();
        Iterator iterator2=entrySet.iterator();
        while (iterator2.hasNext()){
            Object obj=iterator2.next();
            Map.Entry entry= (Entry) obj;
            System.out.println(entry.getValue() + "-----> "+entry.getKey());
        }
  1. 遍历map:使用map.keySet();
     	//遍历map
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Object key=iterator.next();
            Object value=map.get(key);
            System.out.println(key+" = "+value);
        }

TreeMap排序

TreeMap自然排序

  • 重写equals()和hashCode()方法
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Object o) {
        if(o instanceof User ){
            User user= (User) o;
            int compare=this.name.compareTo(user.name);
            if(compare!=0) {
                return compare;
            } else {
                return Integer.compare(this.age,user.age);
            }
        }else {
            throw new RuntimeException("输入的类型不匹配");
        }
    }
  • 遍历排序
 @Test
    public void test1(){
        TreeMap treeMap=new TreeMap();
        User u3=new User("LILY",28);
        User u4=new User("WANG",15);
        User u1=new User("JIM",123);
        User u2=new User("JIM",65);

        treeMap.put(u1,56);
        treeMap.put(u2,88);
        treeMap.put(u3,66);
        treeMap.put(u4,99);

        //自然排序
        Set set = treeMap.entrySet();
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            Object obj=iterator.next();
            Map.Entry entry= (Map.Entry) obj;
            System.out.println(entry.getKey()+"---->"+entry.getValue());
        }
    }

TreeMap定制排序

    @Test
    public void test2(){


       Comparator comparator=new Comparator() {
           @Override
           public int compare(Object o1, Object o2) {
               if(o1 instanceof User && o1 instanceof User){
                   User u1= (User) o1;
                   User u2= (User) o2;
                   return Integer.compare(((User) o1).getAge(),((User) o2).getAge());
               }

               throw  new RuntimeException("输入的类型不匹配");
           }
       };

        TreeMap treeMap=new TreeMap(comparator);
        User u3=new User("LILY",28);
        User u4=new User("WANG",15);
        User u1=new User("JIM",123);
        User u2=new User("JIM",65);

        treeMap.put(u1,56);
        treeMap.put(u2,88);
        treeMap.put(u3,66);
        treeMap.put(u4,99);
        //定制排序
        Set set = treeMap.entrySet();
        Iterator iterator=set.iterator();
        while (iterator.hasNext()){
            Object obj=iterator.next();
            Map.Entry entry= (Map.Entry) obj;
            System.out.println(entry.getKey()+"---->"+entry.getValue());
        }

    }

Properties处理属性文件

  • 在项目下新建jdbc.properties,内容格式为:name=refing
public class PropertiesTest {
    public static void main(String[] args)  {

        FileInputStream fileInputStream=null;
        try {
            Properties pro = new Properties();
            fileInputStream= new FileInputStream("jdbc.properties");
            pro.load(fileInputStream);
            String name = pro.getProperty("name");
            String password = pro.getProperty("password");
            System.out.println("name = " + name);
            System.out.println("password = " + password);
        }catch (Exception e){
            e.getMessage();
        }finally {
            if(fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }

    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅尝酥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值