Is there a convenience method to initialize a Set
equivalent to Collections.singleton
, which returns a mutable Set
instead of an immutable one?
Guava is defintely a good solution.
Alternatively, you can do:
Set<T> mySet = new HashSet<>(Arrays.asList(t1, t2, t3));
Guava's Sets
includes:
public static <E> HashSet<E> newHashSet(E... elements)
which:
Creates a mutable HashSet instance containing the given elements in unspecified order.
You can call it with a single item as:
Sets.newHashSet(item);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With