Here you can find the source of toSet(Set> set)
@SuppressWarnings("unchecked") public static <T> Set<T> toSet(Set<?> set)
//package com.java2s; //License from project: Open Source License import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Main { @SuppressWarnings("unchecked") public static <T> Set<T> toSet(Set<?> set) { Set<T> setOfType = new HashSet<T>(set.size()); Iterator<?> iter = set.iterator(); while (iter.hasNext()) { T element = (T) iter.next(); setOfType.add(element);/* www. j a v a 2 s .com*/ } return setOfType; } }