How to create a Set of Sets in Java - Step by Step Examples



How to create a Set of Sets in Java ?

Answer

To create a Set of Sets in Java, you can use the HashSet class to create individual sets and then nest them within another set using the same class.



✐ Examples

1 Creating a Set of Sets of Integers

In this example,

  1. First, we create three individual sets of integers named set1, set2, and set3 using the HashSet class. We add the elements 1, 2, 3 to set1, 4, 5, 6 to set2, and 7, 8, 9 to set3.
  2. Next, we create a set of sets named setOfSets using the HashSet class, and we add set1, set2, and set3 to this set. This nests the three sets within a single set.
  3. We then use an enhanced for loop to iterate over each set within setOfSets. Within the loop, we print each individual set to the standard output.

Java Program

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<Integer> set1 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);

        Set<Integer> set2 = new HashSet<>();
        set2.add(4);
        set2.add(5);
        set2.add(6);

        Set<Integer> set3 = new HashSet<>();
        set3.add(7);
        set3.add(8);
        set3.add(9);

        Set<Set<Integer>> setOfSets = new HashSet<>();
        setOfSets.add(set1);
        setOfSets.add(set2);
        setOfSets.add(set3);

        for (Set<Integer> set : setOfSets) {
            System.out.println(set);
        }
    }
}

Output

Output:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

2 Creating a Set of Sets of Strings

In this example,

  1. First, we create three individual sets of strings named setA, setB, and setC using the HashSet class. We add the elements "apple", "banana", "cherry" to setA, "dog", "elephant", "fox" to setB, and "grape", "honeydew", "kiwi" to setC.
  2. Next, we create a set of sets named setOfStringSets using the HashSet class, and we add setA, setB, and setC to this set. This nests the three sets within a single set.
  3. We then use an enhanced for loop to iterate over each set within setOfStringSets. Within the loop, we print each individual set to the standard output.

Java Program

import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> setA = new HashSet<>();
        setA.add("apple");
        setA.add("banana");
        setA.add("cherry");

        Set<String> setB = new HashSet<>();
        setB.add("dog");
        setB.add("elephant");
        setB.add("fox");

        Set<String> setC = new HashSet<>();
        setC.add("grape");
        setC.add("honeydew");
        setC.add("kiwi");

        Set<Set<String>> setOfStringSets = new HashSet<>();
        setOfStringSets.add(setA);
        setOfStringSets.add(setB);
        setOfStringSets.add(setC);

        for (Set<String> set : setOfStringSets) {
            System.out.println(set);
        }
    }
}

Output

Output:
[apple, banana, cherry]
[dog, elephant, fox]
[grape, honeydew, kiwi]

Summary

In this tutorial, we learned How to create a Set of Sets in Java language with well detailed examples.




More Java Sets Tutorials

  1. How to create an Empty Set in Java ?
  2. How to Initialize a Set in Java ?
  3. How to Get Length of a Set in Java ?
  4. How to create a Set of size N in Java ?
  5. How to create a Set of Numbers from 1 to N in Java ?
  6. How to create a Set of integers in Java ?
  7. How to create a Set of Strings in Java ?
  8. How to Access Items in a Set in Java ?
  9. How to get a Random Item in a Set in Java ?
  10. How to Iterate Over a Set in Java ?
  11. How to check if a Set is Empty in Java ?
  12. How to check if a Set is Not Empty in Java ?
  13. How to get Subset from a Set in Java ?
  14. How to check if a Specific Item is present in the Set in Java ?
  15. How to check if a Set contains all the items of Another Set in Java ?
  16. How to Sort Items of a Set in Java ?
  17. How to Copy a Set in Java ?
  18. How to add an Item to a Set in Java ?
  19. How to find Union of Two Sets in Java ?
  20. How to find Intersection of Two Sets in Java ?
  21. How to check if Two Sets are Equal in Java ?
  22. How to Convert a Set of Integers to a Set of Strings in Java ?
  23. How to Convert a Set of Strings to a Set of Integers in Java ?
  24. How to Convert a Set of Floats to a Set of Strings in Java ?
  25. How to Convert a Set of Strings to a Set of Floats in Java ?
  26. How to Filter Items of a Set based on a Condition in Java ?
  27. How to Remove Specific Item from a Set in Java ?
  28. How to Remove Items from Set based on a Condition in Java ?
  29. How to create a Set of Sets in Java ?