java——基础数据类型——map集合操作

这篇博客详细介绍了Java中HashMap的数据结构和常用操作,包括初始化、put、get、containsKey、remove等方法的使用,并展示了示例代码及运行结果。通过实例展示了HashMap如何存储和检索元素,以及如何检查键值对的存在和删除元素。
摘要由CSDN通过智能技术生成

import java.util.*;

public class map

{

    public static void main( String[] args )

    {

        //Map list = new HashMap();

        Map<Integer, String> map = new HashMap<>();

        map.put(1,"shen");

        map.put(2,"wei");

        System.out.println(map.size());

        System.out.println("==================================1");

        System.out.println(map);

        System.out.println("==================================2");

        System.out.println(map.get(1));

        System.out.println(map.get(2));

        System.out.println("==================================3");

        Boolean a = map.containsKey(1);  //此映射是否包含

        System.out.println(a);

        System.out.println("==================================4");

        Boolean b = map.containsKey(3);

        System.out.println(b);

        System.out.println("==================================5");

        map.remove(2);

        System.out.println(map.size());

        System.out.println(map);

        System.out.println("==================================6");

        Object  value = map.get(1);

        System.out.println(value);

        System.out.println("==================================7");

        Boolean c = map.containsValue("shen");   //是否包含指定值

        System.out.println(c);

        System.out.println("==================================8");

        Boolean d = map.isEmpty();            //判断是否为空

        System.out.println(d);

        System.out.println("==================================9");

        map.put(2,"wei");

        map.put(3,"wei");

        Set<Integer> key = map.keySet();   //获取map中所有的key值,

        System.out.println(key);

        System.out.println("==================================9______1");

        Collection<String> value4 = map.values();   //获取map中所有的value值,

        System.out.println(value4);

        System.out.println("==================================9______2");

        map.clear();      //清空

        Boolean e = map.isEmpty();

        System.out.println(e);

        System.out.println("==================================10");

    }

}

==============================================================================================================================================================

执行结果:

2

==================================1

{1=shen, 2=wei}

==================================2

shen

wei

==================================3

true

==================================4

false

==================================5

1

{1=shen}

==================================6

shen

==================================7

true

==================================8

false

==================================9

[1, 2, 3]

==================================9______1

[shen, wei, wei]

==================================9______2

true

==================================10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值