EN
Java - create and initialize Set<> simultaneously
5
points
In this short article, we would like to show how to create and initialize Set<>
simultaneously in Java.
Quick solution:
// import java.util.Set;
Set<String> set = Set.of("a", "b", "c");
Note:
Set.of()
method was intoduced in Java 9.
Practical example
import java.util.Set;
public class Program {
public static void main(String[] args) {
Set<Boolean> set1 = Set.of(true, false);
Set<Integer> set2 = Set.of(1, 2, 3);
Set<String> set3 = Set.of("a", "b", "c");
}
}