class EmptyHashSetExample{
public static void main(String[] args) { // Create a HashSet
HashSet hset = new HashSet(); //add elements to HashSet
hset.add("Element1");
hset.add("Element2");
hset.add("Element3");
hset.add("Element4");
hset.add("Element5");
System.out.println("Oldin: HashSet contains: "+ hset);
hset.clear(); // Display HashSet content again
System.out.println("Keyin: HashSet contains: "+ hset);
}
}
Ekranda:
Oldin: HashSet contains: [Element1, Element2, Element3, Element4, Element5]
Keyin: HashSet contains: []
HashSet ni massivga o'girish (convert) ga misol:
import java.util.HashSet;
class ConvertHashSettoArray{
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hset = new HashSet<String>(); //add elements to HashSet
hset.add("Element1");
hset.add("Element2");
hset.add("Element3");
hset.add("Element4"); // Displaying HashSet elements
System.out.println("HashSet contains: "+ hset); // Creating an Array
Do'stlaringiz bilan baham: |