HashSet和HashMap的区别
Java知识点 约 3.8 千字
HashMap
Map<String, String> map = new HashMap<>();
map.put("A", "Apple");
map.put("B", "Ball");
map.forEach((k, v) -> System.out.println(k + " " + v));
HashSet
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.forEach(System.out::println);
HashSet底层是HashMap,HashSet的对象值存储于HashMap的key部分;
HashSet不能有重复值,在插入时若已有相同元素将插入失败,HashMap有重复k值存储时将覆盖此k对应的v;