java-interview icon indicating copy to clipboard operation
java-interview copied to clipboard

HashSet use only key

Open IvanAgafonov opened this issue 3 years ago • 0 comments

Неверно - "В качестве ключа и значения используется добавляемый элемент.". HashSet использует добавляемый элемент только в качестве ключа, а значение - объект Object.

private static final Object PRESENT = new Object();
public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

https://github.com/enhorse/java-interview/blob/master/jcf.md#%D0%BD%D0%B0%D0%B7%D0%BE%D0%B2%D0%B8%D1%82%D0%B5-%D0%BE%D1%81%D0%BD%D0%BE%D0%B2%D0%BD%D1%8B%D0%B5-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D1%8B-jcf-%D0%B8-%D0%B8%D1%85-%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D0%B8

IvanAgafonov avatar Mar 28 '21 20:03 IvanAgafonov