Map接口_HashMap常用方法
Map类的特点
现实生活中,我们经常需要成对存储某些信息,比如一个手机号对应一个微信账户,这就是成对存储的关系。
Map就是用来存储“键(key)-值(value)对”的。Map类中存储“键值对”通过“键(key)”来标识,所以键对象不能重复。
Map接口的实现类
Map接口常用的实现类有:HashMap,TreeMap,HashTable,Properties等
Map接口常用的方法
代码测试
public class TestMap {
public static void main(String[] args) {
Map <Integer,String> m1 = new HashMap<>();
m1.put(1, "阿狸");
m1.put(2, "卡莎");
m1.put(3, "阿卡丽");
System.out.println(m1);
//键(Key)不能重复,若重复则新的值覆盖旧的值(是否重复是根据equals方法判断)
m1.put(2, "艾瑞利亚");
System.out.println(m1);
}
}
效果