tree set

PHOTO EMBED

Fri Jun 07 2024 13:20:36 GMT+0000 (Coordinated Universal Time)

Saved by @dbms

import java.util.TreeSet;
import java.util.Iterator;

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

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

        // Display the TreeSet
        System.out.println("Fruits in the TreeSet: " + 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 TreeSet
        System.out.println("Size of the TreeSet: " + 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 TreeSet using an Iterator
        System.out.println("Iterating through the TreeSet using an Iterator:");
        Iterator<String> iterator = fruits.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

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

        // Convert the TreeSet 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 TreeSet is empty
        System.out.println("Is the TreeSet empty? " + fruits.isEmpty());

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

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

        // Add all elements from another collection
        TreeSet<String> moreFruits = new TreeSet<>();
        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
        TreeSet<String> retainFruits = new TreeSet<>();
        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);

        // Accessing the first and last elements
        System.out.println("First fruit in the TreeSet: " + moreFruits.first());
        System.out.println("Last fruit in the TreeSet: " + moreFruits.last());

        // Subset of the TreeSet
        TreeSet<String> subSet = (TreeSet<String>) moreFruits.subSet("Kiwi", "Mango");
        System.out.println("Subset from 'Kiwi' to 'Mango': " + subSet);
    }
}
content_copyCOPY