hashset

PHOTO EMBED

Fri Jun 07 2024 13:18:27 GMT+0000 (Coordinated Universal Time)

Saved by @dbms

import java.util.HashSet;
import java.util.Iterator;

public class HashSetExample {
    public static void main(String[] args) {
        // Create a HashSet
        HashSet<String> fruits = new HashSet<>();

        // Add elements to the HashSet
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add("Date");
        fruits.add("Elderberry");
        fruits.add("Fig");
        fruits.add("Grape");

        // Display the HashSet
        System.out.println("Fruits in the HashSet: " + fruits);

        // Attempt to add a duplicate element
        boolean isAdded = fruits.add("Apple");
        System.out.println("Attempt to add 'Apple' again: " + isAdded); // Should print false

        // Check the size of the HashSet
        System.out.println("Size of the HashSet: " + fruits.size());

        // Check if an element exists
        boolean containsCherry = fruits.contains("Cherry");
        System.out.println("Does the set contain 'Cherry'? " + containsCherry);

        // Remove an element
        fruits.remove("Banana");
        System.out.println("Fruits after removing 'Banana': " + fruits);

        // Iterate through the HashSet using an Iterator
        System.out.println("Iterating through the HashSet using an Iterator:");
        Iterator<String> iterator = fruits.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // Iterate through the HashSet using a for-each loop
        System.out.println("Iterating through the HashSet using a for-each loop:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Convert the HashSet to an array
        Object[] fruitsArray = fruits.toArray();
        System.out.println("Fruits as an array:");
        for (Object fruit : fruitsArray) {
            System.out.println(fruit);
        }

        // Check if the HashSet is empty
        System.out.println("Is the HashSet empty? " + fruits.isEmpty());

        // Clone the HashSet
        HashSet<String> clonedFruits = (HashSet<String>) fruits.clone();
        System.out.println("Cloned HashSet: " + clonedFruits);

        // Clear the HashSet
        fruits.clear();
        System.out.println("Is the HashSet empty after clearing? " + fruits.isEmpty());

        // Add all elements from another collection
        HashSet<String> moreFruits = new HashSet<>();
        moreFruits.add("Kiwi");
        moreFruits.add("Lemon");
        moreFruits.add("Mango");
        fruits.addAll(moreFruits);
        System.out.println("Fruits after adding more fruits: " + fruits);

        // Retain only certain elements
        HashSet<String> retainFruits = new HashSet<>();
        retainFruits.add("Kiwi");
        retainFruits.add("Mango");
        fruits.retainAll(retainFruits);
        System.out.println("Fruits after retaining certain fruits: " + fruits);

        // Remove all elements from another collection
        fruits.removeAll(retainFruits);
        System.out.println("Fruits after removing retained fruits: " + fruits);
    }
}
content_copyCOPY