In Java, you can create a list of sets using the ArrayList class for the outer list and the HashSet class for the inner sets.
In this example,
set1
and set2
using the HashSet
class.listOfSets
using the ArrayList
class and initialize it with the inner sets.import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3));
Set<Character> set2 = new HashSet<>(Arrays.asList('a', 'b', 'c'));
List<Set<?>> listOfSets = new ArrayList<>(Arrays.asList(set1, set2));
for (Set<?> set : listOfSets) {
System.out.println(set);
}
}
}
[1, 2, 3] [a, b, c]
In this example,
listOfSetsEmpty
using the ArrayList
class.emptySet1
and emptySet2
.emptySet1
and emptySet2
to the outer list listOfSetsEmpty
.import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> emptySet1 = new HashSet<>();
Set<Character> emptySet2 = new HashSet<>();
List<Set<?>> listOfSetsEmpty = new ArrayList<>(Arrays.asList(emptySet1, emptySet2));
for (Set<?> set : listOfSetsEmpty) {
System.out.println(set);
}
}
}
[] []
In this tutorial, we learned How to create a List of Sets in Java language with well detailed examples.