Java Utililty Methods Set Create

List of utility methods to do Set Create

Description

The list of methods to do Set Create are organized into topic(s).

Method

HashSetasSet(K... values)
as Set
return new HashSet<K>(Arrays.asList(values));
SetasSet(Object[] array)
as Set
if (array == null) {
    return null;
Set result = new HashSet(array.length);
for (int i = 0; i < array.length; i++) {
    result.add(array[i]);
return result;
...
CollectionasSet(Optional opt)
as Set
if (opt.isPresent()) {
    return Collections.singleton(opt.get());
return Collections.emptySet();
SetasSet(T... a)
as Set
return a != null ? new HashSet(Arrays.asList(a)) : Collections.emptySet();
SetasSet(T... a)
as Set
if (a.length > 1)
    return new HashSet<T>(Arrays.<T>asList(a));
else
    return Collections.singleton(a[0]);
SetasSet(T... args)
as Set
return new HashSet<T>(Arrays.asList(args));
SetasSet(T... array)
as Set
if (array == null) {
    return null;
Set<T> set = new HashSet<T>(array.length * 2);
for (T t : array) {
    set.add(t);
return set;
...
SetasSet(T... array)
Create a set from an array.
Set<T> result = new HashSet<>();
Collections.addAll(result, array);
return result;
SetasSet(T... array)
This method works like Arrays#asList(Object[]) , but returns an instance of Set instead of a list
final HashSet<T> set = new HashSet<T>();
Collections.addAll(set, array);
return set;
SetasSet(T... element)
as Set
HashSet<T> elements = new HashSet<T>(element.length);
Collections.addAll(elements, element);
return elements;