Set Methods Flashcards
1
Q
Points to Note about HashSet
A
A. HashSet doesn’t maintain any order, the elements would be returned in any random order. B. HashSet doesn’t allow duplicates. If you try to add a duplicate element in HashSet, the old value would be overwritten. C. HashSet allows null values however if you insert more than one nulls it would still return only one null value. D. HashSet is non-synchronized. E. The iterator returned by this class is fail-fast which means iterator would throw ConcurrentModificationException if HashSet has been modified after creation of iterator, by any means except iterator own remove method.
2
Q
Declaring HashSet
A
HashSet hashset = new HashSet();
3
Q
Add an Item to Hashset
A
hashset.add(“Element”);
4
Q
Remove an Item to Hashset
A
hashset.remove(“Element”);
5
Q
Clearing all element from a HashSet
A
hashset.clear();
6
Q
Iterating over an HashSet (Forward)
A
Method 1 - ITERATOR Iterator iterator = hashset.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
7
Q
Does the HashSet contains this Item ?
A
hashset.contains(“Element”);
8
Q
Is the HashSet empty ?
A
hashset.isEmpty();
9
Q
Whats the length/size of the HashSet?
A
hashset.size();