码迷,mamicode.com
首页 > 编程语言 > 详细

Java集合(七)、Map集合 HashMap(常用)方法

时间:2019-08-14 21:39:50      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:image   value   执行   void   java   object   contains   static   pre   

 

 

HashMap的存值

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(1,"Jeo");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
    }
}

注:添加重复的键会被最新添加的覆盖

技术图片

 

HashMap的取值

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.get(3));
        System.out.println(map.get("Sezzy"));
    }
}

技术图片

 

HashMap的判断为空

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.isEmpty());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.isEmpty());
    }
}

技术图片

 

HashMap判断是否含有key

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsKey(4));
        System.out.println(map.containsKey(2));
    }
}

技术图片

 

 HashMap判断是否含有value

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.containsValue("wang"));
        System.out.println(map.containsValue("Sezzy"));
    }
}

技术图片

 

HashMap删除这个key下的value

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.remove(2));
        System.out.println(map);
    }
}

技术图片

 

HashMap显示所有的value值

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.values());
        System.out.println(map);
    }
}

技术图片

 

HashMap的元素个数

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.size());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.size());
    }
}

技术图片

 

HashMap取出全部的key

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        System.out.println(map.keySet());
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        System.out.println(map.keySet());
        map.put(3,"Pit");
        System.out.println(map.keySet());
        System.out.println(map);
    }
}

技术图片

 

HashMap替换key的value

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        System.out.println(map.replace(3, "Jack"));
        System.out.println(map);
    }
}

技术图片

 

清空这个HashMap

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.clear();
        System.out.println(map);
    }
}

技术图片

 

HashMap的克隆

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map.clone());
        Object clone = map.clone();
        System.out.println(clone);
    }
}

技术图片

 

如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(maps);
        System.out.println(map.putIfAbsent(1, "wang"));
        System.out.println(map.putIfAbsent(4, "Lee"));
        System.out.println(map);
    }
}

技术图片

 

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况 

import java.util.*;

public class test{
    public static void main(String[] args) {
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"leslie");
        map.put(2,"Sezzy");
        map.put(3,"Pit");
        System.out.println(map);
        map.compute(2, (k, v) -> v == null ? "Suzan" : "Faker");
        System.out.println(map);
    }
}

技术图片

 

Java集合(七)、Map集合 HashMap(常用)方法

标签:image   value   执行   void   java   object   contains   static   pre   

原文地址:https://www.cnblogs.com/lixiansheng/p/11354623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!