Given below the sample code :
import java.util.HashSet;
public class CollectionExample {
public static void main(String ... args) {
HashSet<String> hs = new HashSet<String>();
String str1 = new String("Buzz");
String str2 = new String("Buzz");
hs.add(str1);
hs.add(str2);
System.out.println(hs);
}
}
What will be the output of the following ?
1. Compilation error
2. [Buzz,Buzz]
3. [Buzz]
4. Buzz
(3)
The 'HashSet' doesn't allow to store duplicate collections. That's why 'Buzz' is stored once in HashSet 'hs'.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.