Java Hashmap 笔记

Map.Entry: https://blog.csdn.net/weixin_40959890/article/details/106715786

直接来代码

package wensong.com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

public class HashMapTest {

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("WenSong", "28");//存放键值对

        System.out.println(map.containsKey("WenSong"));//键中是否包含这个数据
        System.out.println(map.containsKey("Long"));
        System.out.println("00000000000000000");

        System.out.println(map.get("zhang"));//通过键拿值
        System.out.println(map.get("Long"));
        System.out.println("111111111111111111111111");

        System.out.println(map.isEmpty());//判空
        System.out.println(map.size());
        System.out.println("22222222222222222222222222");

        System.out.println(map.remove("WenSong"));//从键值中删除
        // remove刪除key的時候,會在控制台打印對應的values
        System.out.println(map.containsKey("Wensong"));
        System.out.println(map.get("Wensong"));//获取值
        System.out.println(map.isEmpty());
        System.out.println(map.size());
        System.out.println("333333333333333333333333333333");

        map.put("Wensong", "28");
        System.out.println(map.get("Wensong"));
        map.put("Wensong", "28");
        System.out.println(map.get("Wensong"));
       // 多存放不會增加,相同的键值对只存一次
        System.out.println("4444444444444444444444444");

        map.put("Wensong", "28");
        map.put("Long", "27");
        map.put("Lin", "18");

        for (String key : map.keySet()) {
            System.out.println(key);
        }
        System.out.println("5555555555555555555555555");

        for (String values : map.values()) {
            System.out.println(values);
        }
        System.out.println("666666666666666666666666666");

        map.clear();
       //相同的key只能存一个,但是key不同时value可以相同
        map.put("A", "1");
        map.put("B", "2");
        map.put("C", "3");
        map.put("D", "1");
        map.put("E", "2");
        map.put("F", "3");
        map.put("G", "1");
        map.put("H", "2");
        map.put("I", "3");
        for (Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + ":" + value);
        }
        System.out.println("77777777777777777777777");
         /*不能像下面一样迭代删除map中的元素
        for(Entry<String,String> entry : map.entrySet()){
            if(!entry.getValue().equals("1")){
                map.remove(entry.getKey());
            }
         }
         如果你想删除元素,把需要删除的元素放在一个新的List中,
         然后用下面的方法删除
         */
        List<String> removeKeys = new ArrayList<String>();
        for (Entry<String, String> entry : map.entrySet()) {
            if (!entry.getValue().equals("1")) {
                removeKeys.add(entry.getKey());
            }
        }
        for (String removeKey : removeKeys) {
            map.remove(removeKey);
        }
        for (Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + ":" + value);
        }
        System.out.println("~");
    }
}


 

执行结果:


true
false
00000000000000000
null
null
111111111111111111111111
false
1
22222222222222222222222222
28
false
null
true
0
333333333333333333333333333333
28
28
4444444444444444444444444
Lin
Long
Wensong
5555555555555555555555555
18
27
28
666666666666666666666666666
A:1
B:2
C:3
D:1
E:2
F:3
G:1
H:2
I:3
77777777777777777777777
A:1
D:1
G:1
~

Process finished with exit code 0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值