【发布时间】:2015-09-12 01:22:54
【问题描述】:
HashSet 内部调用 HashMap 以避免实现中的重复
public HashSet() {
map = new HashMap<E,Object>();
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
举例
代码:
Set hashSet = new HashSet();
hashSet.add("Abraham");
hashSet.add("Billy");
hashSet.add("Billy");
System.out.println("HashSet Value " +hashSet.toString());
输出:
HashSet Value [Billy, Abraham]
【问题讨论】:
-
put方法呢?
-
您的意思是使用 Map public boolean add(E e) { return map.put(e, PRESENT)==null; 避免重复的所有集合实现}