java - Accepting different Set-implementations as parameter -
i trying write function capable of accepting 1 of these 2 objects parameter:
private static treeset<string> treeset = new treeset<string>(); private static hashset<string> hashset = new hashset<string>(); i've attempted
public static void printset(set s) { // } and
public static void printset(set<string> s) { // } even while casting sets @ invocation error. proper way this?
update 1
i've tried , without casting:
printset(set); printset((set) set); printset((set<string> set); update 2
the error:
exception in thread "main" java.lang.classcastexception: treeset cannot cast java.util.set @ hashtreesetclient.main(hashtreesetclient.java:34)
just use generics did.
public static <t> void printset(set<t> s) { // } avoid raw types.
or set<string> s else said.
public static void printset(set<string> s) { // } set interface every class implement interface accepted in method.
Comments
Post a Comment