1. 程式人生 > >Collection Set&HashSet&TreeSet(HashMap實現,去重特性)

Collection Set&HashSet&TreeSet(HashMap實現,去重特性)

ima alt for spec trees boolean cti image 分享圖片

一、HashSet

1. 慮重功能特性(HashMap實現)

技術分享圖片

2. put(key) 如果重復返回false

  /**
     * Adds the specified element to this set if it is not already present.
     * More formally, adds the specified element <tt>e</tt> to this set if
     * this set contains no element <tt>e2</tt> such that
     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
     * If this set already contains the element, the call leaves the set
     * unchanged and returns <tt>false</tt>.
     *
     * 
@param e element to be added to this set * @return <tt>true</tt> if this set did not already contain the specified * element */ public boolean add(E e) { return map.put(e, PRESENT)==null; // 重復返回false }

Collection Set&HashSet&TreeSet(HashMap實現,去重特性)