To create a Set
with values in Java, you can use the of
method of the Set
interface, like this:
Set<String> set = Set.of("value1", "value2", "value3");
This creates a new Set
called set
that contains the three strings "value1", "value2", and "value3". The Set
interface is a part of the Java Collection Framework and represents a collection of unique elements.
You can also use the of
method to create an ImmutableSet
, which is a Set
that cannot be modified after it is created. To do this, you can use the copyOf
method of the ImmutableSet
class, like this:
Set<String> set = ImmutableSet.copyOf(Set.of("value1", "value2", "value3"));
This creates a new ImmutableSet
called set
that contains the three strings "value1", "value2", and "value3".