How to avoid unchecked cast warning when cloning a HashSet?

Tim picture Tim · Feb 12, 2012 · Viewed 9.1k times · Source

I'm trying to make a shallow copy of a HashSet of Points called myHash. As of now, I have the following:

HashSet<Point> myNewHash = (HashSet<Point>) myHash.clone();

This code gives me an unchecked cast warning however. Is there a better way to do this?

Answer

Ted Hopp picture Ted Hopp · Feb 12, 2012

You can try this:

HashSet<Point> myNewHash = new HashSet<Point>(myHash);