![]() |
VOOZH | about |
In HashSet, duplicates are not allowed. If we are trying to insert duplicates then we won't get any compile-time or runtime error and the add() method simply returns false.
We can use 2 ways to print HashSet elements:
Method 1: By using Cursor which is Iterator.
public Iterator iterator(); // Iterator method of Collection Interface.
Iterator itr = c.iterator(); // where c is any Collection Object like ArrayList,HashSet etc.
Example:
null 2 3 5 6
Method 2: We can directly print HashSet elements by using the HashSet object reference variable. It will print the complete HashSet object.
Note: If we do not know the hash code, so you can't decide the order of insertion.
Example:
[A, B, C, D, null, Z, 10]