czheng15
czheng15

Reputation: 11

cast set to linkedhashset

following is the scenario that I am encountering

I have this set of string here

Set<String> attributes = new LinkedHashSet<>();

yet I have another function with a signature LinkedHashSet

public static List<String> rerankAttributesByType (LinkedHashSet<String> attributeList)

is it safe for me to just cast attributes to LinkedHashSet and proceed with the function calls?

List<String> newAttributes = rerankAttributesByType((LinkedHashSet<String>) unselectedAttribute)

Upvotes: 0

Views: 698

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198391

This is technically safe, since you know the actual concrete type, but not good practice.

Instead, change

public static List<String> rerankAttributesByType(LinkedHashSet<String> attributeList)

to

public static List<String> rerankAttributesByType (Set<String> attributeList)

so you don't have to do a cast, and you can properly use the interface type throughout your program.

Upvotes: 0

Related Questions