Java Collection Tutorial - Java HashSet(Collection <? extends E > c) Constructor








Syntax

HashSet(Collection <? extends E > c) constructor from HashSet has the following syntax.

public HashSet(Collection <? extends E> c)

Example

In the following code shows how to use HashSet.HashSet(Collection <? extends E > c) constructor.

/* w w  w .  j a va  2 s  .  c  o m*/

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Main {
  public static void main(String args[]) {
    List<String> list = new ArrayList<>();
    list.add("java2s.com");
    list.add("java2s.com");
    list.add("java2s.com");
    
    HashSet<String> hs = new HashSet<String>(list);
 
    System.out.println(hs);
  }
}
  

The code above generates the following result.