对比二者的异同点,且Set中:
Set<String> names = new HashSet<String>();//调用HashSet构造方法
//public HashSet() {
// map = new HashMap<>();//为map成员变量赋值,HashMap对象
//}
names.add("Jim");//源码:
//public boolean add(E e) {
// return map.put(e, PRESENT)==null;//向Map集合中HashMap容器添加数据 PRESENT常量\
//本质:HashSet存储数据时本质是存到了HashMap集合的key
//}
可以看出关联
public static void main(String[] args) {
Map<String,Integer> scores = new HashMap<String,Integer>();
scores.put("aa",1);//输入数据
scores.put("aa",1);
//Map不能存储相同key的词,所以只存了一个
int score = scores.get("aa");//获取相应key所对应的value
System.out.println(score);
boolean flag = scores.isEmpty();//判断是否为空
System.out.println(flag);
//scores.clear();//清空数据
int size = scores.size();
System.out.println(size);
scores.replace("aa", 4);//替代特定key的value为新数据
scores.replace("aa", 4, 2);
score = scores.get("aa");
System.out.println(score);
}