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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Declaring HashSet

A

HashSet hashset = new HashSet();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Add an Item to Hashset

A

hashset.add(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Remove an Item to Hashset

A

hashset.remove(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Clearing all element from a HashSet

A

hashset.clear();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Iterating over an HashSet (Forward)

A
Method 1 - ITERATOR 
Iterator iterator = hashset.iterator();
    while (iterator.hasNext()) {
       System.out.println(iterator.next());
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Does the HashSet contains this Item ?

A

hashset.contains(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is the HashSet empty ?

A

hashset.isEmpty();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Whats the length/size of the HashSet?

A

hashset.size();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly