常用函数
代码示例
package cn.hanquan.test;
import java.util.HashMap;
import java.util.Map;
//HashMap的使用
public class HashMapTest
{
public static void main(String[] args)
{
/*-------------------m1------------------*/
Map m1 = new HashMap<>();
m1.put(1, "one");// 存放键值对:将1和one对应
m1.put(2, "two");
m1.put(3, "three");
System.out.println(m1);
System.out.println(m1.get(1));// 通过键对象查找得到值对象
System.out.println(m1.size());
System.out.println(m1.isEmpty());
System.out.println(m1.containsKey(2));
System.out.println(m1.containsValue("five"));
/*-------------------m2-------------------*/
Map m2 = new HashMap<>();
m2.put(4, "四");// 存放键值对:将1和one对应
m2.put(5, "五");
m2.put(6, "六");
System.out.println(m1);
m1.putAll(m2);// 把m2合并到m1上
System.out.println(m1);
}
}
运行结果
{1=one, 2=two, 3=three} one 3 false true false {1=one, 2=two, 3=three} {1=one, 2=two, 3=three, 4=四, 5=五, 6=六}