Create new Hash Set from Iterable<? extends T> - Java java.util

Java examples for java.util:Iterable Convert

Description

Create new Hash Set from Iterable<? extends T>

Demo Code

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

public class Main {
  public static void main(String[] argv) {
    System.out.println(newHashSet());
  }//w  w  w .  ja  va  2s  . c om

  public static <T> HashSet<T> newHashSet() {
    return new HashSet<T>();
  }

  public static <T> HashSet<T> newHashSet(int size) {
    return new HashSet<T>(size);
  }

  public static <T> HashSet<T> newHashSet(Collection<? extends T> c) {
    return new HashSet<T>(c);
  }

  public static <T> HashSet<T> newHashSet(Iterable<? extends T> iterable) {
    HashSet<T> set = newHashSet();
    for (T t : iterable) {
      set.add(t);
    }
    return set;
  }
}

Related Tutorials