I'm trying to create a generic Intersection function that receives two arrays, the function would return an array with the commonality. The following is an example using what I currently have.
public static <T> T[] Intersection(T[] v1, T[] v2) {
    HashSet<T> set = new HashSet<>();
    set.addAll(Arrays.asList(v1));
    set.retainAll(Arrays.asList(v2));
    T[] v3 = {};
    v3 = set.toArray(v3);
    return v3;
}
My issue with the code above is:  T[] v3 = {} results in the following error Cannot create a generic array of T . If I change the code to the following I get a warning stating Type safety: Unchecked cast from Object[] to T[].
public static <T> T[] Intersection(T[] v1, T[] v2) {
    HashSet<T> set = new HashSet<>();
    set.addAll(Arrays.asList(v1));
    set.retainAll(Arrays.asList(v2));
    T[] v3 = (T[])set.toArray();
    return v3;
}
Is there a safe way to accomplish this?
 
     
    