map接口、hashmap常用方法

注意:map中键不能重复(是否重复是根据equals方法判断),否则新的会覆盖为旧的

范例:

public class TestMap {
public static void main(String[] args) {
Map<Integer, String> m1 = new HashMap<Integer, String>();

m1.put(1, "aaa");
m1.put(2, "bbb");
m1.put(3, "ccc");

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

System.out.println(m1.size());
System.out.println(m1.isEmpty());
System.out.println(m1.containsKey(2));
System.out.println(m1.containsValue("ccc"));

Map<Integer, String> m2 = new HashMap<Integer, String>();
m2.put(4, "si");
m2.put(5, "wu");

m1.putAll(m2);

System.out.println(m1);

//map中键不能重复,否则新的会覆盖为旧的
m1.put(3, "san");
System.out.println(m1);
}
}

猜你喜欢

转载自www.cnblogs.com/LuJunlong/p/11588195.html