HashSet的內部調用HashMap中,以避免在執行的Hashset如何避免重複
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方法的要素是什麼? –
你的意思是All Set Implementation避免使用Map的重複public boolean add(E e){ \t return map.put(e,PRESENT)== null; } – Martin