~/ learn/ java-from-zero/ cards/ Set: HashSet, LinkedHashSet, TreeSet
1 of 4

Type a HashSet add that silently ignores a duplicate

Type a HashSet add that silently ignores a duplicate

Answer

Set<String> tags = new HashSet<>(); boolean added1 = tags.add("java"); // true boolean added2 = tags.add("java"); // false System.out.println(tags.size()); // 1

add() returns true on insertion and false when the element already exists. HashSet uniqueness relies on equals and hashCode.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/e2581eb0-6bac-4452-bc1c-018638c166da/flashcard utf-8 LF