Convert Collection to HashSet - Java java.util

Java examples for java.util:Collection Convert

Description

Convert Collection to HashSet

Demo Code


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Main {
  @SafeVarargs//  w ww.j a v  a  2 s.com
  public static <T> Set<T> hashSet(T... values) {
    Set<T> set = new HashSet<>();
    for (T value : values) {
      set.add(value);
    }
    return set;
  }

  public static <T> Set<T> hashSet(Collection<T> values) {
    Set<T> set = new HashSet<>();
    for (T value : values) {
      set.add(value);
    }
    return set;
  }
}

Related Tutorials