日志文章

2019-12-20 aflfte2011

Map的常用方法

package com.aflfte.mycollectiok;

import java.util.HashMap;
import java.util.Map;

/**
 * HashMap的使用方法
 * @author jinhao
 *
 */
public class TestMap {
public static void main(String[] args) {
Map<Integer,String> m1=new HashMap<>();
m1.put(1, "one");//向Map当中添加键值对
m1.put(2, "two");
m1.put(3,"three");
System.out.println(m1.get(1));//获取key为1的键值对
System.out.println(m1.size());//查看Map当中有多少个键值对
System.out.println(m1.isEmpty());//判断map是不是空
System.out.println(m1.containsKey(2));//判断Map当中有没有值为2的key
System.out.println(m1.containsValue("333"));//判断Map当中是否包含333的值
Map<Integer,String>m2=new HashMap<Integer, String>();
m2.put(4, "四");
m2.put(5, "五");
m1.putAll(m2);//将m2的所有内容加入到m1当中
System.out.println(m1);
//Map当中的KEY(键)值不可重复(这里的重复判断是用equals方法来进行判断的),如果重复则新的值会覆盖旧的值!!
m1.put(3, "三");
System.out.println(m1);
}
}


« hashset的使用方法 | Collection接口的使用方法»