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,
- First, we create three individual sets of integers named
set1
,set2
, andset3
using theHashSet
class. We add the elements1, 2, 3
toset1
,4, 5, 6
toset2
, and7, 8, 9
toset3
. - Next, we create a set of sets named
setOfSets
using theHashSet
class, and we addset1
,set2
, andset3
to this set. This nests the three sets within a single set. - 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,
- First, we create three individual sets of strings named
setA
,setB
, andsetC
using theHashSet
class. We add the elements"apple", "banana", "cherry"
tosetA
,"dog", "elephant", "fox"
tosetB
, and"grape", "honeydew", "kiwi"
tosetC
. - Next, we create a set of sets named
setOfStringSets
using theHashSet
class, and we addsetA
,setB
, andsetC
to this set. This nests the three sets within a single set. - 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
- How to create an Empty Set in Java ?
- How to Initialize a Set in Java ?
- How to Get Length of a Set in Java ?
- How to create a Set of size N in Java ?
- How to create a Set of Numbers from 1 to N in Java ?
- How to create a Set of integers in Java ?
- How to create a Set of Strings in Java ?
- How to Access Items in a Set in Java ?
- How to get a Random Item in a Set in Java ?
- How to Iterate Over a Set in Java ?
- How to check if a Set is Empty in Java ?
- How to check if a Set is Not Empty in Java ?
- How to get Subset from a Set in Java ?
- How to check if a Specific Item is present in the Set in Java ?
- How to check if a Set contains all the items of Another Set in Java ?
- How to Sort Items of a Set in Java ?
- How to Copy a Set in Java ?
- How to add an Item to a Set in Java ?
- How to find Union of Two Sets in Java ?
- How to find Intersection of Two Sets in Java ?
- How to check if Two Sets are Equal in Java ?
- How to Convert a Set of Integers to a Set of Strings in Java ?
- How to Convert a Set of Strings to a Set of Integers in Java ?
- How to Convert a Set of Floats to a Set of Strings in Java ?
- How to Convert a Set of Strings to a Set of Floats in Java ?
- How to Filter Items of a Set based on a Condition in Java ?
- How to Remove Specific Item from a Set in Java ?
- How to Remove Items from Set based on a Condition in Java ?
- How to create a Set of Sets in Java ?